最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Go語言context?test源碼分析詳情

 更新時間:2022年02月09日 09:20:10   作者:zwf193071?  
這篇文章主要介紹了Go語言context?test源碼分析詳情,關(guān)于context?test,測試對象是context包,測試包的包名是context_test,下面將對context?test源碼進(jìn)行分析,需要的朋友可以參考一下,希望對你有所幫助

1.測試?yán)臃治?/h2>

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
}

結(jié)構(gòu)分析,gen是一個函數(shù),返回值是一個信道, for range channel是有特殊意義的, for會循環(huán)從channel讀數(shù)據(jù),直到channel被close(),不然就是無限循環(huán).

gen內(nèi)部的協(xié)程就是典型的閉包,for range會不斷觸發(fā)讀,gen內(nèi)部的for select 會不斷觸發(fā)寫,主協(xié)程讀5次之后,會結(jié)束main函數(shù),會觸發(fā)defer函數(shù), 也就是取消操作對應(yīng)的回調(diào),此時done信道會被close,gen內(nèi)部的協(xié)程會正常退出.

這個例子是測試支持取消信號的上下文,取消函數(shù)的調(diào)用放在了maindefer函數(shù)中.

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的這個例子,在maindefer中也有主動調(diào)用取消函數(shù)的. 實際上通過打印可以顯示deadline是否按預(yù)期工作.

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.WithValueContext.Value()是存取操作, 取的時候,如果key沒找到,會返回nil.

2.單元測試

context_text.go,x_test.go是單元測試, example_test.go是示例,benchmark_test.go是基準(zhǔn)測試, 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) {}

這暴露的大多測試函數(shù)的參數(shù)類型是testingT接口類型,但這個源文件中沒有實現(xiàn)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的內(nèi)容,直接是用testing.T類型來實現(xiàn)testingT接口.

那先分析一下testing.T對testingT接口的實現(xiàn).

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的實現(xiàn)類型, Deadline()返回了t.context中存儲的deadline信息.

testing.T內(nèi)嵌了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實現(xiàn),某個測試用例多次重復(fù)執(zhí)行時, 可啟用并發(fā)參數(shù).

到此這篇關(guān)于Go語言context test源碼分析詳情的文章就介紹到這了,更多相關(guān)Go語言context test源碼分析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang map與sync.map的異同詳解

    Golang map與sync.map的異同詳解

    在Go語言中,map和sync.Map都是用于存儲鍵值對的數(shù)據(jù)結(jié)構(gòu),但它們在并發(fā)安全性、性能和使用場景上存在顯著差異,接下來將深入探討這兩種數(shù)據(jù)結(jié)構(gòu)的異同,感興趣的朋友可以參考下
    2024-01-01
  • Go語言操作mysql數(shù)據(jù)庫簡單例子

    Go語言操作mysql數(shù)據(jù)庫簡單例子

    這篇文章主要介紹了Go語言操作mysql數(shù)據(jù)庫簡單例子,本文包含插入數(shù)據(jù)和查詢代碼實例,需要的朋友可以參考下
    2014-10-10
  • Go高級特性探究之HTTP錯誤處理詳解

    Go高級特性探究之HTTP錯誤處理詳解

    在Web應(yīng)用程序中,HTTP錯誤處理是非常重要的,它關(guān)系到Web應(yīng)用程序的穩(wěn)定性和可靠性,本文介紹如何在Go項目中處理HTTP錯誤,并提供相應(yīng)的解決方案和實踐經(jīng)驗,希望對Go語言Web應(yīng)用程序的開發(fā)者有所幫助
    2023-06-06
  • go-cqhttp智能聊天功能的實現(xiàn)

    go-cqhttp智能聊天功能的實現(xiàn)

    這篇文章主要介紹了go-cqhttp智能聊天功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • golang通過反射設(shè)置結(jié)構(gòu)體變量的值

    golang通過反射設(shè)置結(jié)構(gòu)體變量的值

    這篇文章主要介紹了golang通過反射設(shè)置結(jié)構(gòu)體變量的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Golang使用lua腳本實現(xiàn)redis原子操作

    Golang使用lua腳本實現(xiàn)redis原子操作

    這篇文章主要介紹了Golang使用lua腳本實現(xiàn)redis原子操作,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 詳解在Go語言單元測試中如何解決文件依賴問題

    詳解在Go語言單元測試中如何解決文件依賴問題

    現(xiàn)如今的?Web?應(yīng)用程序往往采用?RESTful?API?接口形式對外提供服務(wù),后端接口直接向前端返回?HTML?文件的情況越來越少,所以在程序中操作文件的場景也變少了,在編寫單元測試時,文件就成了被測試代碼的外部依賴,本文就來講解下測試過程中如何解決文件外部依賴問題
    2023-08-08
  • Golang利用channel協(xié)調(diào)協(xié)程的方法詳解

    Golang利用channel協(xié)調(diào)協(xié)程的方法詳解

    go?當(dāng)中的并發(fā)編程是通過goroutine來實現(xiàn)的,利用channel(管道)可以在協(xié)程之間傳遞數(shù)據(jù),所以本文就來講講Golang如何利用channel協(xié)調(diào)協(xié)程吧
    2023-05-05
  • Golang 在gin框架中如何使用JWT鑒權(quán)

    Golang 在gin框架中如何使用JWT鑒權(quán)

    JWT,全稱 JSON Web Token,是一種開放標(biāo)準(zhǔn)(RFC 7519),用于安全地在雙方之間傳遞信息,這篇文章主要介紹了golang 在Gin框架中使用JWT鑒權(quán),需要的朋友可以參考下
    2024-07-07
  • Go語言基礎(chǔ)類型及常量用法示例詳解

    Go語言基礎(chǔ)類型及常量用法示例詳解

    這篇文章主要為大家介紹了Go語言基礎(chǔ)類型及常量的用法及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11

最新評論

怀安县| 余姚市| 房产| 光泽县| 梧州市| 扶绥县| 北海市| 城步| 电白县| 曲麻莱县| 永胜县| 达州市| 敦化市| 大同县| 横峰县| 桂平市| 呼玛县| 松潘县| 旬邑县| 外汇| 大庆市| 湖口县| 丰县| 抚宁县| 外汇| 镇康县| 马山县| 林周县| 库车县| 阳原县| 赤壁市| 莲花县| 稻城县| 三穗县| 威远县| 潮安县| 北碚区| 湖州市| 乌苏市| 蓝山县| 罗平县|