77"testing"
88"time"
99
10- "github.com/benbjohnson/clock"
1110"github.com/google/uuid"
1211"github.com/stretchr/testify/assert"
1312"github.com/stretchr/testify/require"
@@ -22,6 +21,7 @@ import (
2221
2322"cdr.dev/slog"
2423"cdr.dev/slog/sloggers/slogtest"
24+ "github.com/coder/coder/v2/clock"
2525"github.com/coder/coder/v2/tailnet/proto"
2626"github.com/coder/coder/v2/testutil"
2727)
@@ -195,9 +195,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_neverConfigures(t *testing.
195195discoKey := key .NewDisco ()
196196uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
197197defer uut .close ()
198- start := time .Date (2024 ,time .March ,29 ,8 ,0 ,0 ,0 ,time .UTC )
199198mClock := clock .NewMock ()
200- mClock .Set (start )
201199uut .clock = mClock
202200
203201p1ID := uuid.UUID {1 }
@@ -241,9 +239,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_outOfOrder(t *testing.T) {
241239discoKey := key .NewDisco ()
242240uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
243241defer uut .close ()
244- start := time .Date (2024 ,time .March ,29 ,8 ,0 ,0 ,0 ,time .UTC )
245242mClock := clock .NewMock ()
246- mClock .Set (start )
247243uut .clock = mClock
248244
249245p1ID := uuid.UUID {1 }
@@ -314,9 +310,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake(t *testing.T) {
314310discoKey := key .NewDisco ()
315311uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
316312defer uut .close ()
317- start := time .Date (2024 ,time .March ,29 ,8 ,0 ,0 ,0 ,time .UTC )
318313mClock := clock .NewMock ()
319- mClock .Set (start )
320314uut .clock = mClock
321315
322316p1ID := uuid.UUID {1 }
@@ -387,9 +381,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_timeout(t *testing.T) {
387381discoKey := key .NewDisco ()
388382uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
389383defer uut .close ()
390- start := time .Date (2024 ,time .March ,29 ,8 ,0 ,0 ,0 ,time .UTC )
391384mClock := clock .NewMock ()
392- mClock .Set (start )
393385uut .clock = mClock
394386
395387p1ID := uuid.UUID {1 }
@@ -412,7 +404,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_timeout(t *testing.T) {
412404}
413405uut .updatePeers (u1 )
414406
415- mClock .Add ( 5 * time .Second )
407+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
416408
417409// it should now send the peer to the netmap
418410
@@ -574,9 +566,8 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
574566discoKey := key .NewDisco ()
575567uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
576568defer uut .close ()
577- start := time .Date (2024 ,time .January ,1 ,8 ,0 ,0 ,0 ,time .UTC )
578569mClock := clock .NewMock ()
579- mClock .Set ( start )
570+ start := mClock .Now ( )
580571uut .clock = mClock
581572
582573p1ID := uuid.UUID {1 }
@@ -600,7 +591,7 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
600591require .Len (t ,r .wg .Peers ,1 )
601592_ = testutil .RequireRecvCtx (ctx ,t ,s1 )
602593
603- mClock .Add ( 5 * time .Second )
594+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
604595
605596s2 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,start )
606597
@@ -621,7 +612,7 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
621612// latest handshake has advanced by a minute, so we don't remove the peer.
622613lh := start .Add (time .Minute )
623614s3 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,lh )
624- mClock .Add (lostTimeout )
615+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
625616_ = testutil .RequireRecvCtx (ctx ,t ,s3 )
626617select {
627618case <- fEng .setNetworkMap :
@@ -630,18 +621,10 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
630621// OK!
631622}
632623
633- // Before we update the clock again, we need to be sure the timeout has
634- // completed running. To do that, we check the new lastHandshake has been set
635- require .Eventually (t ,func ()bool {
636- uut .L .Lock ()
637- defer uut .L .Unlock ()
638- return uut .peers [p1ID ].lastHandshake == lh
639- },testutil .WaitShort ,testutil .IntervalFast )
640-
641624// Advance the clock again by a minute, which should trigger the reprogrammed
642625// timeout.
643626s4 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,lh )
644- mClock .Add (time .Minute )
627+ mClock .Advance (time .Minute ). MustWait ( ctx , t )
645628
646629nm = testutil .RequireRecvCtx (ctx ,t ,fEng .setNetworkMap )
647630r = testutil .RequireRecvCtx (ctx ,t ,fEng .reconfig )
@@ -667,9 +650,8 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
667650discoKey := key .NewDisco ()
668651uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
669652defer uut .close ()
670- start := time .Date (2024 ,time .January ,1 ,8 ,0 ,0 ,0 ,time .UTC )
671653mClock := clock .NewMock ()
672- mClock .Set ( start )
654+ start := mClock .Now ( )
673655uut .clock = mClock
674656
675657p1ID := uuid.UUID {1 }
@@ -693,7 +675,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
693675require .Len (t ,r .wg .Peers ,1 )
694676_ = testutil .RequireRecvCtx (ctx ,t ,s1 )
695677
696- mClock .Add ( 5 * time .Second )
678+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
697679
698680s2 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,start )
699681
@@ -710,7 +692,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
710692// OK!
711693}
712694
713- mClock .Add ( 5 * time .Second )
695+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
714696s3 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,start )
715697
716698updates [0 ].Kind = proto .CoordinateResponse_PeerUpdate_NODE
@@ -727,7 +709,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
727709
728710// When we advance the clock, nothing happens because the timeout was
729711// canceled
730- mClock .Add (lostTimeout )
712+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
731713select {
732714case <- fEng .setNetworkMap :
733715t .Fatal ("should not reprogram" )
@@ -753,9 +735,8 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
753735discoKey := key .NewDisco ()
754736uut := newConfigMaps (logger ,fEng ,nodeID ,nodePrivateKey ,discoKey .Public ())
755737defer uut .close ()
756- start := time .Date (2024 ,time .January ,1 ,8 ,0 ,0 ,0 ,time .UTC )
757738mClock := clock .NewMock ()
758- mClock .Set ( start )
739+ start := mClock .Now ( )
759740uut .clock = mClock
760741
761742p1ID := uuid.UUID {1 }
@@ -788,7 +769,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
788769require .Len (t ,r .wg .Peers ,2 )
789770_ = testutil .RequireRecvCtx (ctx ,t ,s1 )
790771
791- mClock .Add ( 5 * time .Second )
772+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
792773uut .setAllPeersLost ()
793774
794775// No reprogramming yet, since we keep the peer around.
@@ -802,7 +783,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
802783// When we advance the clock, even by a few ms, the timeout for peer 2 pops
803784// because our status only includes a handshake for peer 1
804785s2 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,start )
805- mClock .Add (time .Millisecond * 10 )
786+ mClock .Advance (time .Millisecond * 10 ). MustWait ( ctx , t )
806787_ = testutil .RequireRecvCtx (ctx ,t ,s2 )
807788
808789nm = testutil .RequireRecvCtx (ctx ,t ,fEng .setNetworkMap )
@@ -812,7 +793,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
812793
813794// Finally, advance the clock until after the timeout
814795s3 := expectStatusWithHandshake (ctx ,t ,fEng ,p1Node .Key ,start )
815- mClock .Add (lostTimeout )
796+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
816797_ = testutil .RequireRecvCtx (ctx ,t ,s3 )
817798
818799nm = testutil .RequireRecvCtx (ctx ,t ,fEng .setNetworkMap )