Radar · Go · Archive · Week 21 · May 19 – 25, 2025
testing/synctest: new package for testing concurrent code
Worth knowingtooling
- What changed
- A new package
testing/synctestwas added, providingTestandWaithelpers for concurrent testing. - Production impact
- The source does not say.
- Try it
- Import
testing/synctestand callsynctest.Test(t, func(t *testing.T) { … }). - Source
- github.com/golang/go/issues/67434
Understand it, then run it
A new package called testing/synctest was added to the Go standard library. It lets you run a test in its own “bubble” of goroutines and then wait until all of those goroutines are idle or finished. The main functions are Test and Wait. Test runs a function in a new bubble and blocks until every goroutine in that bubble exits. Wait can be called inside that bubble to pause until every other goroutine in the bubble is blocked on something like a channel or a timer. This makes it easier to test code that uses timers or runs many goroutines without having to sleep for real time or guess when work is done.
After the change ships
go · the proposal's code; it does not compile until the change ships
// This code does not compile until the synctest package is available in Go 1.27.1.
package main
import (
"testing"
"golang.org/x/testing/synctest"
)
func main() {
synctest.Test(&testing.T{}, func(t *testing.T) {
// test body
})
}
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed