Fake time and versioned testing/synctest
Fake time and versioned testing/synctest
Time-dependent tests become fragile when they wait on the real clock. Inject a controllable event source first. A channel can represent a timer firing so the test decides exactly when work proceeds. This is a manual fake, not a virtual clock for the whole runtime. Go 1.24 introduced experimental testing/synctest with GOEXPERIMENT=synctest and Run; Go 1.25 graduated the package with Test and Wait. The installed Go 1.23.2 does not support either API.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing" "time")
func TestLesson(t *testing.T) { tick := make(chan time.Time, 1) done := make(chan time.Time, 1) go func() { done <- <-tick }() select { case <-done: t.Fatal("unexpected event") default: } want := time.Unix(100, 0) tick <- want if got := <-done; !got.Equal(want) { t.Fatal(got) }}Injecting tick removes real time from the test. The negative assertion is sound because the worker cannot send until tick is supplied. This approach requires an explicit dependency in the application and does not change time.Now or time.Sleep globally.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTest a deadline decision without wall time
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing" "time")
func TestLesson(t *testing.T) { deadline := time.Unix(200, 0) expired := func(now time.Time) bool { return !now.Before(deadline) } for _, tc := range []struct { now time.Time want bool }{{deadline.Add(-time.Nanosecond), false}, {deadline, true}, {deadline.Add(time.Second), true}} { if expired(tc.now) != tc.want { t.Fatal(tc) } }}Explicit time inputs test exact boundaries. For Go 1.25+, the separate staged synctest example uses synctest.Test(t, func(t *testing.T){...}) and synctest.Wait(). The bubble has a virtual clock and advances when its goroutines are durably blocked; Wait waits for other bubble goroutines to be durably blocked. External I/O and mutex waits are not generally durable blocking. Use only in-bubble resources; Wait is not a general-purpose join. The staged example is documentation-verified, NOT RUN here. See https://go.dev/doc/go1.25 and https://pkg.go.dev/testing/synctest.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Inject a channel event to trigger a worker that returns the string expired. Assert it cannot return before receiving the event. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Concurrency testing 07: Fake time and versioned testing/synctest
Check the lesson objective before moving on.
What is the first generally available synctest API release?
Does an injected timer channel change time.Now everywhere?