---
title: "Comment by Ricky on \"Go synctest：徹底解決並發測試的痛點\" | Hashnode"
description: "1.25 的 synctest 看起來沒有 Run 應該是這樣吧 func TestAfterFunc (t *testing.T) { synctest.Test(t, func (*testing.T) { ctx, cancel := context.WithCancel(context.Background()"
url: "https://hashnode.com/posts/go-synctest/687bc2dfac7de9dd7b16e818/comment/690d91b04ebd4e3a75bad13d"
---

[Discussion](https://hashnode.com/posts/go-synctest/687bc2dfac7de9dd7b16e818)

Comment [Go synctest：徹底解決並發測試的痛點](https://hashnode.com/posts/go-synctest/687bc2dfac7de9dd7b16e818)

[Ricky](https://hashnode.com/@rickylin)

[Nov 7, 2025](https://hashnode.com/posts/go-synctest/687bc2dfac7de9dd7b16e818/comment/690d91b04ebd4e3a75bad13d)

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 後被呼叫")
        }
    })
}
```
