@@ -1614,3 +1614,67 @@ func TestTaskRunner_Pre06ScriptCheck(t *testing.T) {
1614
1614
t .Run (run ("0.5.6" , "java" , "tcp" , false ))
1615
1615
t .Run (run ("0.5.6" , "mock_driver" , "tcp" , false ))
1616
1616
}
1617
+
1618
+ func TestTaskRunner_ShutdownDelay (t * testing.T ) {
1619
+ t .Parallel ()
1620
+
1621
+ alloc := mock .Alloc ()
1622
+ task := alloc .Job .TaskGroups [0 ].Tasks [0 ]
1623
+ task .Driver = "mock_driver"
1624
+ task .Config = map [string ]interface {}{
1625
+ "run_for" : "1000s" ,
1626
+ }
1627
+
1628
+ // No shutdown escape hatch for this delay, so don't set it too high
1629
+ task .ShutdownDelay = 500 * time .Duration (testutil .TestMultiplier ()) * time .Millisecond
1630
+
1631
+ ctx := testTaskRunnerFromAlloc (t , true , alloc )
1632
+ ctx .tr .MarkReceived ()
1633
+ go ctx .tr .Run ()
1634
+ defer ctx .Cleanup ()
1635
+
1636
+ // Wait for the task to start
1637
+ testWaitForTaskToStart (t , ctx )
1638
+
1639
+ // Begin the tear down
1640
+ ctx .tr .Destroy (structs .NewTaskEvent (structs .TaskKilled ))
1641
+ destroyed := time .Now ()
1642
+
1643
+ // Service should get removed quickly; loop until RemoveTask is called
1644
+ found := false
1645
+ mockConsul := ctx .tr .consul .(* mockConsulServiceClient )
1646
+ deadline := destroyed .Add (task .ShutdownDelay )
1647
+ for time .Now ().Before (deadline ) {
1648
+ time .Sleep (5 * time .Millisecond )
1649
+
1650
+ mockConsul .mu .Lock ()
1651
+ n := len (mockConsul .ops )
1652
+ if n < 2 {
1653
+ mockConsul .mu .Unlock ()
1654
+ continue
1655
+ }
1656
+
1657
+ lastOp := mockConsul .ops [n - 1 ].op
1658
+ mockConsul .mu .Unlock ()
1659
+
1660
+ if lastOp == "remove" {
1661
+ found = true
1662
+ break
1663
+ }
1664
+ }
1665
+ if ! found {
1666
+ t .Errorf ("task was not removed from Consul first" )
1667
+ }
1668
+
1669
+ // Wait for actual exit
1670
+ select {
1671
+ case <- ctx .tr .WaitCh ():
1672
+ case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1673
+ t .Fatalf ("timeout" )
1674
+ }
1675
+
1676
+ // It should be impossible to reach here in less time than the shutdown delay
1677
+ if time .Now ().Before (destroyed .Add (task .ShutdownDelay )) {
1678
+ t .Fatalf ("task exited before shutdown delay" )
1679
+ }
1680
+ }
0 commit comments