mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-14 15:07:55 +00:00
util/eventbus: add test helpers to simplify testing events (#16294)
Instead of every module having to come up with a set of test methods for the event bus, this handful of test helpers hides a lot of the needed setup for the testing of the event bus. The tests in portmapper is also ported over to the new helpers. Updates #15160 Signed-off-by: Claus Lensbøl <claus@tailscale.com>
This commit is contained in:
@@ -515,7 +515,7 @@ func (c *Client) createMapping() {
|
||||
GoodUntil: mapping.GoodUntil(),
|
||||
})
|
||||
}
|
||||
if c.onChange != nil {
|
||||
if c.onChange != nil && c.pubClient == nil {
|
||||
go c.onChange()
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tailscale.com/control/controlknobs"
|
||||
"tailscale.com/util/eventbus"
|
||||
"tailscale.com/util/eventbus/eventbustest"
|
||||
)
|
||||
|
||||
func TestCreateOrGetMapping(t *testing.T) {
|
||||
@@ -142,22 +142,15 @@ func TestUpdateEvent(t *testing.T) {
|
||||
t.Fatalf("Create test gateway: %v", err)
|
||||
}
|
||||
|
||||
bus := eventbus.New()
|
||||
defer bus.Close()
|
||||
bus := eventbustest.NewBus(t)
|
||||
tw := eventbustest.NewWatcher(t, bus)
|
||||
|
||||
sub := eventbus.Subscribe[Mapping](bus.Client("TestUpdateEvent"))
|
||||
c := newTestClient(t, igd, bus)
|
||||
if _, err := c.Probe(t.Context()); err != nil {
|
||||
t.Fatalf("Probe failed: %v", err)
|
||||
}
|
||||
c.GetCachedMappingOrStartCreatingOne()
|
||||
|
||||
select {
|
||||
case evt := <-sub.Events():
|
||||
t.Logf("Received portmap update: %+v", evt)
|
||||
case <-sub.Done():
|
||||
t.Error("Subscriber closed prematurely")
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("Timed out waiting for an update event")
|
||||
if err := eventbustest.Expect(tw, eventbustest.Type[Mapping]()); err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user