testing/synctest

Guided tour · Testing · pkg.go.dev →

Test concurrent code with a fake clock and deterministic scheduling. Available since Go 1.24 (GOEXPERIMENT) / stable in 1.25+.

Bubbles and fake time

Test

func TestTimeout(t *testing.T) {
    synctest.Test(t, func(t *testing.T) {
        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()

        start := time.Now()
        <-ctx.Done()
        if d := time.Since(start); d != time.Second {
            t.Errorf("wanted exactly 1s, got %v", d)
        }
    })
}

Wait for all goroutines idle

synctest.Wait() // inside a bubble — blocks until all bubble goroutines are durably blocked