網站首頁 編程語言 正文
1.測試例子分析
example_test.go,展示了With-系列的4個例子
func ExampleWithCancel() { ? gen := func(ctx context.Context) <-chan int { ? ? dst := make(chan int) ? ? n := 1 ? ? go func() { ? ? ? for { ? ? ? ? select { ? ? ? ? case <-ctx.Done(): ? ? ? ? ? return // returning not to leak the goroutine ? ? ? ? case dst <- n: ? ? ? ? ? n++ ? ? ? ? } ? ? ? } ? ? }() ? ? return dst ? } ? ctx, cancel := context.WithCancel(context.Background()) ? defer cancel() // cancel when we are finished consuming integers ? for n := range gen(ctx) { ? ? fmt.Println(n) ? ? if n == 5 { ? ? ? break ? ? } ? } ? // Output: ? // 1 ? // 2 ? // 3 ? // 4 ? // 5 }
結構分析,gen
是一個函數,返回值是一個信道, for range channel
是有特殊意義的, for會循環從channel讀數據,直到channel被close(),不然就是無限循環.
gen內部的協程就是典型的閉包,for range會不斷觸發讀,gen內部的for select 會不斷觸發寫,主協程讀5次之后,會結束main函數,會觸發defer函數, 也就是取消操作對應的回調,此時done信道會被close,gen內部的協程會正常退出.
這個例子是測試支持取消信號的上下文,取消函數的調用放在了main
的defer
函數中.
const shortDuration = 1 * time.Millisecond func ExampleWithDeadline() { ? d := time.Now().Add(shortDuration) ? ctx, cancel := context.WithDeadline(context.Background(), d) ? // Even though ctx will be expired, it is good practice to call its ? // cancellation function in any case. Failure to do so may keep the ? // context and its parent alive longer than necessary. ? defer cancel() ? select { ? case <-time.After(1 * time.Second): ? ? fmt.Println("overslept") ? case <-ctx.Done(): ? ? fmt.Println(ctx.Err()) ? } ? // Output: ? // context deadline exceeded }
deadline
的這個例子,在main
的defer
中也有主動調用取消函數的. 實際上通過打印可以顯示deadline是否按預期工作.
func ExampleWithTimeout() { ? ctx, cancel := context.WithTimeout(context.Background(), shortDuration) ? defer cancel() ? select { ? case <-time.After(1 * time.Second): ? ? fmt.Println("overslept") ? case <-ctx.Done(): ? ? fmt.Println(ctx.Err()) // prints "context deadline exceeded" ? } ? // Output: ? // context deadline exceeded }
timeout
只是deadline
的一種簡寫.
func ExampleWithValue() { ? type favContextKey string ? f := func(ctx context.Context, k favContextKey) { ? ? if v := ctx.Value(k); v != nil { ? ? ? fmt.Println("found value:", v) ? ? ? return ? ? } ? ? fmt.Println("key not found:", k) ? } ? k := favContextKey("language") ? ctx := context.WithValue(context.Background(), k, "Go") ? f(ctx, k) ? f(ctx, favContextKey("color")) ? // Output: ? // found value: Go ? // key not found: color }
context.WithValue
和Context.Value()
是存取操作, 取的時候,如果key沒找到,會返回nil.
2.單元測試
context_text.go,x_test.go
是單元測試, example_test.go
是示例,benchmark_test.go是基準測試, net_test.go展示了deadline對net包的支持.
先看單元測試的context_text.go.
type testingT interface {} type otherContext struct {} func quiescent(t testingT) time.Duration {} func XTestBackground(t testingT) {} func XTestTODO(t testingT) {} func XTestWithCancel(t testingT) {} func contains(m map[canceler]struct{}, key canceler) bool {} func XTestParentFinishesChild(t testingT) {} func XTestChildFinishesFirst(t testingT) {} func testDeadline(c Context, name string, t testingT) {} func XTestDeadline(t testingT) {} func XTestTimeout(t testingT) {} func XTestCanceledTimeout(t testingT) {} func XTestValues(t testingT) {} func XTestAllocs(t testingT, testingShort func() bool, testingAllocsPerRun func(int, func()) float64) {} func XTestSimultaneousCancels(t testingT) {} func XTestInterlockedCancels(t testingT) {} func XTestLayersCancel(t testingT) {} func XTestLayersTimeout(t testingT) {} func XTestCancelRemoves(t testingT) {} func XTestWithCancelCanceledParent(t testingT) {} func XTestWithValueChecksKey(t testingT) {} func XTestInvalidDerivedFail(t testingT) {} func recoveredValue(fn func()) (v interface{}) {} func XTestDeadlineExceededSupportsTimeout(t testingT) {} type myCtx struct {} type myDoneCtx struct {} func (d *myDoneCtx) Done() <-chan struct{} {} func XTestCustomContextGoroutines(t testingT) {}
這暴露的大多測試函數的參數類型是testingT接口類型,但這個源文件中沒有實現testingT
接口的,
func TestBackground(t *testing.T) ? ? ? ? ? ? ? ? ? ? ?{ XTestBackground(t) } func TestTODO(t *testing.T) ? ? ? ? ? ? ? ? ? ? ? ? ? ?{ XTestTODO(t) } func TestWithCancel(t *testing.T) ? ? ? ? ? ? ? ? ? ? ?{ XTestWithCancel(t) } func TestParentFinishesChild(t *testing.T) ? ? ? ? ? ? { XTestParentFinishesChild(t) } func TestChildFinishesFirst(t *testing.T) ? ? ? ? ? ? ?{ XTestChildFinishesFirst(t) } func TestDeadline(t *testing.T) ? ? ? ? ? ? ? ? ? ? ? ?{ XTestDeadline(t) } func TestTimeout(t *testing.T) ? ? ? ? ? ? ? ? ? ? ? ? { XTestTimeout(t) } func TestCanceledTimeout(t *testing.T) ? ? ? ? ? ? ? ? { XTestCanceledTimeout(t) } func TestValues(t *testing.T) ? ? ? ? ? ? ? ? ? ? ? ? ?{ XTestValues(t) } func TestAllocs(t *testing.T) ? ? ? ? ? ? ? ? ? ? ? ? ?{ XTestAllocs(t, testing.Short, testing.AllocsPerRun) } func TestSimultaneousCancels(t *testing.T) ? ? ? ? ? ? { XTestSimultaneousCancels(t) } func TestInterlockedCancels(t *testing.T) ? ? ? ? ? ? ?{ XTestInterlockedCancels(t) } func TestLayersCancel(t *testing.T) ? ? ? ? ? ? ? ? ? ?{ XTestLayersCancel(t) } func TestLayersTimeout(t *testing.T) ? ? ? ? ? ? ? ? ? { XTestLayersTimeout(t) } func TestCancelRemoves(t *testing.T) ? ? ? ? ? ? ? ? ? { XTestCancelRemoves(t) } func TestWithCancelCanceledParent(t *testing.T) ? ? ? ?{ XTestWithCancelCanceledParent(t) } func TestWithValueChecksKey(t *testing.T) ? ? ? ? ? ? ?{ XTestWithValueChecksKey(t) } func TestInvalidDerivedFail(t *testing.T) ? ? ? ? ? ? ?{ XTestInvalidDerivedFail(t) } func TestDeadlineExceededSupportsTimeout(t *testing.T) { XTestDeadlineExceededSupportsTimeout(t) } func TestCustomContextGoroutines(t *testing.T) ? ? ? ? { XTestCustomContextGoroutines(t) }
這是x_test.go
的內容,直接是用testing.T
類型來實現testingT接口.
那先分析一下testing.T對testingT接口的實現.
type T struct { ? common ? isParallel bool ? context ? ?*testContext } func (t *T) Deadline() (deadline time.Time, ok bool) { ? deadline = t.context.deadline ? return deadline, !deadline.IsZero() }
注意:testing.T.context不是context.Context的實現類型, Deadline()返回了t.context中存儲的deadline信息.
testing.T內嵌了testing.common,大部分方法集都來至common:
Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Failed() bool Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Helper() Log(args ...interface{}) Logf(format string, args ...interface{}) Name() string Skip(args ...interface{}) SkipNow() Skipf(format string, args ...interface{}) Skipped() bool
Parallel()
是由testing.T
實現,某個測試用例多次重復執行時, 可啟用并發參數.
原文鏈接:https://juejin.cn/post/7062246261667135518
相關推薦
- 2022-03-21 C語言楊輝三角兩種實現方法_C 語言
- 2022-03-17 正確使用dotnet-*工具的方法_實用技巧
- 2022-09-26 使用JDBC連接數據庫執行sql語句,創建數據庫連接池
- 2022-05-24 C#?JWT權限驗證的實現_C#教程
- 2023-01-01 解決React報錯No?duplicate?props?allowed_React
- 2022-04-08 WPF基本控件介紹_基礎應用
- 2022-04-04 Python數據處理-導入導出excel數據_python
- 2022-12-05 Python中WebService客戶端接口調用及身份驗證的問題_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支