Skip to content
Myr

fix: report only external ctx cancel as ctx error

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by desertwitch · committed by desertwitch · parent 0f9cd5b · Browse files · 4 files+50 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+50 added−12 removed

  • other:+50 added−12 removed

4 files+50 added−12 removed

modified:options_test.go+11 added
1package tea23import (4 "bytes"5 "context"6 "os"7 "sync/atomic"8 "testing"9)
41 unmodified lines
51 t.Errorf("expected filter to be set")52 }53 })5455 t.Run("external context", func(t *testing.T) {56 extCtx, extCancel := context.WithCancel(context.Background())57 defer extCancel()5859 p := NewProgram(nil, WithContext(extCtx))60 if p.externalCtx != extCtx || p.externalCtx == context.Background() {61 t.Errorf("expected passed in external context, got default (nil)")62 }63 })6465 t.Run("input options", func(t *testing.T) {66 exercise := func(t *testing.T, opt ProgramOption, expect inputType) {67 p := NewProgram(nil, opt)68 if p.inputType != expect {
75 unmodified lines
modified:options.go+1 added-1 removed
17 unmodified lines
18// useful if you want to cancel the execution from outside. When a Program gets19// cancelled it will exit with an error ErrProgramKilled.20func WithContext(ctx context.Context) ProgramOption {21 return func(p *Program) {22 p.ctx = ctx22 p.externalCtx = ctx23 }24}2526// WithOutput sets the output which, by default, is stdout. In most cases you
226 unmodified lines
modified:tea_test.go+17 added-2 removed
250 unmodified lines
251 }252 }253 }()254255 _, err := p.Run()256255 if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {257 if !errors.Is(err, ErrProgramKilled) {258 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)259 }260261 if errors.Is(err, context.Canceled) {262 // The end user should not know about the program's internal context state.263 // The program should only report external context cancellation as a context error.264 t.Fatalf("Internal context cancellation was reported as context error!")265 }266}267268func TestTeaContext(t *testing.T) {269 ctx, cancel := context.WithCancel(context.Background())
11 unmodified lines
281 }282 }283 }()284285 _, err := p.Run()286277 if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {287 if !errors.Is(err, ErrProgramKilled) {288 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)289 }290291 if !errors.Is(err, context.Canceled) {292 // The end user should know that their passed in context caused the kill.293 t.Fatalf("Expected %v, got %v", context.Canceled, err)294 }295}296297func TestTeaContextImplodeDeadlock(t *testing.T) {298 ctx, cancel := context.WithCancel(context.Background())
215 unmodified lines
modified:tea.go+21 added-9 removed
148 unmodified lines
149 startupTitle string150151 inputType inputType152153 // externalCtx is a context that was passed in via WithContext, otherwise defaulting154 // to ctx.Background() (in case it was not), the internal context is derived from it.155 externalCtx context.Context156157 // ctx is the programs's internal context for signalling internal teardown.158 // It is built and derived from the externalCtx in NewProgram().159 ctx context.Context160 cancel context.CancelFunc161162 msgs chan Msg
88 unmodified lines
251 }252253 // A context can be provided with a ProgramOption, but if none was provided254 // we'll use the default background context.249 if p.ctx == nil {250 p.ctx = context.Background()255 if p.externalCtx == nil {256 p.externalCtx = context.Background()257 }258 // Initialize context and teardown channel.253 p.ctx, p.cancel = context.WithCancel(p.ctx)259 p.ctx, p.cancel = context.WithCancel(p.externalCtx)260261 // if no output was set, set it to stdout262 if p.output == nil {263 p.output = os.Stdout
417 unmodified lines
681 if err == nil && len(p.errs) > 0 {682 err = <-p.errs // Drain a leftover error in case eventLoop crashed683 }684679 killed := p.ctx.Err() != nil || err != nil685 killed := p.externalCtx.Err() != nil || p.ctx.Err() != nil || err != nil686 if killed {681 if err == nil {682 err = fmt.Errorf("%w: %s", ErrProgramKilled, p.ctx.Err())687 if err == nil && p.externalCtx.Err() != nil {688 // Return also as context error the cancellation of an external context.689 // This is the context the user knows about and should be able to act on.690 err = fmt.Errorf("%w: %w", ErrProgramKilled, p.externalCtx.Err())691 } else if err == nil && p.ctx.Err() != nil {692 // Return only that the program was killed (not the internal mechanism).693 // The user does not know or need to care about the internal program context.694 err = ErrProgramKilled695 } else {696 // Return that the program was killed and also the error that caused it.697 err = fmt.Errorf("%w: %w", ErrProgramKilled, err)698 }686 }687688 if err == nil {699 } else {700 // Graceful shutdown of the program (not killed):701 // Ensure we rendered the final state of the model.702 p.renderer.write(model.View())703 }704
200 unmodified lines