Skip to content

Commit 30402b9

Browse files
committed
Add async background test
1 parent e94ba7f commit 30402b9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/AsyncTaskTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ public function testAsyncBasic()
6767

6868
unlink($testFileName);
6969
}
70+
71+
public function testAsyncBackground()
72+
{
73+
// tests that the async really runs in the background: it should not block the main thread
74+
// test by starting a long sleep task and check the elapsed time in the main process
75+
// note: we cannot test the "nohup" part because we can't really kill phpunit and start it up again on demand
76+
$sleepDuration = 2; // how many seconds
77+
$task = new AsyncTask(function () use ($sleepDuration) {
78+
// just sleep long is ok
79+
sleep($sleepDuration);
80+
});
81+
// time it
82+
$timeBefore = microtime(true);
83+
$task->start();
84+
$timeAfter = microtime(true);
85+
$timeElapsed = $timeAfter - $timeBefore;
86+
$this->assertLessThan($sleepDuration, $timeElapsed, "The async task probably did not start in the background because the time taken to start it was too long.");
87+
}
7088
}

0 commit comments

Comments
 (0)