Add WithContext function for cancellation

This commit is contained in:
Dan Jones 2025-11-14 14:53:36 -06:00
commit 09feb51213
2 changed files with 43 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package waiterr_test
import (
"context"
"errors"
"testing"
"testing/synctest"
@ -114,3 +115,29 @@ func TestUnwrap(tt *testing.T) {
be.Equal(t, weNoErr.Unwrap(), nil)
})
}
func TestWithContext(tt *testing.T) {
tt.Run("with error", func(tt2 *testing.T) {
er1 := errors.New("uh-oh")
er2 := errors.New("oops")
synctest.Test(tt2, func(t *testing.T) {
we, ctx := waiterr.WithContext(t.Context())
we.Go(func() error { return er1 })
synctest.Wait() // Ensure it finishes first
we.Go(func() error { return er2 })
er := context.Cause(ctx)
be.Err(t, er, er1)
be.True(t, !errors.Is(er, er2))
})
})
tt.Run("no error", func(t *testing.T) {
we, ctx := waiterr.WithContext(t.Context())
we.Go(func() error { return nil })
we.Go(func() error { return nil })
er := we.Wait()
be.Err(t, er, nil)
be.Err(t, context.Cause(ctx), context.Canceled)
})
}