]> 127.0.0.1 Git - part/.git/commitdiff
add v0.28.0+202309137937674
authorqydysky <qydysky@foxmail.com>
Wed, 13 Sep 2023 22:21:37 +0000 (06:21 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 13 Sep 2023 22:21:37 +0000 (06:21 +0800)
ctx/Ctx.go
ctx/Ctx_test.go

index 65aaa80271f764b33cdca1b76db6971043e9bb51..2edafcd93fd424a91ef03182cb2b2c04e4b75b1b 100644 (file)
@@ -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) {
index f51024412315c7a68bd74599f7f6343724a95b9d..f4ae1d86bca25f68eeab741d8e7f2d83053e4a17 100644 (file)
@@ -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))
 }