From: qydysky Date: Mon, 27 Jan 2025 04:33:00 +0000 (+0800) Subject: Dev (#17) X-Git-Tag: v0.28.20250127043311 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=5e4e75f9598091bc3bf8770fef5a76512ba9cefa;p=part%2F.git Dev (#17) * 1 * 1 --- diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7b0d59..1a33cd5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,123 +5,8 @@ on: branches: [ master ] jobs: - mac-test: - name: mac-test - runs-on: macos-latest - steps: - - - name: Set up Go 1.x - uses: actions/setup-go@v5 - with: - go-version: '1.23' - - - name: Check out code into the Go module directory - uses: actions/checkout@v4 - - - name: Test - run: | - go test -count 1 -timeout 30s -v . - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/signal - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/log - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/reqf - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/limit - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/file - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/pool - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/funcCtrl - go test -count 1 -timeout 50s -v -race github.com/qydysky/part/msgq - go test -count 10 -race -timeout 10s -run ^Test_3$ github.com/qydysky/part/msgq - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sync - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/web - go test -count 1 -timeout 10s -v -run "Test_Client" -race github.com/qydysky/part/websocket - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx - go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto - - w-test: - name: w-test - runs-on: windows-latest - steps: - - - name: Set up Go 1.x - uses: actions/setup-go@v5 - with: - go-version: '1.23' - - - name: Check out code into the Go module directory - uses: actions/checkout@v4 - - - name: Test - run: | - go test -count 1 -timeout 30s -v . - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/signal - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/log - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/reqf - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/limit - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/file - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/pool - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/funcCtrl - go test -count 1 -timeout 50s -v -race github.com/qydysky/part/msgq - go test -count 10 -race -timeout 10s -run ^Test_3$ github.com/qydysky/part/msgq - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sync - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/web - go test -count 1 -timeout 10s -v -run "Test_Client" -race github.com/qydysky/part/websocket - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx - go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto - - u-test: - name: u-test - runs-on: ubuntu-latest - steps: - - - name: Set up Go 1.x - uses: actions/setup-go@v5 - with: - go-version: '1.23' - - - name: Check out code into the Go module directory - uses: actions/checkout@v4 - - - name: Test - run: | - go test -count 1 -timeout 30s -v . - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/signal - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/log - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/reqf - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/limit - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/file - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/pool - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/funcCtrl - go test -count 1 -timeout 50s -v -race github.com/qydysky/part/msgq - go test -count 10 -race -timeout 10s -run ^Test_3$ github.com/qydysky/part/msgq - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sync - go test -count 1 -timeout 20s -v -race github.com/qydysky/part/web - go test -count 1 -timeout 10s -v -run "Test_Client" -race github.com/qydysky/part/websocket - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql - go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx - go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors - go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto - test: name: test - needs: [u-test,w-test,mac-test] runs-on: ubuntu-latest steps: - name: Set Release Name diff --git a/.github/workflows/test1.yml b/.github/workflows/test1.yml index 59a2861..5bd49c2 100644 --- a/.github/workflows/test1.yml +++ b/.github/workflows/test1.yml @@ -37,7 +37,7 @@ jobs: go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx + go test -count 1 -timeout 20s -v -race github.com/qydysky/part/ctx go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors @@ -75,7 +75,7 @@ jobs: go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx + go test -count 1 -timeout 20s -v -race github.com/qydysky/part/ctx go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors @@ -113,7 +113,7 @@ jobs: go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2 - go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx + go test -count 1 -timeout 20s -v -race github.com/qydysky/part/ctx go test -count 1 -timeout 7s -v -race github.com/qydysky/part/slice go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors diff --git a/ctx/Ctx.go b/ctx/Ctx.go index 4f566c2..62c056c 100644 --- a/ctx/Ctx.go +++ b/ctx/Ctx.go @@ -4,6 +4,7 @@ import ( "context" "errors" "runtime" + "sync" "sync/atomic" "time" ) @@ -85,17 +86,17 @@ func WithWait(sctx context.Context, planNum int32, to ...time.Duration) (dctx co // or // use as a normal context.WithCancel(ctx) func WaitCtx(ctx context.Context) (dctx context.Context, done func()) { - ctx1, done1 := context.WithCancel(ctx) - if ctxp, ok := ctx1.Value(ptr).(*Ctx); ok { + dctx1, done1 := context.WithCancel(ctx) + if ctxp, ok := dctx1.Value(ptr).(*Ctx); ok { ctxp.r32.Add(1) ctxp.w32.Add(-1) } - return ctx1, func() { + return dctx1, sync.OnceFunc(func() { done1() - if ctxp, ok := ctx1.Value(ptr).(*Ctx); ok { + if ctxp, ok := dctx1.Value(ptr).(*Ctx); ok { ctxp.r32.Add(-1) } - } + }) } func Done(ctx context.Context) bool { diff --git a/ctx/Ctx_test.go b/ctx/Ctx_test.go index 0a33ff4..f75a775 100644 --- a/ctx/Ctx_test.go +++ b/ctx/Ctx_test.go @@ -54,6 +54,36 @@ func TestMain5(t *testing.T) { } } +func TestMain6(t *testing.T) { + ctx1, done := WithWait(context.Background(), 1, time.Second*2) + go func() { + ctx2, done1 := WaitCtx(ctx1) + go func() { + time.Sleep(time.Millisecond * 500) + done1() + }() + t1 := time.Now() + <-ctx2.Done() + done1() + if time.Since(t1)-time.Millisecond*500 > time.Millisecond*100 { + t.Fatal() + } + + ctx3, done2 := WaitCtx(ctx1) + defer done2() + <-ctx3.Done() + time.Sleep(time.Second) + }() + time.Sleep(time.Second) + t1 := time.Now() + if done() != nil { + t.Fatal() + } + if time.Since(t1) < time.Second { + t.Fatal() + } +} + func TestMain1(t *testing.T) { ctx1, done := WithWait(context.Background(), 1, time.Second) t0 := time.Now()