ptr = &struct{}{}
ErrWaitTo = errors.New("ErrWaitTo")
ErrNothingWait = errors.New("ErrNothingWait")
+ ErrDoneCalled = errors.New("ErrDoneCalled")
)
type Ctx struct {
var doneWait context.CancelFunc
dctx, doneWait = context.WithCancel(ctx.Ctx)
+
+ var oncef atomic.Bool
done = func() error {
+ if !oncef.CompareAndSwap(false, true) {
+ return ErrDoneCalled
+ }
+
doneWait()
if ctxp, ok := sctx.Value(ptr).(*Ctx); ok {
defer func() {
}
func TestMain5(t *testing.T) {
+ ctx1, done := WithWait(context.Background(), 1)
+ t0 := time.Now()
+ go func() {
+ ctx2, done1 := WaitCtx(ctx1)
+ defer done1()
+ <-ctx2.Done()
+ if time.Since(t0) < time.Millisecond*100 {
+ t.Fail()
+ }
+ time.Sleep(time.Second)
+ }()
+ time.Sleep(time.Second)
+ t1 := time.Now()
+ if done() != nil {
+ t.Fatal()
+ }
+ if time.Since(t1) < time.Second {
+ t.Fail()
+ }
+ if !errors.Is(done(), ErrDoneCalled) {
+ t.Fatal()
+ }
+}
+
+func TestMain7(t *testing.T) {
ctx1, done := WithWait(context.Background(), 1, time.Second*2)
go func() {
ctx2, done1 := WaitCtx(ctx1)