Skip to content

Commit e8c59a1

Browse files
authored
chore: avoid flake in resume token test (coder#14378)
1 parent d7800a4 commit e8c59a1

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

coderd/workspaceagents_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,27 @@ func TestWorkspaceAgentClientCoordinate_BadVersion(t *testing.T) {
515515

516516
type resumeTokenTestFakeCoordinator struct {
517517
tailnet.Coordinator
518-
lastPeerID uuid.UUID
518+
t testing.TB
519+
peerIDCh chan uuid.UUID
519520
}
520521

521522
var _ tailnet.Coordinator = &resumeTokenTestFakeCoordinator{}
522523

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+
523532
func (c *resumeTokenTestFakeCoordinator) ServeClient(conn net.Conn, id uuid.UUID, agentID uuid.UUID) error {
524-
c.lastPeerID = id
533+
c.storeID(id)
525534
return c.Coordinator.ServeClient(conn, id, agentID)
526535
}
527536

528537
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)
530539
return c.Coordinator.Coordinate(ctx, id, name, a)
531540
}
532541

@@ -540,7 +549,10 @@ func TestWorkspaceAgentClientCoordinate_ResumeToken(t *testing.T) {
540549
resumeTokenProvider := tailnet.NewResumeTokenKeyProvider(resumeTokenSigningKey, clock, time.Hour)
541550
coordinator := &resumeTokenTestFakeCoordinator{
542551
Coordinator: tailnet.NewCoordinator(logger),
552+
t: t,
553+
peerIDCh: make(chan uuid.UUID, 1),
543554
}
555+
defer close(coordinator.peerIDCh)
544556
client, closer, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
545557
Coordinator: coordinator,
546558
CoordinatorResumeTokenProvider: resumeTokenProvider,
@@ -562,33 +574,36 @@ func TestWorkspaceAgentClientCoordinate_ResumeToken(t *testing.T) {
562574

563575
// Connect with no resume token, and ensure that the peer ID is set to a
564576
// random value.
565-
coordinator.lastPeerID = uuid.Nil
566577
originalResumeToken, err := connectToCoordinatorAndFetchResumeToken(ctx, logger, client, agentAndBuild.WorkspaceAgent.ID, "")
567578
require.NoError(t, err)
568-
originalPeerID := coordinator.lastPeerID
579+
originalPeerID := testutil.RequireRecvCtx(ctx, t, coordinator.peerIDCh)
569580
require.NotEqual(t, originalPeerID, uuid.Nil)
570581

571582
// Connect with a valid resume token, and ensure that the peer ID is set to
572583
// the stored value.
573584
clock.Advance(time.Second)
574-
coordinator.lastPeerID = uuid.Nil
575585
newResumeToken, err := connectToCoordinatorAndFetchResumeToken(ctx, logger, client, agentAndBuild.WorkspaceAgent.ID, originalResumeToken)
576586
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)
578589
require.NotEqual(t, originalResumeToken, newResumeToken)
579590

580591
// Connect with an invalid resume token, and ensure that the request is
581592
// rejected.
582593
clock.Advance(time.Second)
583-
coordinator.lastPeerID = uuid.Nil
584594
_, err = connectToCoordinatorAndFetchResumeToken(ctx, logger, client, agentAndBuild.WorkspaceAgent.ID, "invalid")
585595
require.Error(t, err)
586596
var sdkErr *codersdk.Error
587597
require.ErrorAs(t, err, &sdkErr)
588598
require.Equal(t, http.StatusUnauthorized, sdkErr.StatusCode())
589599
require.Len(t, sdkErr.Validations, 1)
590600
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+
}
592607
}
593608

594609
// connectToCoordinatorAndFetchResumeToken connects to the tailnet coordinator

0 commit comments

Comments
 (0)