@@ -515,18 +515,27 @@ func TestWorkspaceAgentClientCoordinate_BadVersion(t *testing.T) {
515
515
516
516
type resumeTokenTestFakeCoordinator struct {
517
517
tailnet.Coordinator
518
- lastPeerID uuid.UUID
518
+ t testing.TB
519
+ peerIDCh chan uuid.UUID
519
520
}
520
521
521
522
var _ tailnet.Coordinator = & resumeTokenTestFakeCoordinator {}
522
523
524
+ func (c * resumeTokenTestFakeCoordinator ) storeID (id uuid.UUID ) {
525
+ select {
526
+ case c .peerIDCh <- id :
527
+ default :
528
+ c .t .Fatal ("peer ID channel full" )
529
+ }
530
+ }
531
+
523
532
func (c * resumeTokenTestFakeCoordinator ) ServeClient (conn net.Conn , id uuid.UUID , agentID uuid.UUID ) error {
524
- c .lastPeerID = id
533
+ c .storeID ( id )
525
534
return c .Coordinator .ServeClient (conn , id , agentID )
526
535
}
527
536
528
537
func (c * resumeTokenTestFakeCoordinator ) Coordinate (ctx context.Context , id uuid.UUID , name string , a tailnet.CoordinateeAuth ) (chan <- * tailnetproto.CoordinateRequest , <- chan * tailnetproto.CoordinateResponse ) {
529
- c .lastPeerID = id
538
+ c .storeID ( id )
530
539
return c .Coordinator .Coordinate (ctx , id , name , a )
531
540
}
532
541
@@ -540,7 +549,10 @@ func TestWorkspaceAgentClientCoordinate_ResumeToken(t *testing.T) {
540
549
resumeTokenProvider := tailnet .NewResumeTokenKeyProvider (resumeTokenSigningKey , clock , time .Hour )
541
550
coordinator := & resumeTokenTestFakeCoordinator {
542
551
Coordinator : tailnet .NewCoordinator (logger ),
552
+ t : t ,
553
+ peerIDCh : make (chan uuid.UUID , 1 ),
543
554
}
555
+ defer close (coordinator .peerIDCh )
544
556
client , closer , api := coderdtest .NewWithAPI (t , & coderdtest.Options {
545
557
Coordinator : coordinator ,
546
558
CoordinatorResumeTokenProvider : resumeTokenProvider ,
@@ -562,33 +574,36 @@ func TestWorkspaceAgentClientCoordinate_ResumeToken(t *testing.T) {
562
574
563
575
// Connect with no resume token, and ensure that the peer ID is set to a
564
576
// random value.
565
- coordinator .lastPeerID = uuid .Nil
566
577
originalResumeToken , err := connectToCoordinatorAndFetchResumeToken (ctx , logger , client , agentAndBuild .WorkspaceAgent .ID , "" )
567
578
require .NoError (t , err )
568
- originalPeerID := coordinator .lastPeerID
579
+ originalPeerID := testutil . RequireRecvCtx ( ctx , t , coordinator .peerIDCh )
569
580
require .NotEqual (t , originalPeerID , uuid .Nil )
570
581
571
582
// Connect with a valid resume token, and ensure that the peer ID is set to
572
583
// the stored value.
573
584
clock .Advance (time .Second )
574
- coordinator .lastPeerID = uuid .Nil
575
585
newResumeToken , err := connectToCoordinatorAndFetchResumeToken (ctx , logger , client , agentAndBuild .WorkspaceAgent .ID , originalResumeToken )
576
586
require .NoError (t , err )
577
- require .Equal (t , originalPeerID , coordinator .lastPeerID )
587
+ newPeerID := testutil .RequireRecvCtx (ctx , t , coordinator .peerIDCh )
588
+ require .Equal (t , originalPeerID , newPeerID )
578
589
require .NotEqual (t , originalResumeToken , newResumeToken )
579
590
580
591
// Connect with an invalid resume token, and ensure that the request is
581
592
// rejected.
582
593
clock .Advance (time .Second )
583
- coordinator .lastPeerID = uuid .Nil
584
594
_ , err = connectToCoordinatorAndFetchResumeToken (ctx , logger , client , agentAndBuild .WorkspaceAgent .ID , "invalid" )
585
595
require .Error (t , err )
586
596
var sdkErr * codersdk.Error
587
597
require .ErrorAs (t , err , & sdkErr )
588
598
require .Equal (t , http .StatusUnauthorized , sdkErr .StatusCode ())
589
599
require .Len (t , sdkErr .Validations , 1 )
590
600
require .Equal (t , "resume_token" , sdkErr .Validations [0 ].Field )
591
- require .Equal (t , uuid .Nil , coordinator .lastPeerID )
601
+
602
+ select {
603
+ case <- coordinator .peerIDCh :
604
+ t .Fatal ("unexpected peer ID in channel" )
605
+ default :
606
+ }
592
607
}
593
608
594
609
// connectToCoordinatorAndFetchResumeToken connects to the tailnet coordinator
0 commit comments