Skip to content

Commit

Permalink
Provide test cases for task status checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorial1024 committed Jan 21, 2025
1 parent 6e6e1db commit 9d32cf7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/AsyncTaskStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function isRunning(): bool
return false;
}
// prove it is running
$isRunning = false;
$isRunning = $this->proveTaskIsRunning();
if (!$isRunning) {
$this->isStopped = true;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/AsyncTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,31 @@ public function testAsyncTaskID()
$taskStatus = new AsyncTaskStatus("");
unset($taskStatus);
}

public function testAsyncTaskNormalStatus()
{
// test that the task status reading is correct
$taskID = "testingTask";
$task = new AsyncTask(new SleepingAsyncTask(), taskID: $taskID);

// we haven't started yet, so this should say false
$preStatus = new AsyncTaskStatus($taskID);
$this->assertFalse($preStatus->isRunning());
// note: since it detects false, it will always continue to say false
$this->assertFalse($preStatus->isRunning());

// now we start running the task
$liveStatus = $task->start();

// the task is to sleep for 2 seconds, and then exit.
for ($i = 0; $i < 2; $i++) {
// check the statuses; most likely still be running
$this->assertFalse($preStatus->isRunning(), "Incorrect pre-run task status at loop $i");
$this->assertTrue($liveStatus->isRunning(), "Incorrect live-run task status at loop $i");
$this->sleep(0.9);
}
// should have finished
$this->assertFalse($preStatus->isRunning(), "Incorrect pre-run task status at loop end");
$this->assertFalse($liveStatus->isRunning(), "Incorrect live-run task status at loop end");
}
}
4 changes: 2 additions & 2 deletions tests/Tasks/SleepingAsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

class SleepingAsyncTask implements AsyncTaskInterface
{
// just sleeps for 5 seconds, and finish
// just sleeps for 2 seconds, and finish

public function __construct()
{
}

public function execute(): void
{
sleep(5);
sleep(2);
}

public function handleTimeout(): void
Expand Down

0 comments on commit 9d32cf7

Please sign in to comment.