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:
@@ -5,6 +5,8 @@ package eventbustest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"tailscale.com/util/eventbus"
|
||||
"tailscale.com/util/eventbus/eventbustest"
|
||||
@@ -199,3 +201,60 @@ func TestExample_ExpectExactly_WithMultipleFunctions(t *testing.T) {
|
||||
// Output:
|
||||
// expected event type eventbustest.eventOfCuriosity, saw eventbustest.eventOfNoConcern, at index 1
|
||||
}
|
||||
|
||||
func TestExample_ExpectExactly_NoEvents(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
bus := eventbustest.NewBus(t)
|
||||
tw := eventbustest.NewWatcher(t, bus)
|
||||
|
||||
go func() {
|
||||
// Do some work that does not produce an event
|
||||
time.Sleep(10 * time.Second)
|
||||
t.Log("Not producing events")
|
||||
}()
|
||||
|
||||
// Wait for all other routines to be stale before continuing to ensure that
|
||||
// there is nothing running that would produce an event at a later time.
|
||||
synctest.Wait()
|
||||
|
||||
if err := eventbustest.ExpectExactly(tw); err != nil {
|
||||
t.Error(err.Error())
|
||||
} else {
|
||||
t.Log("OK")
|
||||
}
|
||||
// Output:
|
||||
// OK
|
||||
})
|
||||
}
|
||||
|
||||
func TestExample_ExpectExactly_OneEventExpectingTwo(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
type eventOfInterest struct{}
|
||||
|
||||
bus := eventbustest.NewBus(t)
|
||||
tw := eventbustest.NewWatcher(t, bus)
|
||||
client := bus.Client("testClient")
|
||||
updater := eventbus.Publish[eventOfInterest](client)
|
||||
|
||||
go func() {
|
||||
// Do some work that does not produce an event
|
||||
time.Sleep(10 * time.Second)
|
||||
updater.Publish(eventOfInterest{})
|
||||
}()
|
||||
|
||||
// Wait for all other routines to be stale before continuing to ensure that
|
||||
// there is nothing running that would produce an event at a later time.
|
||||
synctest.Wait()
|
||||
|
||||
if err := eventbustest.ExpectExactly(tw,
|
||||
eventbustest.Type[eventOfInterest](),
|
||||
eventbustest.Type[eventOfInterest](),
|
||||
); err != nil {
|
||||
t.Log(err.Error())
|
||||
} else {
|
||||
t.Log("OK")
|
||||
}
|
||||
// Output:
|
||||
// timed out waiting for event, saw 1 events, 2 was expected
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user