@@ -13,14 +13,13 @@ import (
13
13
14
14
"github.com/google/uuid"
15
15
"github.com/stretchr/testify/require"
16
- "google.golang.org/protobuf/types/known/durationpb"
17
16
"google.golang.org/protobuf/types/known/timestamppb"
18
- "tailscale.com/ipn/ipnstate"
19
17
"tailscale.com/util/dnsname"
20
18
21
19
"github.com/coder/coder/v2/tailnet"
22
20
"github.com/coder/coder/v2/tailnet/proto"
23
21
"github.com/coder/coder/v2/testutil"
22
+ "github.com/coder/quartz"
24
23
)
25
24
26
25
func newFakeClient (ctx context.Context ,t * testing.T )* fakeClient {
@@ -69,10 +68,6 @@ func (f *fakeConn) CurrentWorkspaceState() (tailnet.WorkspaceUpdate, error) {
69
68
return f .state ,nil
70
69
}
71
70
72
- func (* fakeConn )Ping (context.Context , netip.Addr ) (time.Duration ,bool ,* ipnstate.PingResult ,error ) {
73
- return time .Millisecond * 100 ,true ,& ipnstate.PingResult {},nil
74
- }
75
-
76
71
func (f * fakeConn )GetPeerDiagnostics (uuid.UUID ) tailnet.PeerDiagnostics {
77
72
return tailnet.PeerDiagnostics {
78
73
LastWireguardHandshake :f .hsTime ,
@@ -93,7 +88,7 @@ func TestTunnel_StartStop(t *testing.T) {
93
88
client := newFakeClient (ctx ,t )
94
89
conn := newFakeConn (tailnet.WorkspaceUpdate {}, time.Time {})
95
90
96
- _ ,mgr := setupTunnel (t ,ctx ,client )
91
+ _ ,mgr := setupTunnel (t ,ctx ,client , quartz . NewMock ( t ) )
97
92
98
93
errCh := make (chan error ,1 )
99
94
var resp * TunnelMessage
@@ -159,7 +154,7 @@ func TestTunnel_PeerUpdate(t *testing.T) {
159
154
},
160
155
}, time.Time {})
161
156
162
- tun ,mgr := setupTunnel (t ,ctx ,client )
157
+ tun ,mgr := setupTunnel (t ,ctx ,client , quartz . NewMock ( t ) )
163
158
164
159
errCh := make (chan error ,1 )
165
160
var resp * TunnelMessage
@@ -224,7 +219,7 @@ func TestTunnel_NetworkSettings(t *testing.T) {
224
219
client := newFakeClient (ctx ,t )
225
220
conn := newFakeConn (tailnet.WorkspaceUpdate {}, time.Time {})
226
221
227
- tun ,mgr := setupTunnel (t ,ctx ,client )
222
+ tun ,mgr := setupTunnel (t ,ctx ,client , quartz . NewMock ( t ) )
228
223
229
224
errCh := make (chan error ,1 )
230
225
var resp * TunnelMessage
@@ -287,7 +282,7 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
287
282
288
283
client := newFakeClient (ctx ,t )
289
284
290
- tun ,_ := setupTunnel (t ,ctx ,client )
285
+ tun ,_ := setupTunnel (t ,ctx ,client , quartz . NewMock ( t ) )
291
286
hsTime := time .Now ().Add (- time .Minute ).UTC ()
292
287
tun .conn = newFakeConn (tailnet.WorkspaceUpdate {},hsTime )
293
288
@@ -337,7 +332,6 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
337
332
Fqdn : []string {"w1.coder." ,"w1a1.w1.me.coder." ,"w1a1.w1.testy.coder." },
338
333
IpAddrs : []string {w1a1IP .String ()},
339
334
LastHandshake :timestamppb .New (hsTime ),
340
- Latency :durationpb .New (100 * time .Millisecond ),
341
335
},
342
336
},
343
337
DeletedWorkspaces : []* Workspace {
@@ -348,7 +342,6 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
348
342
Id :w2a1ID [:],Name :"w2a1" ,WorkspaceId :w2ID [:],
349
343
Fqdn : []string {"w2.coder." ,"w2a1.w2.me.coder." ,"w2a1.w2.testy.coder." },
350
344
IpAddrs : []string {w2a1IP .String ()},
351
- Latency :nil ,
352
345
LastHandshake :nil ,
353
346
},
354
347
},
@@ -360,6 +353,8 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
360
353
361
354
ctx := testutil .Context (t ,testutil .WaitShort )
362
355
356
+ mClock := quartz .NewMock (t )
357
+
363
358
wID1 := uuid.UUID {1 }
364
359
aID1 := uuid.UUID {2 }
365
360
aID2 := uuid.UUID {3 }
@@ -368,8 +363,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
368
363
client := newFakeClient (ctx ,t )
369
364
conn := newFakeConn (tailnet.WorkspaceUpdate {},hsTime )
370
365
371
- tun ,mgr := setupTunnel (t ,ctx ,client )
372
-
366
+ tun ,mgr := setupTunnel (t ,ctx ,client ,mClock )
373
367
errCh := make (chan error ,1 )
374
368
var resp * TunnelMessage
375
369
go func () {
@@ -391,9 +385,6 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
391
385
_ ,ok := resp .Msg .(* TunnelMessage_Start )
392
386
require .True (t ,ok )
393
387
394
- // `sendAgentUpdate` is a no-op if there's no agents
395
- tun .sendAgentUpdate ()
396
-
397
388
// Inform the tunnel of the initial state
398
389
err = tun .Update (tailnet.WorkspaceUpdate {
399
390
UpsertedWorkspaces : []* tailnet.Workspace {
@@ -422,8 +413,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
422
413
// `sendAgentUpdate` produces the same PeerUpdate message until an agent
423
414
// update is received
424
415
for range 2 {
425
- // When: we send a (normally scheduled) agent update
426
- tun .sendAgentUpdate ()
416
+ mClock .AdvanceNext ()
427
417
// Then: the tunnel sends a PeerUpdate message of agent upserts,
428
418
// with the last handshake and latency set
429
419
req = testutil .RequireRecvCtx (ctx ,t ,mgr .requests )
@@ -432,7 +422,6 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
432
422
require .Len (t ,req .msg .GetPeerUpdate ().UpsertedAgents ,1 )
433
423
require .Equal (t ,aID1 [:],req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Id )
434
424
require .Equal (t ,hsTime ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].LastHandshake .AsTime ())
435
- require .Equal (t ,100 * time .Millisecond ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Latency .AsDuration ())
436
425
}
437
426
438
427
// Upsert a new agent
@@ -453,7 +442,7 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
453
442
testutil .RequireRecvCtx (ctx ,t ,mgr .requests )
454
443
455
444
// The new update includes the new agent
456
- tun . sendAgentUpdate ()
445
+ mClock . AdvanceNext ()
457
446
req = testutil .RequireRecvCtx (ctx ,t ,mgr .requests )
458
447
require .Nil (t ,req .msg .Rpc )
459
448
require .NotNil (t ,req .msg .GetPeerUpdate ())
@@ -464,10 +453,8 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
464
453
465
454
require .Equal (t ,aID1 [:],req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Id )
466
455
require .Equal (t ,hsTime ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].LastHandshake .AsTime ())
467
- require .Equal (t ,100 * time .Millisecond ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Latency .AsDuration ())
468
456
require .Equal (t ,aID2 [:],req .msg .GetPeerUpdate ().UpsertedAgents [1 ].Id )
469
457
require .Equal (t ,hsTime ,req .msg .GetPeerUpdate ().UpsertedAgents [1 ].LastHandshake .AsTime ())
470
- require .Equal (t ,100 * time .Millisecond ,req .msg .GetPeerUpdate ().UpsertedAgents [1 ].Latency .AsDuration ())
471
458
472
459
// Delete an agent
473
460
err = tun .Update (tailnet.WorkspaceUpdate {
@@ -486,18 +473,17 @@ func TestTunnel_sendAgentUpdate(t *testing.T) {
486
473
testutil .RequireRecvCtx (ctx ,t ,mgr .requests )
487
474
488
475
// The new update doesn't include the deleted agent
489
- tun . sendAgentUpdate ()
476
+ mClock . AdvanceNext ()
490
477
req = testutil .RequireRecvCtx (ctx ,t ,mgr .requests )
491
478
require .Nil (t ,req .msg .Rpc )
492
479
require .NotNil (t ,req .msg .GetPeerUpdate ())
493
480
require .Len (t ,req .msg .GetPeerUpdate ().UpsertedAgents ,1 )
494
481
require .Equal (t ,aID2 [:],req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Id )
495
482
require .Equal (t ,hsTime ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].LastHandshake .AsTime ())
496
- require .Equal (t ,100 * time .Millisecond ,req .msg .GetPeerUpdate ().UpsertedAgents [0 ].Latency .AsDuration ())
497
483
}
498
484
499
485
//nolint:revive // t takes precedence
500
- func setupTunnel (t * testing.T ,ctx context.Context ,client * fakeClient ) (* Tunnel ,* speaker [* ManagerMessage ,* TunnelMessage ,TunnelMessage ]) {
486
+ func setupTunnel (t * testing.T ,ctx context.Context ,client * fakeClient , mClock quartz. Clock ) (* Tunnel ,* speaker [* ManagerMessage ,* TunnelMessage ,TunnelMessage ]) {
501
487
mp ,tp := net .Pipe ()
502
488
t .Cleanup (func () {_ = mp .Close () })
503
489
t .Cleanup (func () {_ = tp .Close () })
@@ -507,7 +493,7 @@ func setupTunnel(t *testing.T, ctx context.Context, client *fakeClient) (*Tunnel
507
493
var mgr * speaker [* ManagerMessage ,* TunnelMessage ,TunnelMessage ]
508
494
errCh := make (chan error ,2 )
509
495
go func () {
510
- tunnel ,err := NewTunnel (ctx ,logger .Named ("tunnel" ),tp ,client )
496
+ tunnel ,err := NewTunnel (ctx ,logger .Named ("tunnel" ),tp ,client , WithClock ( mClock ) )
511
497
tun = tunnel
512
498
errCh <- err
513
499
}()