From fbad5b22de0cf3194f9ddcdb273dfd23e3112d40 Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 27 Jan 2025 10:53:55 +0800 Subject: [PATCH] 1 (#16) --- ctx/Ctx.go | 11 ++++++++--- ctx/Ctx_test.go | 32 +++++++++++++++++++++++++++++++- util/util.go | 15 +++++++++++---- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/ctx/Ctx.go b/ctx/Ctx.go index c47703f..4f566c2 100644 --- a/ctx/Ctx.go +++ b/ctx/Ctx.go @@ -81,13 +81,18 @@ func WithWait(sctx context.Context, planNum int32, to ...time.Duration) (dctx co // do something.. // <-ctx1.Done() // wait mainDone call // }() +// +// or +// use as a normal context.WithCancel(ctx) func WaitCtx(ctx context.Context) (dctx context.Context, done func()) { - if ctxp, ok := ctx.Value(ptr).(*Ctx); ok { + ctx1, done1 := context.WithCancel(ctx) + if ctxp, ok := ctx1.Value(ptr).(*Ctx); ok { ctxp.r32.Add(1) ctxp.w32.Add(-1) } - return ctx, func() { - if ctxp, ok := ctx.Value(ptr).(*Ctx); ok { + return ctx1, func() { + done1() + if ctxp, ok := ctx1.Value(ptr).(*Ctx); ok { ctxp.r32.Add(-1) } } diff --git a/ctx/Ctx_test.go b/ctx/Ctx_test.go index 4c9a47a..0a33ff4 100644 --- a/ctx/Ctx_test.go +++ b/ctx/Ctx_test.go @@ -8,7 +8,7 @@ import ( ) func TestMain(t *testing.T) { - ctx1, done := WithWait(context.Background(), 1, time.Second) + ctx1, done := WithWait(context.Background(), 1, time.Second*2) t0 := time.Now() go func() { ctx2, done1 := WaitCtx(ctx1) @@ -17,11 +17,41 @@ func TestMain(t *testing.T) { if time.Since(t0) < time.Millisecond*100 { t.Fail() } + time.Sleep(time.Second) + }() + time.Sleep(time.Second) + t1 := time.Now() + if done() != nil { + t.Fatal() + } + if time.Since(t1) < time.Second { + t.Fail() + } +} + +func TestMain5(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() + if time.Since(t1)-time.Millisecond*500 > time.Millisecond*100 { + t.Fatal() + } + 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) { diff --git a/util/util.go b/util/util.go index a432f08..056c637 100644 --- a/util/util.go +++ b/util/util.go @@ -2,21 +2,28 @@ package part import ( "reflect" + "time" ) //语法糖 -//数组切片,重新分配内存 +// 数组切片,重新分配内存 // i := []int{0,1,2} // b := SliceCut(i[:1]).([]int) func SliceCopy(src interface{}) (des interface{}) { srcV := reflect.ValueOf(src) - if sk := srcV.Kind();sk != reflect.Slice && sk != reflect.Array { - panic(&reflect.ValueError{Method:"reflect.Copy", Kind:sk}) + if sk := srcV.Kind(); sk != reflect.Slice && sk != reflect.Array { + panic(&reflect.ValueError{Method: "reflect.Copy", Kind: sk}) } - desV := reflect.MakeSlice(srcV.Type(),srcV.Len(),srcV.Len()) + desV := reflect.MakeSlice(srcV.Type(), srcV.Len(), srcV.Len()) reflect.Copy(desV, srcV) des = desV.Interface() return } +func Callback(f func(startT time.Time, args ...any)) func(args ...any) { + now := time.Now() + return func(args ...any) { + f(now, args...) + } +} -- 2.39.2