Skip to content

Commit b2ce1dd

Browse files
committed
🧪
1 parent 4a9d862 commit b2ce1dd

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

coderd/agentapi/stats_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/coder/coder/v2/coderd/database"
2020
"github.com/coder/coder/v2/coderd/database/dbmock"
2121
"github.com/coder/coder/v2/coderd/database/dbtime"
22+
"github.com/coder/coder/v2/coderd/database/pubsub"
2223
"github.com/coder/coder/v2/coderd/prometheusmetrics"
2324
"github.com/coder/coder/v2/coderd/schedule"
2425
)
@@ -78,8 +79,10 @@ func TestUpdateStates(t *testing.T) {
7879
t.Parallel()
7980

8081
var (
81-
now = dbtime.Now()
82-
dbM = dbmock.NewMockStore(gomock.NewController(t))
82+
now = dbtime.Now()
83+
db = dbmock.NewMockStore(gomock.NewController(t))
84+
ps = pubsub.NewInMemory()
85+
8386
templateScheduleStore = schedule.MockTemplateScheduleStore{
8487
GetFn: func(context.Context, database.Store, uuid.UUID) (schedule.TemplateScheduleOptions, error) {
8588
panic("should not be called")
@@ -124,7 +127,8 @@ func TestUpdateStates(t *testing.T) {
124127
AgentFn: func(context.Context) (database.WorkspaceAgent, error) {
125128
return agent, nil
126129
},
127-
Database: dbM,
130+
Database: db,
131+
Pubsub: ps,
128132
StatsBatcher: batcher,
129133
TemplateScheduleStore: templateScheduleStorePtr(templateScheduleStore),
130134
AgentStatsRefreshInterval: 10 * time.Second,
@@ -144,25 +148,25 @@ func TestUpdateStates(t *testing.T) {
144148
}
145149

146150
// Workspace gets fetched.
147-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
151+
db.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
148152
Workspace: workspace,
149153
TemplateName: template.Name,
150154
}, nil)
151155

152156
// We expect an activity bump because ConnectionCount > 0.
153-
dbM.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
157+
db.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
154158
WorkspaceID: workspace.ID,
155159
NextAutostart: time.Time{}.UTC(),
156160
}).Return(nil)
157161

158162
// Workspace last used at gets bumped.
159-
dbM.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
163+
db.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
160164
ID: workspace.ID,
161165
LastUsedAt: now,
162166
}).Return(nil)
163167

164168
// User gets fetched to hit the UpdateAgentMetricsFn.
165-
dbM.EXPECT().GetUserByID(gomock.Any(), user.ID).Return(user, nil)
169+
db.EXPECT().GetUserByID(gomock.Any(), user.ID).Return(user, nil)
166170

167171
resp, err := api.UpdateStats(context.Background(), req)
168172
require.NoError(t, err)
@@ -188,7 +192,8 @@ func TestUpdateStates(t *testing.T) {
188192

189193
var (
190194
now = dbtime.Now()
191-
dbM = dbmock.NewMockStore(gomock.NewController(t))
195+
db = dbmock.NewMockStore(gomock.NewController(t))
196+
ps = pubsub.NewInMemory()
192197
templateScheduleStore = schedule.MockTemplateScheduleStore{
193198
GetFn: func(context.Context, database.Store, uuid.UUID) (schedule.TemplateScheduleOptions, error) {
194199
panic("should not be called")
@@ -213,7 +218,8 @@ func TestUpdateStates(t *testing.T) {
213218
AgentFn: func(context.Context) (database.WorkspaceAgent, error) {
214219
return agent, nil
215220
},
216-
Database: dbM,
221+
Database: db,
222+
Pubsub: ps,
217223
StatsBatcher: batcher,
218224
TemplateScheduleStore: templateScheduleStorePtr(templateScheduleStore),
219225
AgentStatsRefreshInterval: 10 * time.Second,
@@ -225,13 +231,13 @@ func TestUpdateStates(t *testing.T) {
225231
}
226232

227233
// Workspace gets fetched.
228-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
234+
db.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
229235
Workspace: workspace,
230236
TemplateName: template.Name,
231237
}, nil)
232238

233239
// Workspace last used at gets bumped.
234-
dbM.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
240+
db.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
235241
ID: workspace.ID,
236242
LastUsedAt: now,
237243
}).Return(nil)
@@ -244,7 +250,8 @@ func TestUpdateStates(t *testing.T) {
244250
t.Parallel()
245251

246252
var (
247-
dbM = dbmock.NewMockStore(gomock.NewController(t))
253+
db = dbmock.NewMockStore(gomock.NewController(t))
254+
ps = pubsub.NewInMemory()
248255
req = &agentproto.UpdateStatsRequest{
249256
Stats: &agentproto.Stats{
250257
ConnectionsByProto: map[string]int64{}, // len() == 0
@@ -255,7 +262,8 @@ func TestUpdateStates(t *testing.T) {
255262
AgentFn: func(context.Context) (database.WorkspaceAgent, error) {
256263
return agent, nil
257264
},
258-
Database: dbM,
265+
Database: db,
266+
Pubsub: ps,
259267
StatsBatcher: nil, // should not be called
260268
TemplateScheduleStore: nil, // should not be called
261269
AgentStatsRefreshInterval: 10 * time.Second,
@@ -290,7 +298,8 @@ func TestUpdateStates(t *testing.T) {
290298
nextAutostart := now.Add(30 * time.Minute).UTC() // always sent to DB as UTC
291299

292300
var (
293-
dbM = dbmock.NewMockStore(gomock.NewController(t))
301+
db = dbmock.NewMockStore(gomock.NewController(t))
302+
ps = pubsub.NewInMemory()
294303
templateScheduleStore = schedule.MockTemplateScheduleStore{
295304
GetFn: func(context.Context, database.Store, uuid.UUID) (schedule.TemplateScheduleOptions, error) {
296305
return schedule.TemplateScheduleOptions{
@@ -321,7 +330,8 @@ func TestUpdateStates(t *testing.T) {
321330
AgentFn: func(context.Context) (database.WorkspaceAgent, error) {
322331
return agent, nil
323332
},
324-
Database: dbM,
333+
Database: db,
334+
Pubsub: ps,
325335
StatsBatcher: batcher,
326336
TemplateScheduleStore: templateScheduleStorePtr(templateScheduleStore),
327337
AgentStatsRefreshInterval: 15 * time.Second,
@@ -341,26 +351,26 @@ func TestUpdateStates(t *testing.T) {
341351
}
342352

343353
// Workspace gets fetched.
344-
dbM.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
354+
db.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(database.GetWorkspaceByAgentIDRow{
345355
Workspace: workspace,
346356
TemplateName: template.Name,
347357
}, nil)
348358

349359
// We expect an activity bump because ConnectionCount > 0. However, the
350360
// next autostart time will be set on the bump.
351-
dbM.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
361+
db.EXPECT().ActivityBumpWorkspace(gomock.Any(), database.ActivityBumpWorkspaceParams{
352362
WorkspaceID: workspace.ID,
353363
NextAutostart: nextAutostart,
354364
}).Return(nil)
355365

356366
// Workspace last used at gets bumped.
357-
dbM.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
367+
db.EXPECT().UpdateWorkspaceLastUsedAt(gomock.Any(), database.UpdateWorkspaceLastUsedAtParams{
358368
ID: workspace.ID,
359369
LastUsedAt: now,
360370
}).Return(nil)
361371

362372
// User gets fetched to hit the UpdateAgentMetricsFn.
363-
dbM.EXPECT().GetUserByID(gomock.Any(), user.ID).Return(user, nil)
373+
db.EXPECT().GetUserByID(gomock.Any(), user.ID).Return(user, nil)
364374

365375
resp, err := api.UpdateStats(context.Background(), req)
366376
require.NoError(t, err)

site/src/utils/schedule.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const autostopDisplay = (
114114
const deadline = dayjs(workspace.latest_build.deadline).tz(
115115
dayjs.tz.guess(),
116116
);
117-
const now = dayjs(workspace.latest_build.deadline).utc();
117+
const now = dayjs(workspace.latest_build.deadline);
118118
if (isShuttingDown(workspace, deadline)) {
119119
return {
120120
message: Language.workspaceShuttingDownLabel,

0 commit comments

Comments
 (0)