@@ -219,6 +219,59 @@ func Test_ServicesStateWithData(t *testing.T) {
219
219
}
220
220
So (pendingBroadcast , ShouldBeFalse )
221
221
})
222
+
223
+ Convey ("Sets a service's status to DRAINING" , func () {
224
+ state .AddServiceEntry (svc )
225
+
226
+ svc .Status = service .DRAINING
227
+ svc .Updated = time .Now ().UTC ()
228
+
229
+ state .AddServiceEntry (svc )
230
+
231
+ So (state .HasServer (anotherHostname ), ShouldBeTrue )
232
+ So (state .Servers [anotherHostname ].Services [svc .ID ].Status ,
233
+ ShouldEqual , service .DRAINING )
234
+ })
235
+
236
+ Convey ("Doesn't mark a DRAINING service as ALIVE" , func () {
237
+ svc .Status = service .DRAINING
238
+ state .AddServiceEntry (svc )
239
+
240
+ svc .Status = service .ALIVE
241
+ svc .Updated = time .Now ().UTC ()
242
+
243
+ state .AddServiceEntry (svc )
244
+
245
+ So (state .HasServer (anotherHostname ), ShouldBeTrue )
246
+ So (state .Servers [anotherHostname ].Services [svc .ID ].Status ,
247
+ ShouldEqual , service .DRAINING )
248
+ })
249
+ })
250
+
251
+ Convey ("GetLocalServiceByID()" , func () {
252
+ Convey ("Returns an existing service on the current host" , func () {
253
+ state .Hostname = anotherHostname
254
+ state .AddServiceEntry (svc )
255
+
256
+ returnedSvc , err := state .GetLocalServiceByID (svc .ID )
257
+ So (err , ShouldBeNil )
258
+ So (returnedSvc .ID , ShouldEqual , svc .ID )
259
+ })
260
+
261
+ Convey ("Doesn't return a service running on other hosts" , func () {
262
+ state .AddServiceEntry (svc )
263
+
264
+ _ , err := state .GetLocalServiceByID (svc .ID )
265
+ So (err , ShouldNotBeNil )
266
+ })
267
+
268
+ Convey ("Returns an error for a non-existent service ID" , func () {
269
+ state .AddServiceEntry (svc )
270
+
271
+ _ , err := state .GetLocalServiceByID ("missing" )
272
+ So (err , ShouldNotBeNil )
273
+ So (err .Error (), ShouldContainSubstring , "not found" )
274
+ })
222
275
})
223
276
224
277
Convey ("Merge() merges state we care about from other state structs" , func () {
@@ -416,6 +469,36 @@ func Test_TrackingAndBroadcasting(t *testing.T) {
416
469
So (state .Servers [hostname ].LastChanged .After (lastChanged ), ShouldBeTrue )
417
470
})
418
471
472
+ Convey ("Draining services have a lifespan and then are tombstoned" , func () {
473
+ lastChanged := state .Servers [hostname ].LastChanged
474
+ service1 .Status = service .DRAINING
475
+ state .AddServiceEntry (service1 )
476
+ svc := state .Servers [hostname ].Services [service1 .ID ]
477
+ stamp := service1 .Updated .Add (0 - DRAINING_LIFESPAN - 5 * time .Second )
478
+ svc .Updated = stamp
479
+
480
+ state .TombstoneOthersServices ()
481
+
482
+ So (svc .Status , ShouldEqual , service .TOMBSTONE )
483
+ So (svc .Updated , ShouldBeTheSameTimeAs , stamp .Add (time .Second ))
484
+ So (state .Servers [hostname ].LastChanged .After (lastChanged ), ShouldBeTrue )
485
+ })
486
+
487
+ Convey ("Draining services are not tombstoned before their lifespan expires" , func () {
488
+ lastChanged := state .Servers [hostname ].LastChanged
489
+ service1 .Status = service .DRAINING
490
+ state .AddServiceEntry (service1 )
491
+ svc := state .Servers [hostname ].Services [service1 .ID ]
492
+ stamp := service1 .Updated .Add (0 - ALIVE_LIFESPAN - 5 * time .Second )
493
+ svc .Updated = stamp
494
+
495
+ state .TombstoneOthersServices ()
496
+
497
+ So (svc .Status , ShouldEqual , service .DRAINING )
498
+ So (svc .Updated , ShouldBeTheSameTimeAs , stamp )
499
+ So (state .Servers [hostname ].LastChanged .After (lastChanged ), ShouldBeTrue )
500
+ })
501
+
419
502
Convey ("Unhealthy/Unknown services have a lifespan and then are tombstoned" , func () {
420
503
unhealthyService := service.Service {ID : "unhealthy_shakespeare" , Hostname : hostname , Updated : baseTime , Status : service .UNHEALTHY }
421
504
state .AddServiceEntry (unhealthyService )
0 commit comments