Search posts, tags, users, and pages
Go introduces synctest for precise goroutine synchronization, enhancing concurrent testing accuracy
雷N
@nathanrayn · Taipei, Taiwan
Markdown supported · ⌘↵ to post
1.25 的 synctest 看起來沒有 Run
應該是這樣吧
func TestAfterFunc(t *testing.T) { synctest.Test(t, func(*testing.T) { ctx, cancel := context.WithCancel(context.Background()) called := false context.AfterFunc(ctx, func() { called = true }) synctest.Wait() // 等到所有 goroutine 都卡住 if called { t.Fatal("AfterFunc 在 cancel 前就被呼叫") } cancel() synctest.Wait() // 再等一次 if !called { t.Fatal("AfterFunc 沒有在 cancel 後被呼叫") } }) }
Ricky
1.25 的 synctest 看起來沒有 Run
應該是這樣吧
func TestAfterFunc(t *testing.T) { synctest.Test(t, func(*testing.T) { ctx, cancel := context.WithCancel(context.Background()) called := false context.AfterFunc(ctx, func() { called = true }) synctest.Wait() // 等到所有 goroutine 都卡住 if called { t.Fatal("AfterFunc 在 cancel 前就被呼叫") } cancel() synctest.Wait() // 再等一次 if !called { t.Fatal("AfterFunc 沒有在 cancel 後被呼叫") } }) }