From: qydysky Date: Wed, 7 May 2025 17:09:37 +0000 (+0800) Subject: 1 (#49) X-Git-Tag: v0.28.20250507170946 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=5c5e84a7257194e99db1b46ed5f0e7c08b6c47d3;p=part%2F.git 1 (#49) --- diff --git a/.github/workflows/test1.yml b/.github/workflows/test1.yml index 1922382..a32c866 100644 --- a/.github/workflows/test1.yml +++ b/.github/workflows/test1.yml @@ -27,7 +27,7 @@ jobs: 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 15s -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 @@ -66,7 +66,7 @@ jobs: 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 15s -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 @@ -105,7 +105,7 @@ jobs: 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 15s -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 diff --git a/funcCtrl/FuncCtrl.go b/funcCtrl/FuncCtrl.go index 24236ce..cb1c7d8 100644 --- a/funcCtrl/FuncCtrl.go +++ b/funcCtrl/FuncCtrl.go @@ -2,6 +2,7 @@ package part import ( "context" + "iter" "sync" "sync/atomic" "unsafe" @@ -102,3 +103,31 @@ func (t *BlockFuncN) BlockAll() (unBlock func()) { } } } + +type RangeSource[T any] iter.Seq[T] + +func (i RangeSource[T]) RangeCtx(pctx context.Context) iter.Seq2[context.Context, T] { + return func(yield func(context.Context, T) bool) { + for o := range i { + ctx, cancle := context.WithCancel(pctx) + exit := !yield(ctx, o) + cancle() + if exit { + return + } + } + } +} + +func (i RangeSource[T]) RangeCtxCancel(pctx context.Context, cancle context.CancelFunc) iter.Seq2[context.Context, T] { + return func(yield func(context.Context, T) bool) { + for o := range i { + ctx, cancle := context.WithCancel(pctx) + exit := !yield(ctx, o) + cancle() + if exit { + return + } + } + } +} diff --git a/funcCtrl/FuncCtrl_test.go b/funcCtrl/FuncCtrl_test.go index 2894947..767acc6 100644 --- a/funcCtrl/FuncCtrl_test.go +++ b/funcCtrl/FuncCtrl_test.go @@ -1,10 +1,59 @@ package part import ( + "context" "testing" "time" ) +func Test_RangeCtx(t *testing.T) { + var rs RangeSource[int] = func(yield func(int) bool) { + for i := range 10 { + if !yield(i) { + return + } + } + } + + for ctx, val := range rs.RangeCtxCancel(context.WithTimeout(context.Background(), time.Millisecond*2500)) { + select { + case <-ctx.Done(): + if val < 2 { + t.Fatal() + } + case <-time.After(time.Second): + if val > 1 { + t.Fatal() + } + } + } +} + +func Test_RangeCtx2(t *testing.T) { + var i int + var rs RangeSource[any] = func(yield func(any) bool) { + for i < 10 { + if !yield(nil) { + return + } + } + } + + for ctx := range rs.RangeCtxCancel(context.WithTimeout(context.Background(), time.Millisecond*2500)) { + i++ + select { + case <-ctx.Done(): + if i < 2 { + t.Fatal() + } + case <-time.After(time.Second): + if i > 2 { + t.Fatal() + } + } + } +} + func Test_SkipFunc(t *testing.T) { var c = make(chan int, 2) var b SkipFunc