]> 127.0.0.1 Git - part/.git/commitdiff
1 (#49) v0.28.20250507170946
authorqydysky <qydysky@foxmail.com>
Wed, 7 May 2025 17:09:37 +0000 (01:09 +0800)
committerGitHub <noreply@github.com>
Wed, 7 May 2025 17:09:37 +0000 (01:09 +0800)
.github/workflows/test1.yml
funcCtrl/FuncCtrl.go
funcCtrl/FuncCtrl_test.go

index 1922382243cbfb04e5cef2ac20fc95ae3078c7dc..a32c866f1ef5418049a8ffee0ae23a292c702862 100644 (file)
@@ -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
index 24236ce9eed4a08cb3ab15890539b7b0925507a2..cb1c7d8e7b7ea40ea085065c922d4c69e6b499c6 100644 (file)
@@ -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
+                       }
+               }
+       }
+}
index 2894947baaa3b882406562b3a7d9ab8a0c75c996..767acc6290df3104b2b82b34a8b30e2c8c7768e1 100644 (file)
@@ -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