diff options
| author | Damien Neil <dneil@google.com> | 2026-01-28 15:04:46 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-02-02 14:43:36 -0800 |
| commit | 8ac41b52c4b7a6ab9c41d008abbe254270200b8e (patch) | |
| tree | a3fa91b65a385138e2385f5b77b4b04500def277 | |
| parent | 62d08234b797806796af0d51051f2e13caa42e2a (diff) | |
| download | go-8ac41b52c4b7a6ab9c41d008abbe254270200b8e.tar.xz | |
testing/synctest: add Sleep
Add a convenience function which combines time.Sleep and synctest.Wait.
Fixes #77169
Change-Id: I2ff105105e95cfd8e5b4f72ccacf7afa59efb6bd
Reviewed-on: https://go-review.googlesource.com/c/go/+/740066
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
| -rw-r--r-- | api/next/77169.txt | 1 | ||||
| -rw-r--r-- | doc/next/6-stdlib/99-minor/testing/synctest/77169.md | 1 | ||||
| -rw-r--r-- | src/testing/synctest/synctest.go | 21 |
3 files changed, 23 insertions, 0 deletions
diff --git a/api/next/77169.txt b/api/next/77169.txt new file mode 100644 index 0000000000..0d4ed86904 --- /dev/null +++ b/api/next/77169.txt @@ -0,0 +1 @@ +pkg testing/synctest, func Sleep(time.Duration) #77169 diff --git a/doc/next/6-stdlib/99-minor/testing/synctest/77169.md b/doc/next/6-stdlib/99-minor/testing/synctest/77169.md new file mode 100644 index 0000000000..b214a5d645 --- /dev/null +++ b/doc/next/6-stdlib/99-minor/testing/synctest/77169.md @@ -0,0 +1 @@ +The new [Sleep] helper function combines [time.Sleep] and [testing/synctest.Wait]. diff --git a/src/testing/synctest/synctest.go b/src/testing/synctest/synctest.go index 9f499515b8..f254d561c7 100644 --- a/src/testing/synctest/synctest.go +++ b/src/testing/synctest/synctest.go @@ -268,6 +268,7 @@ package synctest import ( "internal/synctest" "testing" + "time" _ "unsafe" // for linkname ) @@ -309,3 +310,23 @@ func testingSynctestTest(t *testing.T, f func(*testing.T)) bool func Wait() { synctest.Wait() } + +// Sleep blocks until the current bubble's clock has advanced +// by the duration of d and every goroutine within the current bubble, +// other than the current goroutine, is durably blocked. +// +// This is exactly equivalent to +// +// time.Sleep(d) +// synctest.Wait() +// +// In tests, this is often preferable to calling only [time.Sleep]. +// If the test itself and another goroutine running the system under test +// sleeps for the exact same amount of time, it's unpredictable which +// of the two goroutines will run first. The test itself usually wants +// to wait for the system under test to "settle" after sleeping. +// This is what Sleep accomplishes. +func Sleep(d time.Duration) { + time.Sleep(d) + Wait() +} |
