Skip to content

Commit 80aa609

Browse files
committed
add test for agent calling RefreshToken()
1 parent 2947bcd commit 80aa609

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

agent/agent_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,9 +2952,12 @@ func TestAgent_Reconnect(t *testing.T) {
29522952
defer closer.Close()
29532953

29542954
call1 := testutil.RequireReceive(ctx, t, fCoordinator.CoordinateCalls)
2955+
require.Equal(t, client.GetNumRefreshTokenCalls(), 1)
29552956
close(call1.Resps) // hang up
29562957
// expect reconnect
29572958
testutil.RequireReceive(ctx, t, fCoordinator.CoordinateCalls)
2959+
// Check that the agent refreshes the token when it reconnects.
2960+
require.Equal(t, client.GetNumRefreshTokenCalls(), 2)
29582961
closer.Close()
29592962
}
29602963

agent/agenttest/client.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ type Client struct {
8888
fakeAgentAPI *FakeAgentAPI
8989
LastWorkspaceAgent func()
9090

91-
mu sync.Mutex // Protects following.
92-
logs []agentsdk.Log
93-
derpMapUpdates chan *tailcfg.DERPMap
94-
derpMapOnce sync.Once
91+
mu sync.Mutex // Protects following.
92+
logs []agentsdk.Log
93+
derpMapUpdates chan *tailcfg.DERPMap
94+
derpMapOnce sync.Once
95+
refreshTokenCalls int
9596
}
9697

9798
func (*Client) AsRequestOption() codersdk.RequestOption {
@@ -104,10 +105,19 @@ func (*Client) GetSessionToken() string {
104105
return "agenttest-token"
105106
}
106107

107-
func (*Client) RefreshToken(context.Context) error {
108+
func (c *Client) RefreshToken(context.Context) error {
109+
c.mu.Lock()
110+
defer c.mu.Unlock()
111+
c.refreshTokenCalls++
108112
return nil
109113
}
110114

115+
func (c *Client) GetNumRefreshTokenCalls() int {
116+
c.mu.Lock()
117+
defer c.mu.Unlock()
118+
return c.refreshTokenCalls
119+
}
120+
111121
func (*Client) RewriteDERPMap(*tailcfg.DERPMap) {}
112122

113123
func (c *Client) Close() {

0 commit comments

Comments
 (0)