util/eventbus/eventbustest: add support for synctest instead of timers (#17522)

Before synctest, timers was needed to allow the events to flow into the
test bus. There is still a timer, but this one is not derived from the
test deadline and it is mostly arbitrary as synctest will render it
practically non-existent.

With this approach, tests that do not need to test for the absence of
events do not rely on synctest.

Updates #15160

Signed-off-by: Claus Lensbøl <claus@tailscale.com>
This commit is contained in:
Claus Lensbøl
2025-10-10 15:33:30 -04:00
committed by GitHub
parent d8a6d0183c
commit 005e264b54
6 changed files with 237 additions and 152 deletions
+14
View File
@@ -39,6 +39,20 @@
// checks that the stream contains exactly the given events in the given order,
// and no others.
//
// To test for the absence of events, use [ExpectExactly] without any
// expected events, along side [testing/synctest] to avoid waiting for timers
// to ensure that no events are produced. This will look like:
//
// synctest.Test(t, func(t *testing.T) {
// bus := eventbustest.NewBus(t)
// tw := eventbustest.NewWatcher(t, bus)
// somethingThatShouldNotEmitsSomeEvent()
// synctest.Wait()
// if err := eventbustest.ExpectExactly(tw); err != nil {
// t.Errorf("Expected no events or errors, got %v", err)
// }
// })
//
// See the [usage examples].
//
// [usage examples]: https://github.com/tailscale/tailscale/blob/main/util/eventbus/eventbustest/examples_test.go