return false
}
+// errCtx := pctx.Value[error]{}
+//
+// cancelC = errCtx.LinkCtx(cancelC)
+//
+// pctx.PutVal(cancelC, &errCtx, fmt.Errorf("%vs未接收到有效数据", readTO))
+//
+// err := errCtx.Get()
type Value[T any] struct {
data T
}
pt.Set(v)
}
}
+
+var (
+ selfCancel = "selfCancel"
+ ErrNotCarryYet = errors.New("ErrNotCarryYet")
+)
+
+func CarryCancel(ctx context.Context, cancelFunc context.CancelFunc) context.Context {
+ return context.WithValue(ctx, &selfCancel, cancelFunc)
+}
+
+func CallCancel(ctx context.Context) error {
+ if pt, ok := ctx.Value(&selfCancel).(context.CancelFunc); ok {
+ pt()
+ } else {
+ return ErrNotCarryYet
+ }
+ return nil
+}
t.Fatal()
}
}
+
+func TestMain4(t *testing.T) {
+ ctx := CarryCancel(context.WithCancel(context.Background()))
+ time.AfterFunc(time.Millisecond*500, func() {
+ if CallCancel(ctx) != nil {
+ t.Fail()
+ }
+ })
+ n := time.Now()
+ <-ctx.Done()
+ if time.Since(n) < time.Millisecond*500 {
+ t.Fail()
+ }
+}