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
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
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
import (
"context"
+ "iter"
"sync"
"sync/atomic"
"unsafe"
}
}
}
+
+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
+ }
+ }
+ }
+}
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