From: qydysky Date: Wed, 13 Sep 2023 22:21:37 +0000 (+0800) Subject: add X-Git-Tag: v0.28.0+202309137937674 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=7937674d3f171a22372d6333eb8a87fb3d758976;p=part%2F.git add --- diff --git a/ctx/Ctx.go b/ctx/Ctx.go index 65aaa80..2edafcd 100644 --- a/ctx/Ctx.go +++ b/ctx/Ctx.go @@ -41,7 +41,7 @@ func WithWait(sctx context.Context, to ...time.Duration) (dctx context.Context, done = func() error { <-ctx.Ctx.Done() if ctxp, ok := sctx.Value(ptr).(*Ctx); ok { - defer ctxp.i32.Add(-1) + defer func() { ctxp.i32.Add(-1) }() } be := time.Now() for !ctx.i32.CompareAndSwap(0, -1) { diff --git a/ctx/Ctx_test.go b/ctx/Ctx_test.go index f510244..f4ae1d8 100644 --- a/ctx/Ctx_test.go +++ b/ctx/Ctx_test.go @@ -68,6 +68,27 @@ func TestMain4(t *testing.T) { }() time.Sleep(time.Second) if e := done(); !errors.Is(e, ErrWaitTo) { - t.Fail() + t.Fatal(e) + } +} + +func TestMain5(t *testing.T) { + ctx, can := context.WithTimeout(context.Background(), time.Millisecond*500) + defer can() + + ctx, done := WithWait(ctx) + + var gr = func(ctx context.Context, to time.Duration) { + done := Wait(ctx) + defer done() + time.Sleep(to) } + + bg := time.Now() + + go gr(ctx, 0) + // go gr(ctx, time.Second) + // go gr(ctx, time.Second*5) + + t.Log(done(), time.Since(bg)) }