]> 127.0.0.1 Git - part/.git/commitdiff
1 (#32) v0.28.20250302064654
authorqydysky <qydysky@foxmail.com>
Sun, 2 Mar 2025 06:46:44 +0000 (14:46 +0800)
committerGitHub <noreply@github.com>
Sun, 2 Mar 2025 06:46:44 +0000 (14:46 +0800)
ctx/Ctx.go
ctx/Ctx_test.go

index 361c44c3f88bd96cbf600201870e46c335075d52..7dd15d99287a6b3cf167adb8d1a7d4b6ce13d783 100644 (file)
@@ -54,9 +54,7 @@ func WithWait(sctx context.Context, planNum int32, to ...time.Duration) (dctx co
 
                doneWait()
                if ctxp, ok := sctx.Value(ptr).(*Ctx); ok {
-                       defer func() {
-                               ctxp.r32.Add(-1)
-                       }()
+                       defer ctxp.r32.Add(-1)
                }
                if planNum == 0 && ctx.w32.Load() == 0 {
                        return ErrNothingWait
@@ -66,6 +64,7 @@ func WithWait(sctx context.Context, planNum int32, to ...time.Duration) (dctx co
                        if len(to) > 0 && time.Since(be) > to[0] {
                                return ErrWaitTo
                        }
+                       time.Sleep(time.Millisecond * 100)
                        runtime.Gosched()
                }
                for !ctx.r32.CompareAndSwap(0, -1) {
index f15c07f46dcac48d812fcec761937b396aee74c9..5a83c1b797531c77fc0ecc64a087e479fe46239d 100644 (file)
@@ -7,6 +7,66 @@ import (
        "time"
 )
 
+func TestMain12(t *testing.T) {
+       ctx1, done1 := WithWait(context.Background(), 0, time.Second*2)
+       ctx2, done2 := WithWait(ctx1, 0, time.Second*2)
+       go func() {
+               ctx3, done3 := WaitCtx(ctx2)
+               defer done3()
+               <-ctx3.Done()
+       }()
+       time.Sleep(time.Second)
+       if e := done2(); e != nil {
+               t.Fatal(e)
+       }
+       if e := done1(); e != nil {
+               t.Fatal(e)
+       }
+}
+
+func TestMain11(t *testing.T) {
+       ctx1, done1 := WithWait(context.Background(), 0, time.Second*2)
+       ctx2, _ := WithWait(ctx1, 0, time.Second*2)
+       go func() {
+               ctx3, done3 := WaitCtx(ctx2)
+               defer done3()
+               <-ctx3.Done()
+       }()
+       time.Sleep(time.Second)
+       if e := done1(); e == nil {
+               t.Fatal(e)
+       }
+}
+
+func TestMain10(t *testing.T) {
+       ctx1, done := WithWait(context.Background(), 0, time.Second*2)
+       go func() {
+               ctx2, done2 := WaitCtx(ctx1)
+               _, done3 := WaitCtx(ctx2)
+               defer done2()
+               defer done3()
+               <-ctx2.Done()
+       }()
+       time.Sleep(time.Second)
+       if e := done(); e != nil {
+               t.Fatal(e)
+       }
+}
+
+func TestMain9(t *testing.T) {
+       ctx1, done := WithWait(context.Background(), 0, time.Second*2)
+       go func() {
+               ctx2, done2 := WaitCtx(ctx1)
+               _, _ = WaitCtx(ctx2)
+               defer done2()
+               <-ctx2.Done()
+       }()
+       time.Sleep(time.Second)
+       if e := done(); e == nil {
+               t.Fatal(e)
+       }
+}
+
 func TestMain(t *testing.T) {
        ctx1, done := WithWait(context.Background(), 1, time.Second*2)
        t0 := time.Now()