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
Provenance
- Commit
1923181- Tree
6f5e3f7- Author
desertwitch <24509509+desertwitch@users.noreply.github.com>- Committer
desertwitch <24509509+desertwitch@users.noreply.github.com>- Parent
0f9cd5b
4 files changed+50 added−12 removed
modified:options_test.go+11 added
1 package tea2 3 import (4 "bytes"5+ "context"6 "os"7 "sync/atomic"8 "testing"9 )41 unmodified lines51 t.Errorf("expected filter to be set")52 }53 })54 55+ t.Run("external context", func(t *testing.T) {56+ extCtx, extCancel := context.WithCancel(context.Background())57+ defer extCancel()58+59+ 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+ })64+65 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 linesmodified:options.go+1 added-1 removed
17 unmodified lines18 // useful if you want to cancel the execution from outside. When a Program gets19 // cancelled it will exit with an error ErrProgramKilled.20 func WithContext(ctx context.Context) ProgramOption {21 return func(p *Program) {22- p.ctx = ctx22+ p.externalCtx = ctx23 }24 }25 26 // WithOutput sets the output which, by default, is stdout. In most cases you226 unmodified linesmodified:tea_test.go+17 added-2 removed
250 unmodified lines251 }252 }253 }()254 255+ _, err := p.Run()256+255- if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {257+ if !errors.Is(err, ErrProgramKilled) {258 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)259 }260+261+ 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 }267 268 func TestTeaContext(t *testing.T) {269 ctx, cancel := context.WithCancel(context.Background())11 unmodified lines281 }282 }283 }()284 285+ _, err := p.Run()286+277- if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {287+ if !errors.Is(err, ErrProgramKilled) {288 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)289 }290+291+ 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 }296 297 func TestTeaContextImplodeDeadlock(t *testing.T) {298 ctx, cancel := context.WithCancel(context.Background())215 unmodified linesmodified:tea.go+21 added-9 removed
148 unmodified lines149 startupTitle string150 151 inputType inputType152 153+ // 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.Context156+157+ // 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.CancelFunc161 162 msgs chan Msg88 unmodified lines251 }252 253 // 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)260 261 // if no output was set, set it to stdout262 if p.output == nil {263 p.output = os.Stdout417 unmodified lines681 if err == nil && len(p.errs) > 0 {682 err = <-p.errs // Drain a leftover error in case eventLoop crashed683 }684 679- 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- }687-688- 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