Skip to content

Commit fc5e96f

Browse files
committed
Add basic test cases
1 parent 1d4fb2a commit fc5e96f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tests/AsyncTaskTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Vectorial1024\LaravelProcessAsync\Tests;
44

5+
use InvalidArgumentException;
56
use LogicException;
67
use RuntimeException;
78
use Vectorial1024\LaravelProcessAsync\AsyncTask;
9+
use Vectorial1024\LaravelProcessAsync\AsyncTaskStatus;
810
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\DummyAsyncTask;
11+
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\SleepingAsyncTask;
912
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutENoticeTask;
1013
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutErrorTask;
1114
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNoOpTask;
@@ -204,4 +207,25 @@ public function testAsyncTimeoutIgnoreENotice()
204207
$this->assertFileDoesNotExist($textFilePath, "The async task timeout handler was inappropriately triggered (E_NOTICE should not trigger timeouts).");
205208
$this->assertNoNohupFile();
206209
}
210+
211+
public function testAsyncTaskID()
212+
{
213+
// test that we can correctly handle good and bad task IDs
214+
215+
// no ID is ok
216+
$task = new AsyncTask(new SleepingAsyncTask());
217+
unset($task);
218+
219+
// has ID is also ok
220+
$task = new AsyncTask(new SleepingAsyncTask(), taskID: "yeah");
221+
unset($task);
222+
223+
// but blank ID is not allowed
224+
$this->expectException(InvalidArgumentException::class);
225+
$task = new AsyncTask(new SleepingAsyncTask(), taskID: "");
226+
unset($task);
227+
$this->expectException(InvalidArgumentException::class);
228+
$taskStatus = new AsyncTaskStatus("");
229+
unset($taskStatus);
230+
}
207231
}

tests/Tasks/SleepingAsyncTask.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Vectorial1024\LaravelProcessAsync\Tests\Tasks;
4+
5+
use Vectorial1024\LaravelProcessAsync\AsyncTaskInterface;
6+
7+
class SleepingAsyncTask implements AsyncTaskInterface
8+
{
9+
// just sleeps for 5 seconds, and finish
10+
11+
public function __construct()
12+
{
13+
}
14+
15+
public function execute(): void
16+
{
17+
sleep(5);
18+
}
19+
20+
public function handleTimeout(): void
21+
{
22+
// nothing for now
23+
}
24+
}

0 commit comments

Comments
 (0)