43 lines
893 B
Go
43 lines
893 B
Go
// Package demo ...
|
|
//
|
|
// Description : demo ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-12-02 12:46 下午
|
|
package demo
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestCancelCtx_UDC 测试自定义context
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 12:46 下午 2021/12/2
|
|
func TestCancelCtx_UDC(t *testing.T) {
|
|
childCancel := true
|
|
|
|
parentCtx, parentFunc := WithCancel(Background())
|
|
mctx := MyContext{parentCtx}
|
|
|
|
childCtx, childFun := WithCancel(mctx)
|
|
|
|
if childCancel {
|
|
childFun()
|
|
} else {
|
|
parentFunc()
|
|
}
|
|
|
|
fmt.Println("parent context => ", parentCtx, reflect.TypeOf(parentCtx).String())
|
|
fmt.Println("my context => ", mctx, reflect.TypeOf(mctx).String())
|
|
fmt.Println("child context => ", childCtx, reflect.TypeOf(childCtx).String())
|
|
|
|
// 防止主协程退出太快,子协程来不及打印
|
|
time.Sleep(5 * time.Second)
|
|
}
|