8
8
"testing"
9
9
"time"
10
10
11
- "github.com/golang/mock/gomock"
12
11
"github.com/google/uuid"
13
12
"github.com/stretchr/testify/require"
13
+ "go.uber.org/mock/gomock"
14
14
"google.golang.org/protobuf/types/known/timestamppb"
15
15
16
16
"cdr.dev/slog/sloggers/slogtest"
@@ -64,7 +64,7 @@ func TestUpdateLifecycle(t *testing.T) {
64
64
ReadyAt : sql.NullTime {Valid : false },
65
65
}).Return (nil )
66
66
67
- var publishCalled int64
67
+ publishCalled := false
68
68
api := & agentapi.LifecycleAPI {
69
69
AgentFn : func (ctx context.Context ) (database.WorkspaceAgent , error ) {
70
70
return agentCreated , nil
@@ -75,7 +75,7 @@ func TestUpdateLifecycle(t *testing.T) {
75
75
Database : dbM ,
76
76
Log : slogtest .Make (t , nil ),
77
77
PublishWorkspaceUpdateFn : func (ctx context.Context , agent * database.WorkspaceAgent ) error {
78
- atomic . AddInt64 ( & publishCalled , 1 )
78
+ publishCalled = true
79
79
return nil
80
80
},
81
81
}
@@ -85,7 +85,7 @@ func TestUpdateLifecycle(t *testing.T) {
85
85
})
86
86
require .NoError (t , err )
87
87
require .Equal (t , lifecycle , resp )
88
- require .Equal (t , int64 ( 1 ), atomic . LoadInt64 ( & publishCalled ) )
88
+ require .True (t , publishCalled )
89
89
})
90
90
91
91
t .Run ("OKReadying" , func (t * testing.T ) {
@@ -151,7 +151,7 @@ func TestUpdateLifecycle(t *testing.T) {
151
151
},
152
152
}).Return (nil )
153
153
154
- var publishCalled int64
154
+ publishCalled := false
155
155
api := & agentapi.LifecycleAPI {
156
156
AgentFn : func (ctx context.Context ) (database.WorkspaceAgent , error ) {
157
157
return agentCreated , nil
@@ -162,7 +162,7 @@ func TestUpdateLifecycle(t *testing.T) {
162
162
Database : dbM ,
163
163
Log : slogtest .Make (t , nil ),
164
164
PublishWorkspaceUpdateFn : func (ctx context.Context , agent * database.WorkspaceAgent ) error {
165
- atomic . AddInt64 ( & publishCalled , 1 )
165
+ publishCalled = true
166
166
return nil
167
167
},
168
168
}
@@ -172,7 +172,7 @@ func TestUpdateLifecycle(t *testing.T) {
172
172
})
173
173
require .NoError (t , err )
174
174
require .Equal (t , lifecycle , resp )
175
- require .Equal (t , int64 ( 1 ), atomic . LoadInt64 ( & publishCalled ) )
175
+ require .True (t , publishCalled )
176
176
})
177
177
178
178
t .Run ("NoTimeSpecified" , func (t * testing.T ) {
@@ -263,19 +263,20 @@ func TestUpdateLifecycle(t *testing.T) {
263
263
}
264
264
for i , state := range states {
265
265
t .Log ("state" , state )
266
- now := now .Add (time .Hour * time .Duration (i ))
266
+ // Use a time after the last state change to ensure ordering.
267
+ stateNow := now .Add (time .Hour * time .Duration (i ))
267
268
lifecycle := & agentproto.Lifecycle {
268
269
State : state ,
269
- ChangedAt : timestamppb .New (now ),
270
+ ChangedAt : timestamppb .New (stateNow ),
270
271
}
271
272
272
273
expectedStartedAt := agent .StartedAt
273
274
expectedReadyAt := agent .ReadyAt
274
275
if state == agentproto .Lifecycle_STARTING {
275
- expectedStartedAt = sql.NullTime {Valid : true , Time : now }
276
+ expectedStartedAt = sql.NullTime {Valid : true , Time : stateNow }
276
277
}
277
278
if state == agentproto .Lifecycle_READY || state == agentproto .Lifecycle_START_ERROR {
278
- expectedReadyAt = sql.NullTime {Valid : true , Time : now }
279
+ expectedReadyAt = sql.NullTime {Valid : true , Time : stateNow }
279
280
}
280
281
281
282
dbM .EXPECT ().UpdateWorkspaceAgentLifecycleStateByID (gomock .Any (), database.UpdateWorkspaceAgentLifecycleStateByIDParams {
@@ -308,7 +309,7 @@ func TestUpdateLifecycle(t *testing.T) {
308
309
309
310
dbM := dbmock .NewMockStore (gomock .NewController (t ))
310
311
311
- var publishCalled int64
312
+ publishCalled := false
312
313
api := & agentapi.LifecycleAPI {
313
314
AgentFn : func (ctx context.Context ) (database.WorkspaceAgent , error ) {
314
315
return agentCreated , nil
@@ -319,7 +320,7 @@ func TestUpdateLifecycle(t *testing.T) {
319
320
Database : dbM ,
320
321
Log : slogtest .Make (t , nil ),
321
322
PublishWorkspaceUpdateFn : func (ctx context.Context , agent * database.WorkspaceAgent ) error {
322
- atomic . AddInt64 ( & publishCalled , 1 )
323
+ publishCalled = true
323
324
return nil
324
325
},
325
326
}
@@ -330,7 +331,7 @@ func TestUpdateLifecycle(t *testing.T) {
330
331
require .Error (t , err )
331
332
require .ErrorContains (t , err , "unknown lifecycle state" )
332
333
require .Nil (t , resp )
333
- require .Equal (t , int64 ( 0 ), atomic . LoadInt64 ( & publishCalled ) )
334
+ require .False (t , publishCalled )
334
335
})
335
336
}
336
337
0 commit comments