From 7937674d3f171a22372d6333eb8a87fb3d758976 Mon Sep 17 00:00:00 2001 From: qydysky Date: Thu, 14 Sep 2023 06:21:37 +0800 Subject: [PATCH] add --- ctx/Ctx.go | 2 +- ctx/Ctx_test.go | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) 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)) } -- 2.39.2