@@ -335,7 +335,7 @@ func TestTaskRunner_Restore_Running(t *testing.T) {
335
335
defer newTR .Kill (context .Background (), structs .NewTaskEvent ("cleanup" ))
336
336
337
337
// Wait for new task runner to exit when the process does
338
- <- newTR . WaitCh ( )
338
+ testWaitForTaskToDie ( t , newTR )
339
339
340
340
// Assert that the process was only started once
341
341
started := 0
@@ -603,11 +603,7 @@ func TestTaskRunner_TaskEnv_Interpolated(t *testing.T) {
603
603
defer cleanup ()
604
604
605
605
// Wait for task to complete
606
- select {
607
- case <- tr .WaitCh ():
608
- case <- time .After (3 * time .Second ):
609
- require .Fail ("timeout waiting for task to exit" )
610
- }
606
+ testWaitForTaskToDie (t , tr )
611
607
612
608
// Get the mock driver plugin
613
609
driverPlugin , err := conf .DriverManager .Dispense (mockdriver .PluginID .Name )
@@ -654,7 +650,9 @@ func TestTaskRunner_TaskEnv_Chroot(t *testing.T) {
654
650
go tr .Run ()
655
651
defer tr .Kill (context .Background (), structs .NewTaskEvent ("cleanup" ))
656
652
657
- // Wait for task to exit
653
+ // Wait for task to exit and kill the task runner to run the stop hooks.
654
+ testWaitForTaskToDie (t , tr )
655
+ tr .Kill (context .Background (), structs .NewTaskEvent ("kill" ))
658
656
timeout := 15 * time .Second
659
657
if testutil .IsCI () {
660
658
timeout = 120 * time .Second
@@ -703,7 +701,9 @@ func TestTaskRunner_TaskEnv_Image(t *testing.T) {
703
701
tr , conf , cleanup := runTestTaskRunner (t , alloc , task .Name )
704
702
defer cleanup ()
705
703
706
- // Wait for task to exit
704
+ // Wait for task to exit and kill task runner to run the stop hooks.
705
+ testWaitForTaskToDie (t , tr )
706
+ tr .Kill (context .Background (), structs .NewTaskEvent ("kill" ))
707
707
select {
708
708
case <- tr .WaitCh ():
709
709
case <- time .After (15 * time .Second ):
@@ -750,7 +750,9 @@ func TestTaskRunner_TaskEnv_None(t *testing.T) {
750
750
%s
751
751
` , root , taskDir , taskDir , os .Getenv ("PATH" ))
752
752
753
- // Wait for task to exit
753
+ // Wait for task to exit and kill the task runner to run the stop hooks.
754
+ testWaitForTaskToDie (t , tr )
755
+ tr .Kill (context .Background (), structs .NewTaskEvent ("kill" ))
754
756
select {
755
757
case <- tr .WaitCh ():
756
758
case <- time .After (15 * time .Second ):
@@ -818,10 +820,7 @@ func TestTaskRunner_DevicePropogation(t *testing.T) {
818
820
defer tr .Kill (context .Background (), structs .NewTaskEvent ("cleanup" ))
819
821
820
822
// Wait for task to complete
821
- select {
822
- case <- tr .WaitCh ():
823
- case <- time .After (3 * time .Second ):
824
- }
823
+ testWaitForTaskToDie (t , tr )
825
824
826
825
// Get the mock driver plugin
827
826
driverPlugin , err := conf .DriverManager .Dispense (mockdriver .PluginID .Name )
@@ -1306,15 +1305,15 @@ func TestTaskRunner_CheckWatcher_Restart(t *testing.T) {
1306
1305
"Received" ,
1307
1306
"Task Setup" ,
1308
1307
"Started" ,
1309
- "Restart Signaled" ,
1308
+ "Restart Running Signaled" ,
1310
1309
"Terminated" ,
1311
1310
"Restarting" ,
1312
1311
"Started" ,
1313
- "Restart Signaled" ,
1312
+ "Restart Running Signaled" ,
1314
1313
"Terminated" ,
1315
1314
"Restarting" ,
1316
1315
"Started" ,
1317
- "Restart Signaled" ,
1316
+ "Restart Running Signaled" ,
1318
1317
"Terminated" ,
1319
1318
"Not Restarting" ,
1320
1319
}
@@ -1328,11 +1327,7 @@ func TestTaskRunner_CheckWatcher_Restart(t *testing.T) {
1328
1327
// Wait until the task exits. Don't simply wait for it to run as it may
1329
1328
// get restarted and terminated before the test is able to observe it
1330
1329
// running.
1331
- select {
1332
- case <- tr .WaitCh ():
1333
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1334
- require .Fail (t , "timeout" )
1335
- }
1330
+ testWaitForTaskToDie (t , tr )
1336
1331
1337
1332
state := tr .TaskState ()
1338
1333
actualEvents := make ([]string , len (state .Events ))
@@ -1421,11 +1416,7 @@ func TestTaskRunner_BlockForSIDSToken(t *testing.T) {
1421
1416
1422
1417
// task runner should exit now that it has been unblocked and it is a batch
1423
1418
// job with a zero sleep time
1424
- select {
1425
- case <- tr .WaitCh ():
1426
- case <- time .After (15 * time .Second * time .Duration (testutil .TestMultiplier ())):
1427
- r .Fail ("timed out waiting for batch task to exist" )
1428
- }
1419
+ testWaitForTaskToDie (t , tr )
1429
1420
1430
1421
// assert task exited successfully
1431
1422
finalState := tr .TaskState ()
@@ -1478,11 +1469,7 @@ func TestTaskRunner_DeriveSIToken_Retry(t *testing.T) {
1478
1469
go tr .Run ()
1479
1470
1480
1471
// assert task runner blocks on SI token
1481
- select {
1482
- case <- tr .WaitCh ():
1483
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1484
- r .Fail ("timed out waiting for task runner" )
1485
- }
1472
+ testWaitForTaskToDie (t , tr )
1486
1473
1487
1474
// assert task exited successfully
1488
1475
finalState := tr .TaskState ()
@@ -1598,11 +1585,7 @@ func TestTaskRunner_BlockForVaultToken(t *testing.T) {
1598
1585
1599
1586
// TR should exit now that it's unblocked by vault as its a batch job
1600
1587
// with 0 sleeping.
1601
- select {
1602
- case <- tr .WaitCh ():
1603
- case <- time .After (15 * time .Second * time .Duration (testutil .TestMultiplier ())):
1604
- require .Fail (t , "timed out waiting for batch task to exit" )
1605
- }
1588
+ testWaitForTaskToDie (t , tr )
1606
1589
1607
1590
// Assert task exited successfully
1608
1591
finalState := tr .TaskState ()
@@ -1615,6 +1598,14 @@ func TestTaskRunner_BlockForVaultToken(t *testing.T) {
1615
1598
require .NoError (t , err )
1616
1599
require .Equal (t , token , string (data ))
1617
1600
1601
+ // Kill task runner to trigger stop hooks
1602
+ tr .Kill (context .Background (), structs .NewTaskEvent ("kill" ))
1603
+ select {
1604
+ case <- tr .WaitCh ():
1605
+ case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1606
+ require .Fail (t , "timed out waiting for task runner to exit" )
1607
+ }
1608
+
1618
1609
// Check the token was revoked
1619
1610
testutil .WaitForResult (func () (bool , error ) {
1620
1611
if len (vaultClient .StoppedTokens ()) != 1 {
@@ -1661,17 +1652,21 @@ func TestTaskRunner_DeriveToken_Retry(t *testing.T) {
1661
1652
defer tr .Kill (context .Background (), structs .NewTaskEvent ("cleanup" ))
1662
1653
go tr .Run ()
1663
1654
1664
- // Wait for TR to exit and check its state
1655
+ // Wait for TR to die and check its state
1656
+ testWaitForTaskToDie (t , tr )
1657
+
1658
+ state := tr .TaskState ()
1659
+ require .Equal (t , structs .TaskStateDead , state .State )
1660
+ require .False (t , state .Failed )
1661
+
1662
+ // Kill task runner to trigger stop hooks
1663
+ tr .Kill (context .Background (), structs .NewTaskEvent ("kill" ))
1665
1664
select {
1666
1665
case <- tr .WaitCh ():
1667
1666
case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1668
1667
require .Fail (t , "timed out waiting for task runner to exit" )
1669
1668
}
1670
1669
1671
- state := tr .TaskState ()
1672
- require .Equal (t , structs .TaskStateDead , state .State )
1673
- require .False (t , state .Failed )
1674
-
1675
1670
require .Equal (t , 1 , count )
1676
1671
1677
1672
// Check that the token is on disk
@@ -1771,11 +1766,7 @@ func TestTaskRunner_Download_ChrootExec(t *testing.T) {
1771
1766
defer cleanup ()
1772
1767
1773
1768
// Wait for task to run and exit
1774
- select {
1775
- case <- tr .WaitCh ():
1776
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1777
- require .Fail (t , "timed out waiting for task runner to exit" )
1778
- }
1769
+ testWaitForTaskToDie (t , tr )
1779
1770
1780
1771
state := tr .TaskState ()
1781
1772
require .Equal (t , structs .TaskStateDead , state .State )
@@ -1816,11 +1807,7 @@ func TestTaskRunner_Download_RawExec(t *testing.T) {
1816
1807
defer cleanup ()
1817
1808
1818
1809
// Wait for task to run and exit
1819
- select {
1820
- case <- tr .WaitCh ():
1821
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1822
- require .Fail (t , "timed out waiting for task runner to exit" )
1823
- }
1810
+ testWaitForTaskToDie (t , tr )
1824
1811
1825
1812
state := tr .TaskState ()
1826
1813
require .Equal (t , structs .TaskStateDead , state .State )
@@ -1851,11 +1838,7 @@ func TestTaskRunner_Download_List(t *testing.T) {
1851
1838
defer cleanup ()
1852
1839
1853
1840
// Wait for task to run and exit
1854
- select {
1855
- case <- tr .WaitCh ():
1856
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1857
- require .Fail (t , "timed out waiting for task runner to exit" )
1858
- }
1841
+ testWaitForTaskToDie (t , tr )
1859
1842
1860
1843
state := tr .TaskState ()
1861
1844
require .Equal (t , structs .TaskStateDead , state .State )
@@ -1902,11 +1885,7 @@ func TestTaskRunner_Download_Retries(t *testing.T) {
1902
1885
tr , _ , cleanup := runTestTaskRunner (t , alloc , task .Name )
1903
1886
defer cleanup ()
1904
1887
1905
- select {
1906
- case <- tr .WaitCh ():
1907
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
1908
- require .Fail (t , "timed out waiting for task to exit" )
1909
- }
1888
+ testWaitForTaskToDie (t , tr )
1910
1889
1911
1890
state := tr .TaskState ()
1912
1891
require .Equal (t , structs .TaskStateDead , state .State )
@@ -2100,6 +2079,8 @@ func TestTaskRunner_RestartSignalTask_NotRunning(t *testing.T) {
2100
2079
case <- time .After (1 * time .Second ):
2101
2080
}
2102
2081
2082
+ require .Equal (t , structs .TaskStatePending , tr .TaskState ().State )
2083
+
2103
2084
// Send a signal and restart
2104
2085
err = tr .Signal (structs .NewTaskEvent ("don't panic" ), "QUIT" )
2105
2086
require .EqualError (t , err , ErrTaskNotRunning .Error ())
@@ -2110,12 +2091,7 @@ func TestTaskRunner_RestartSignalTask_NotRunning(t *testing.T) {
2110
2091
2111
2092
// Unblock and let it finish
2112
2093
waitCh <- struct {}{}
2113
-
2114
- select {
2115
- case <- tr .WaitCh ():
2116
- case <- time .After (10 * time .Second ):
2117
- require .Fail (t , "timed out waiting for task to complete" )
2118
- }
2094
+ testWaitForTaskToDie (t , tr )
2119
2095
2120
2096
// Assert the task ran and never restarted
2121
2097
state := tr .TaskState ()
@@ -2153,11 +2129,7 @@ func TestTaskRunner_Run_RecoverableStartError(t *testing.T) {
2153
2129
tr , _ , cleanup := runTestTaskRunner (t , alloc , task .Name )
2154
2130
defer cleanup ()
2155
2131
2156
- select {
2157
- case <- tr .WaitCh ():
2158
- case <- time .After (time .Duration (testutil .TestMultiplier ()* 15 ) * time .Second ):
2159
- require .Fail (t , "timed out waiting for task to exit" )
2160
- }
2132
+ testWaitForTaskToDie (t , tr )
2161
2133
2162
2134
state := tr .TaskState ()
2163
2135
require .Equal (t , structs .TaskStateDead , state .State )
@@ -2202,11 +2174,7 @@ func TestTaskRunner_Template_Artifact(t *testing.T) {
2202
2174
go tr .Run ()
2203
2175
2204
2176
// Wait for task to run and exit
2205
- select {
2206
- case <- tr .WaitCh ():
2207
- case <- time .After (15 * time .Second * time .Duration (testutil .TestMultiplier ())):
2208
- require .Fail (t , "timed out waiting for task runner to exit" )
2209
- }
2177
+ testWaitForTaskToDie (t , tr )
2210
2178
2211
2179
state := tr .TaskState ()
2212
2180
require .Equal (t , structs .TaskStateDead , state .State )
@@ -2536,7 +2504,9 @@ func TestTaskRunner_UnregisterConsul_Retries(t *testing.T) {
2536
2504
tr , err := NewTaskRunner (conf )
2537
2505
require .NoError (t , err )
2538
2506
defer tr .Kill (context .Background (), structs .NewTaskEvent ("cleanup" ))
2539
- tr .Run ()
2507
+ go tr .Run ()
2508
+
2509
+ testWaitForTaskToDie (t , tr )
2540
2510
2541
2511
state := tr .TaskState ()
2542
2512
require .Equal (t , structs .TaskStateDead , state .State )
@@ -2562,7 +2532,17 @@ func TestTaskRunner_UnregisterConsul_Retries(t *testing.T) {
2562
2532
func testWaitForTaskToStart (t * testing.T , tr * TaskRunner ) {
2563
2533
testutil .WaitForResult (func () (bool , error ) {
2564
2534
ts := tr .TaskState ()
2565
- return ts .State == structs .TaskStateRunning , fmt .Errorf ("%v" , ts .State )
2535
+ return ts .State == structs .TaskStateRunning , fmt .Errorf ("expected task to be runnig, got %v" , ts .State )
2536
+ }, func (err error ) {
2537
+ require .NoError (t , err )
2538
+ })
2539
+ }
2540
+
2541
+ // testWaitForTaskToDie waits for the task to die or fails the test
2542
+ func testWaitForTaskToDie (t * testing.T , tr * TaskRunner ) {
2543
+ testutil .WaitForResult (func () (bool , error ) {
2544
+ ts := tr .TaskState ()
2545
+ return ts .State == structs .TaskStateDead , fmt .Errorf ("expected task to be dead, got %v" , ts .State )
2566
2546
}, func (err error ) {
2567
2547
require .NoError (t , err )
2568
2548
})
0 commit comments