Skip to content

Commit 9d32cf7

Browse files
committed
Provide test cases for task status checking
1 parent 6e6e1db commit 9d32cf7

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/AsyncTaskStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function isRunning(): bool
7575
return false;
7676
}
7777
// prove it is running
78-
$isRunning = false;
78+
$isRunning = $this->proveTaskIsRunning();
7979
if (!$isRunning) {
8080
$this->isStopped = true;
8181
}

tests/AsyncTaskTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,31 @@ public function testAsyncTaskID()
231231
$taskStatus = new AsyncTaskStatus("");
232232
unset($taskStatus);
233233
}
234+
235+
public function testAsyncTaskNormalStatus()
236+
{
237+
// test that the task status reading is correct
238+
$taskID = "testingTask";
239+
$task = new AsyncTask(new SleepingAsyncTask(), taskID: $taskID);
240+
241+
// we haven't started yet, so this should say false
242+
$preStatus = new AsyncTaskStatus($taskID);
243+
$this->assertFalse($preStatus->isRunning());
244+
// note: since it detects false, it will always continue to say false
245+
$this->assertFalse($preStatus->isRunning());
246+
247+
// now we start running the task
248+
$liveStatus = $task->start();
249+
250+
// the task is to sleep for 2 seconds, and then exit.
251+
for ($i = 0; $i < 2; $i++) {
252+
// check the statuses; most likely still be running
253+
$this->assertFalse($preStatus->isRunning(), "Incorrect pre-run task status at loop $i");
254+
$this->assertTrue($liveStatus->isRunning(), "Incorrect live-run task status at loop $i");
255+
$this->sleep(0.9);
256+
}
257+
// should have finished
258+
$this->assertFalse($preStatus->isRunning(), "Incorrect pre-run task status at loop end");
259+
$this->assertFalse($liveStatus->isRunning(), "Incorrect live-run task status at loop end");
260+
}
234261
}

tests/Tasks/SleepingAsyncTask.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
class SleepingAsyncTask implements AsyncTaskInterface
88
{
9-
// just sleeps for 5 seconds, and finish
9+
// just sleeps for 2 seconds, and finish
1010

1111
public function __construct()
1212
{
1313
}
1414

1515
public function execute(): void
1616
{
17-
sleep(5);
17+
sleep(2);
1818
}
1919

2020
public function handleTimeout(): void

0 commit comments

Comments
 (0)