欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go語(yǔ)言context?test源碼分析詳情

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

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

example_test.go,展示了With-系列的4個(gè)例子

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是一個(gè)函數(shù),返回值是一個(gè)信道, for range channel是有特殊意義的, for會(huì)循環(huán)從channel讀數(shù)據(jù),直到channel被close(),不然就是無(wú)限循環(huán).

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

這個(gè)例子是測(cè)試支持取消信號(hào)的上下文,取消函數(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的這個(gè)例子,在maindefer中也有主動(dòng)調(diào)用取消函數(shù)的. 實(shí)際上通過(guò)打印可以顯示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的一種簡(jiǎn)寫(xiě).

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()是存取操作, 取的時(shí)候,如果key沒(méi)找到,會(huì)返回nil.

2.單元測(cè)試

context_text.go,x_test.go是單元測(cè)試, example_test.go是示例,benchmark_test.go是基準(zhǔn)測(cè)試, net_test.go展示了deadline對(duì)net包的支持.

先看單元測(cè)試的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) {}

這暴露的大多測(cè)試函數(shù)的參數(shù)類型是testingT接口類型,但這個(gè)源文件中沒(méi)有實(shí)現(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類型來(lái)實(shí)現(xiàn)testingT接口.

那先分析一下testing.T對(duì)testingT接口的實(shí)現(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的實(shí)現(xiàn)類型, Deadline()返回了t.context中存儲(chǔ)的deadline信息.

testing.T內(nèi)嵌了testing.common,大部分方法集都來(lái)至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實(shí)現(xiàn),某個(gè)測(cè)試用例多次重復(fù)執(zhí)行時(shí), 可啟用并發(fā)參數(shù).

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

相關(guān)文章

  • golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能

    golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能

    這篇文章主要介紹了golang實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能,文章通過(guò)golang導(dǎo)出excel文件返回給web,實(shí)現(xiàn)瀏覽器導(dǎo)出excel文件功能,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • Go語(yǔ)言實(shí)現(xiàn)定時(shí)器的方法

    Go語(yǔ)言實(shí)現(xiàn)定時(shí)器的方法

    這篇文章主要介紹了Go語(yǔ)言實(shí)現(xiàn)定時(shí)器的方法,涉及Go語(yǔ)言時(shí)間操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • 在Go中復(fù)制文件最流行的3種方法

    在Go中復(fù)制文件最流行的3種方法

    今天小編就為大家分享一篇關(guān)于在Go中復(fù)制文件最流行的3種方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • golang獲取用戶輸入的幾種方式

    golang獲取用戶輸入的幾種方式

    這篇文章給大家介紹了golang獲取用戶輸入的幾種方式,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友跟著小編一起來(lái)學(xué)習(xí)吧
    2024-01-01
  • 淺析Golang中變量與常量的聲明與使用

    淺析Golang中變量與常量的聲明與使用

    變量、常量的聲明與使用是掌握一門(mén)編程語(yǔ)言的基礎(chǔ),這篇文章主要為大家詳細(xì)介紹了Golang中變量與常量的聲明與使用,需要的可以參考一下
    2023-04-04
  • Go語(yǔ)言入門(mén)Go?Web?Fiber框架快速了解

    Go語(yǔ)言入門(mén)Go?Web?Fiber框架快速了解

    這篇文章主要為大家介紹了Go語(yǔ)言入門(mén)Go?Web?Fiber框架的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解如何在Go中循環(huán)中使用Defer關(guān)鍵字示例詳解

    詳解如何在Go中循環(huán)中使用Defer關(guān)鍵字示例詳解

    這篇文章主要為大家介紹了詳解如何在Go中循環(huán)中使用Defer關(guān)鍵字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • golang連接redis庫(kù)及基本操作示例過(guò)程

    golang連接redis庫(kù)及基本操作示例過(guò)程

    這篇文章主要介紹了golang連接redis庫(kù)及基本操作示例過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • Golang負(fù)載均衡和保活設(shè)計(jì)原理示例探究

    Golang負(fù)載均衡和保活設(shè)計(jì)原理示例探究

    這篇文章主要為大家介紹了Golang負(fù)載均衡和?;钤O(shè)計(jì)原理示例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)

    Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)

    這篇文章主要介紹了Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評(píng)論