diff options
Diffstat (limited to 'ipn')
| -rw-r--r-- | ipn/ipnlocal/bus_test.go | 150 | ||||
| -rw-r--r-- | ipn/ipnlocal/network-lock_test.go | 4 |
2 files changed, 77 insertions, 77 deletions
diff --git a/ipn/ipnlocal/bus_test.go b/ipn/ipnlocal/bus_test.go index 5c75ac54d..55c936594 100644 --- a/ipn/ipnlocal/bus_test.go +++ b/ipn/ipnlocal/bus_test.go @@ -8,11 +8,11 @@ import ( "reflect" "slices" "testing" + "testing/synctest" "time" "tailscale.com/drive" "tailscale.com/ipn" - "tailscale.com/tstest" "tailscale.com/tstime" "tailscale.com/types/logger" "tailscale.com/types/netmap" @@ -78,21 +78,17 @@ func TestIsNotableNotify(t *testing.T) { } type rateLimitingBusSenderTester struct { - tb testing.TB - got []*ipn.Notify - clock *tstest.Clock - s *rateLimitingBusSender + tb testing.TB + got []*ipn.Notify + s *rateLimitingBusSender } func (st *rateLimitingBusSenderTester) init() { if st.s != nil { return } - st.clock = tstest.NewClock(tstest.ClockOpts{ - Start: time.Unix(1731777537, 0), // time I wrote this test :) - }) st.s = &rateLimitingBusSender{ - clock: tstime.DefaultClock{Clock: st.clock}, + clock: tstime.DefaultClock{}, fn: func(n *ipn.Notify) bool { st.got = append(st.got, n) return true @@ -110,7 +106,7 @@ func (st *rateLimitingBusSenderTester) send(n *ipn.Notify) { func (st *rateLimitingBusSenderTester) advance(d time.Duration) { st.tb.Helper() - st.clock.Advance(d) + time.Sleep(d) select { case <-st.s.flushChan(): if !st.s.flush() { @@ -138,83 +134,87 @@ func TestRateLimitingBusSender(t *testing.T) { }) t.Run("buffered", func(t *testing.T) { - st := &rateLimitingBusSenderTester{tb: t} - st.init() - st.s.interval = 1 * time.Second - st.send(&ipn.Notify{Version: "initial"}) - if len(st.got) != 1 { - t.Fatalf("got %d items; expected 1 (first to flush immediately)", len(st.got)) - } - st.send(nm1) - st.send(nm2) - st.send(eng1) - st.send(eng2) - if len(st.got) != 1 { + synctest.Test(t, func(t *testing.T) { + st := &rateLimitingBusSenderTester{tb: t} + st.init() + st.s.interval = 1 * time.Second + st.send(&ipn.Notify{Version: "initial"}) if len(st.got) != 1 { - t.Fatalf("got %d items; expected still just that first 1", len(st.got)) + t.Fatalf("got %d items; expected 1 (first to flush immediately)", len(st.got)) + } + st.send(nm1) + st.send(nm2) + st.send(eng1) + st.send(eng2) + if len(st.got) != 1 { + if len(st.got) != 1 { + t.Fatalf("got %d items; expected still just that first 1", len(st.got)) + } } - } - // But moving the clock should flush the rest, collasced into one new one. - st.advance(5 * time.Second) - if len(st.got) != 2 { - t.Fatalf("got %d items; want 2", len(st.got)) - } - gotn := st.got[1] - if gotn.NetMap != nm2.NetMap { - t.Errorf("got wrong NetMap; got %p", gotn.NetMap) - } - if gotn.Engine != eng2.Engine { - t.Errorf("got wrong Engine; got %p", gotn.Engine) - } - if t.Failed() { - t.Logf("failed Notify was: %v", logger.AsJSON(gotn)) - } + // But moving the clock should flush the rest, collasced into one new one. + st.advance(5 * time.Second) + if len(st.got) != 2 { + t.Fatalf("got %d items; want 2", len(st.got)) + } + gotn := st.got[1] + if gotn.NetMap != nm2.NetMap { + t.Errorf("got wrong NetMap; got %p", gotn.NetMap) + } + if gotn.Engine != eng2.Engine { + t.Errorf("got wrong Engine; got %p", gotn.Engine) + } + if t.Failed() { + t.Logf("failed Notify was: %v", logger.AsJSON(gotn)) + } + }) }) // Test the Run method t.Run("run", func(t *testing.T) { - st := &rateLimitingBusSenderTester{tb: t} - st.init() - st.s.interval = 1 * time.Second - st.s.lastFlush = st.clock.Now() // pretend we just flushed + synctest.Test(t, func(t *testing.T) { + st := &rateLimitingBusSenderTester{tb: t} + st.init() + st.s.interval = 1 * time.Second + st.s.lastFlush = time.Now() // pretend we just flushed - flushc := make(chan *ipn.Notify, 1) - st.s.fn = func(n *ipn.Notify) bool { - flushc <- n - return true - } - didSend := make(chan bool, 2) - st.s.didSendTestHook = func() { didSend <- true } - waitSend := func() { - select { - case <-didSend: - case <-time.After(5 * time.Second): - t.Error("timeout waiting for call to send") + flushc := make(chan *ipn.Notify, 1) + st.s.fn = func(n *ipn.Notify) bool { + flushc <- n + return true + } + didSend := make(chan bool, 2) + st.s.didSendTestHook = func() { didSend <- true } + waitSend := func() { + select { + case <-didSend: + case <-time.After(5 * time.Second): + t.Error("timeout waiting for call to send") + } } - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() - incoming := make(chan *ipn.Notify, 2) - go func() { - incoming <- nm1 - waitSend() - incoming <- nm2 - waitSend() - st.advance(5 * time.Second) - select { - case n := <-flushc: - if n.NetMap != nm2.NetMap { - t.Errorf("got wrong NetMap; got %p", n.NetMap) + incoming := make(chan *ipn.Notify, 2) + go func() { + incoming <- nm1 + waitSend() + incoming <- nm2 + waitSend() + st.advance(5 * time.Second) + select { + case n := <-flushc: + if n.NetMap != nm2.NetMap { + t.Errorf("got wrong NetMap; got %p", n.NetMap) + } + case <-time.After(10 * time.Second): + t.Error("timeout") } - case <-time.After(10 * time.Second): - t.Error("timeout") - } - cancel() - }() + cancel() + }() - st.s.Run(ctx, incoming) + st.s.Run(ctx, incoming) + }) }) } diff --git a/ipn/ipnlocal/network-lock_test.go b/ipn/ipnlocal/network-lock_test.go index e5df38bdb..d2b6c9f4f 100644 --- a/ipn/ipnlocal/network-lock_test.go +++ b/ipn/ipnlocal/network-lock_test.go @@ -32,8 +32,8 @@ import ( "tailscale.com/tailcfg" "tailscale.com/tka" "tailscale.com/tsd" - "tailscale.com/tstest" "tailscale.com/tstest/tkatest" + "tailscale.com/tstime" "tailscale.com/types/key" "tailscale.com/types/netmap" "tailscale.com/types/persist" @@ -470,7 +470,7 @@ func TestTKASyncTriggersCompact(t *testing.T) { // // Our compaction algorithm preserves AUMs received in the last 14 days, so // we need to backdate the commit times to make the AUMs eligible for compaction. - clock := tstest.NewClock(tstest.ClockOpts{}) + clock := tstime.StdClock{} clock.Advance(-30 * 24 * time.Hour) // Set up the TKA authority on the control plane. |
