@@ -112,8 +112,10 @@ func TestUserLatencyInsights(t *testing.T) {
112
112
AgentStatsRefreshInterval : time .Millisecond * 100 ,
113
113
})
114
114
115
+ // Create two users, one that will appear in the report and another that
116
+ // won't (due to not having/using a workspace).
115
117
user := coderdtest .CreateFirstUser (t , client )
116
- _ , user2 : = coderdtest .CreateAnotherUser (t , client , user .OrganizationID )
118
+ _ , _ = coderdtest .CreateAnotherUser (t , client , user .OrganizationID )
117
119
authToken := uuid .NewString ()
118
120
version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
119
121
Parse : echo .ParseComplete ,
@@ -127,6 +129,7 @@ func TestUserLatencyInsights(t *testing.T) {
127
129
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
128
130
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
129
131
132
+ // Start an agent so that we can generate stats.
130
133
agentClient := agentsdk .New (client .URL )
131
134
agentClient .SetSessionToken (authToken )
132
135
agentCloser := agent .New (agent.Options {
@@ -138,9 +141,15 @@ func TestUserLatencyInsights(t *testing.T) {
138
141
}()
139
142
resources := coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
140
143
144
+ // Start must be at the beginning of the day, initialize it early in case
145
+ // the day changes so that we get the relevant stats faster.
146
+ y , m , d := time .Now ().UTC ().Date ()
147
+ today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
148
+
141
149
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
142
150
defer cancel ()
143
151
152
+ // Connect to the agent to generate usage/latency stats.
144
153
conn , err := client .DialWorkspaceAgent (ctx , resources [0 ].Agents [0 ].ID , & codersdk.DialWorkspaceAgentOptions {
145
154
Logger : logger .Named ("client" ),
146
155
})
@@ -151,19 +160,6 @@ func TestUserLatencyInsights(t *testing.T) {
151
160
require .NoError (t , err )
152
161
defer sshConn .Close ()
153
162
154
- // Create users that will not appear in the report.
155
- _ , user3 := coderdtest .CreateAnotherUser (t , client , user .OrganizationID )
156
- _ , user4 := coderdtest .CreateAnotherUser (t , client , user .OrganizationID )
157
- _ , err = client .UpdateUserStatus (ctx , user3 .Username , codersdk .UserStatusSuspended )
158
- require .NoError (t , err )
159
- err = client .DeleteUser (ctx , user4 .ID )
160
- require .NoError (t , err )
161
-
162
- y , m , d := time .Now ().Date ()
163
- today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
164
-
165
- _ = sshConn .Close ()
166
-
167
163
var userLatencies codersdk.UserLatencyInsightsResponse
168
164
require .Eventuallyf (t , func () bool {
169
165
userLatencies , err = client .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
@@ -174,16 +170,16 @@ func TestUserLatencyInsights(t *testing.T) {
174
170
if ! assert .NoError (t , err ) {
175
171
return false
176
172
}
177
- if userLatencies .Report .Users [0 ].UserID == user2 .ID {
178
- userLatencies .Report .Users [0 ], userLatencies .Report .Users [1 ] = userLatencies .Report .Users [1 ], userLatencies .Report .Users [0 ]
179
- }
180
- return userLatencies .Report .Users [0 ].LatencyMS != nil
173
+ return len (userLatencies .Report .Users ) > 0 && userLatencies .Report .Users [0 ].LatencyMS .P50 > 0
181
174
}, testutil .WaitShort , testutil .IntervalFast , "user latency is missing" )
182
175
183
- require .Len (t , userLatencies .Report .Users , 2 , "want only 2 users" )
176
+ // We got our latency data, close the connection.
177
+ _ = sshConn .Close ()
178
+
179
+ require .Len (t , userLatencies .Report .Users , 1 , "want only 1 user" )
180
+ require .Equal (t , userLatencies .Report .Users [0 ].UserID , user .UserID , "want user id to match" )
184
181
assert .Greater (t , userLatencies .Report .Users [0 ].LatencyMS .P50 , float64 (0 ), "want p50 to be greater than 0" )
185
182
assert .Greater (t , userLatencies .Report .Users [0 ].LatencyMS .P95 , float64 (0 ), "want p95 to be greater than 0" )
186
- assert .Nil (t , userLatencies .Report .Users [1 ].LatencyMS , "want user 2 to have no latency" )
187
183
}
188
184
189
185
func TestUserLatencyInsights_BadRequest (t * testing.T ) {
@@ -192,7 +188,7 @@ func TestUserLatencyInsights_BadRequest(t *testing.T) {
192
188
client := coderdtest .New (t , & coderdtest.Options {})
193
189
_ = coderdtest .CreateFirstUser (t , client )
194
190
195
- y , m , d := time .Now ().Date ()
191
+ y , m , d := time .Now ().UTC (). Date ()
196
192
today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
197
193
198
194
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -235,6 +231,7 @@ func TestTemplateInsights(t *testing.T) {
235
231
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
236
232
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
237
233
234
+ // Start an agent so that we can generate stats.
238
235
agentClient := agentsdk .New (client .URL )
239
236
agentClient .SetSessionToken (authToken )
240
237
agentCloser := agent .New (agent.Options {
@@ -246,12 +243,15 @@ func TestTemplateInsights(t *testing.T) {
246
243
}()
247
244
resources := coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
248
245
249
- y , m , d := time .Now ().Date ()
246
+ // Start must be at the beginning of the day, initialize it early in case
247
+ // the day changes so that we get the relevant stats faster.
248
+ y , m , d := time .Now ().UTC ().Date ()
250
249
today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
251
250
252
251
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
253
252
defer cancel ()
254
253
254
+ // Connect to the agent to generate usage/latency stats.
255
255
conn , err := client .DialWorkspaceAgent (ctx , resources [0 ].Agents [0 ].ID , & codersdk.DialWorkspaceAgentOptions {
256
256
Logger : logger .Named ("client" ),
257
257
})
@@ -262,18 +262,19 @@ func TestTemplateInsights(t *testing.T) {
262
262
require .NoError (t , err )
263
263
defer sshConn .Close ()
264
264
265
+ // Start an SSH session to generate SSH usage stats.
265
266
sess , err := sshConn .NewSession ()
266
267
require .NoError (t , err )
267
268
defer sess .Close ()
268
269
269
- // Keep SSH session open for long enough to generate insights.
270
270
r , w := io .Pipe ()
271
271
defer r .Close ()
272
272
defer w .Close ()
273
273
sess .Stdin = r
274
274
err = sess .Start ("cat" )
275
275
require .NoError (t , err )
276
276
277
+ // Start an rpty session to generate rpty usage stats.
277
278
rpty , err := client .WorkspaceAgentReconnectingPTY (ctx , codersdk.WorkspaceAgentReconnectingPTYOpts {
278
279
AgentID : resources [0 ].Agents [0 ].ID ,
279
280
Reconnect : uuid .New (),
@@ -289,7 +290,7 @@ func TestTemplateInsights(t *testing.T) {
289
290
return func () bool {
290
291
req = codersdk.TemplateInsightsRequest {
291
292
StartTime : today ,
292
- EndTime : time .Now ().Truncate (time .Hour ).Add (time .Hour ),
293
+ EndTime : time .Now ().UTC (). Truncate (time .Hour ).Add (time .Hour ),
293
294
Interval : codersdk .InsightsReportIntervalDay ,
294
295
}
295
296
resp , err = client .TemplateInsights (ctx , req )
@@ -308,6 +309,7 @@ func TestTemplateInsights(t *testing.T) {
308
309
require .Eventually (t , waitForAppSeconds ("reconnecting-pty" ), testutil .WaitShort , testutil .IntervalFast , "reconnecting-pty seconds missing" )
309
310
require .Eventually (t , waitForAppSeconds ("ssh" ), testutil .WaitShort , testutil .IntervalFast , "ssh seconds missing" )
310
311
312
+ // We got our data, close down sessions and connections.
311
313
_ = rpty .Close ()
312
314
_ = sess .Close ()
313
315
_ = sshConn .Close ()
@@ -335,7 +337,7 @@ func TestTemplateInsights_BadRequest(t *testing.T) {
335
337
client := coderdtest .New (t , & coderdtest.Options {})
336
338
_ = coderdtest .CreateFirstUser (t , client )
337
339
338
- y , m , d := time .Now ().Date ()
340
+ y , m , d := time .Now ().UTC (). Date ()
339
341
today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
340
342
341
343
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
0 commit comments