7
7
"testing"
8
8
"time"
9
9
10
- "github.com/benbjohnson/clock"
11
10
"github.com/google/uuid"
12
11
"github.com/stretchr/testify/assert"
13
12
"github.com/stretchr/testify/require"
@@ -22,6 +21,7 @@ import (
22
21
23
22
"cdr.dev/slog"
24
23
"cdr.dev/slog/sloggers/slogtest"
24
+ "github.com/coder/coder/v2/clock"
25
25
"github.com/coder/coder/v2/tailnet/proto"
26
26
"github.com/coder/coder/v2/testutil"
27
27
)
@@ -195,9 +195,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_neverConfigures(t *testing.
195
195
discoKey := key .NewDisco ()
196
196
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
197
197
defer uut .close ()
198
- start := time .Date (2024 , time .March , 29 , 8 , 0 , 0 , 0 , time .UTC )
199
198
mClock := clock .NewMock ()
200
- mClock .Set (start )
201
199
uut .clock = mClock
202
200
203
201
p1ID := uuid.UUID {1 }
@@ -241,9 +239,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_outOfOrder(t *testing.T) {
241
239
discoKey := key .NewDisco ()
242
240
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
243
241
defer uut .close ()
244
- start := time .Date (2024 , time .March , 29 , 8 , 0 , 0 , 0 , time .UTC )
245
242
mClock := clock .NewMock ()
246
- mClock .Set (start )
247
243
uut .clock = mClock
248
244
249
245
p1ID := uuid.UUID {1 }
@@ -314,9 +310,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake(t *testing.T) {
314
310
discoKey := key .NewDisco ()
315
311
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
316
312
defer uut .close ()
317
- start := time .Date (2024 , time .March , 29 , 8 , 0 , 0 , 0 , time .UTC )
318
313
mClock := clock .NewMock ()
319
- mClock .Set (start )
320
314
uut .clock = mClock
321
315
322
316
p1ID := uuid.UUID {1 }
@@ -387,9 +381,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_timeout(t *testing.T) {
387
381
discoKey := key .NewDisco ()
388
382
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
389
383
defer uut .close ()
390
- start := time .Date (2024 , time .March , 29 , 8 , 0 , 0 , 0 , time .UTC )
391
384
mClock := clock .NewMock ()
392
- mClock .Set (start )
393
385
uut .clock = mClock
394
386
395
387
p1ID := uuid.UUID {1 }
@@ -412,7 +404,7 @@ func TestConfigMaps_updatePeers_new_waitForHandshake_timeout(t *testing.T) {
412
404
}
413
405
uut .updatePeers (u1 )
414
406
415
- mClock .Add ( 5 * time .Second )
407
+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
416
408
417
409
// it should now send the peer to the netmap
418
410
@@ -574,9 +566,8 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
574
566
discoKey := key .NewDisco ()
575
567
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
576
568
defer uut .close ()
577
- start := time .Date (2024 , time .January , 1 , 8 , 0 , 0 , 0 , time .UTC )
578
569
mClock := clock .NewMock ()
579
- mClock .Set ( start )
570
+ start := mClock .Now ( )
580
571
uut .clock = mClock
581
572
582
573
p1ID := uuid.UUID {1 }
@@ -600,7 +591,7 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
600
591
require .Len (t , r .wg .Peers , 1 )
601
592
_ = testutil .RequireRecvCtx (ctx , t , s1 )
602
593
603
- mClock .Add ( 5 * time .Second )
594
+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
604
595
605
596
s2 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , start )
606
597
@@ -621,7 +612,7 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
621
612
// latest handshake has advanced by a minute, so we don't remove the peer.
622
613
lh := start .Add (time .Minute )
623
614
s3 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , lh )
624
- mClock .Add (lostTimeout )
615
+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
625
616
_ = testutil .RequireRecvCtx (ctx , t , s3 )
626
617
select {
627
618
case <- fEng .setNetworkMap :
@@ -630,18 +621,10 @@ func TestConfigMaps_updatePeers_lost(t *testing.T) {
630
621
// OK!
631
622
}
632
623
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
-
641
624
// Advance the clock again by a minute, which should trigger the reprogrammed
642
625
// timeout.
643
626
s4 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , lh )
644
- mClock .Add (time .Minute )
627
+ mClock .Advance (time .Minute ). MustWait ( ctx , t )
645
628
646
629
nm = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
647
630
r = testutil .RequireRecvCtx (ctx , t , fEng .reconfig )
@@ -667,9 +650,8 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
667
650
discoKey := key .NewDisco ()
668
651
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
669
652
defer uut .close ()
670
- start := time .Date (2024 , time .January , 1 , 8 , 0 , 0 , 0 , time .UTC )
671
653
mClock := clock .NewMock ()
672
- mClock .Set ( start )
654
+ start := mClock .Now ( )
673
655
uut .clock = mClock
674
656
675
657
p1ID := uuid.UUID {1 }
@@ -693,7 +675,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
693
675
require .Len (t , r .wg .Peers , 1 )
694
676
_ = testutil .RequireRecvCtx (ctx , t , s1 )
695
677
696
- mClock .Add ( 5 * time .Second )
678
+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
697
679
698
680
s2 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , start )
699
681
@@ -710,7 +692,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
710
692
// OK!
711
693
}
712
694
713
- mClock .Add ( 5 * time .Second )
695
+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
714
696
s3 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , start )
715
697
716
698
updates [0 ].Kind = proto .CoordinateResponse_PeerUpdate_NODE
@@ -727,7 +709,7 @@ func TestConfigMaps_updatePeers_lost_and_found(t *testing.T) {
727
709
728
710
// When we advance the clock, nothing happens because the timeout was
729
711
// canceled
730
- mClock .Add (lostTimeout )
712
+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
731
713
select {
732
714
case <- fEng .setNetworkMap :
733
715
t .Fatal ("should not reprogram" )
@@ -753,9 +735,8 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
753
735
discoKey := key .NewDisco ()
754
736
uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
755
737
defer uut .close ()
756
- start := time .Date (2024 , time .January , 1 , 8 , 0 , 0 , 0 , time .UTC )
757
738
mClock := clock .NewMock ()
758
- mClock .Set ( start )
739
+ start := mClock .Now ( )
759
740
uut .clock = mClock
760
741
761
742
p1ID := uuid.UUID {1 }
@@ -788,7 +769,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
788
769
require .Len (t , r .wg .Peers , 2 )
789
770
_ = testutil .RequireRecvCtx (ctx , t , s1 )
790
771
791
- mClock .Add ( 5 * time .Second )
772
+ mClock .Advance ( 5 * time .Second ). MustWait ( ctx , t )
792
773
uut .setAllPeersLost ()
793
774
794
775
// No reprogramming yet, since we keep the peer around.
@@ -802,7 +783,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
802
783
// When we advance the clock, even by a few ms, the timeout for peer 2 pops
803
784
// because our status only includes a handshake for peer 1
804
785
s2 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , start )
805
- mClock .Add (time .Millisecond * 10 )
786
+ mClock .Advance (time .Millisecond * 10 ). MustWait ( ctx , t )
806
787
_ = testutil .RequireRecvCtx (ctx , t , s2 )
807
788
808
789
nm = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
@@ -812,7 +793,7 @@ func TestConfigMaps_setAllPeersLost(t *testing.T) {
812
793
813
794
// Finally, advance the clock until after the timeout
814
795
s3 := expectStatusWithHandshake (ctx , t , fEng , p1Node .Key , start )
815
- mClock .Add (lostTimeout )
796
+ mClock .Advance (lostTimeout ). MustWait ( ctx , t )
816
797
_ = testutil .RequireRecvCtx (ctx , t , s3 )
817
798
818
799
nm = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
0 commit comments