Skip to content

Commit f7cc039

Browse files
committed
Test async timeout handlers are not wrongly triggered
1 parent 5294065 commit f7cc039

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

tests/AsyncTaskTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use RuntimeException;
77
use Vectorial1024\LaravelProcessAsync\AsyncTask;
88
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\DummyAsyncTask;
9+
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutENoticeTask;
10+
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutErrorTask;
11+
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNoOpTask;
912
use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNormalTask;
1013

1114
class AsyncTaskTest extends BaseTestCase
@@ -145,4 +148,49 @@ public function testAsyncTimeout()
145148
$this->assertStringEqualsFile($textFilePath, $message);
146149
$this->assertNoNohupFile();
147150
}
151+
152+
public function testAsyncTimeoutIgnoreErrors()
153+
{
154+
// test that the async timeout handler is not triggered due to other fatal errors
155+
$message = "timeout occured";
156+
$textFilePath = $this->getStoragePath("testAsyncTimeoutIgnoreErrors.txt");
157+
$timeoutTask = new TestTimeoutErrorTask($message, $textFilePath);
158+
$task = new AsyncTask($timeoutTask);
159+
$task->withTimeLimit(1)->start();
160+
// we wait for it to timeout
161+
$this->sleep(1);
162+
// should have timed out
163+
$this->assertFileDoesNotExist($textFilePath, "The async task timeout handler was inappropriately triggered (PHP fatal errors should not trigger timeouts).");
164+
$this->assertNoNohupFile();
165+
}
166+
167+
public function testAsyncTimeoutIgnoreNoProblem()
168+
{
169+
// test that the async timeout handler is not triggered when nothing happened
170+
$message = "timeout occured";
171+
$textFilePath = $this->getStoragePath("testAsyncTimeoutIgnoreNoProblem.txt");
172+
$timeoutTask = new TestTimeoutNoOpTask($message, $textFilePath);
173+
$task = new AsyncTask($timeoutTask);
174+
$task->withTimeLimit(1)->start();
175+
// we wait for it to timeout
176+
$this->sleep(1);
177+
// should have timed out
178+
$this->assertFileDoesNotExist($textFilePath, "The async task timeout handler was inappropriately triggered (finishing a task before the time limit should not trigger timeouts).");
179+
$this->assertNoNohupFile();
180+
}
181+
182+
public function testAsyncTimeoutIgnoreENotice()
183+
{
184+
// test that the async timeout handler is not triggered when there is an E_NOTICE error
185+
$message = "timeout occured";
186+
$textFilePath = $this->getStoragePath("testAsyncTimeoutIgnoreENotice.txt");
187+
$timeoutTask = new TestTimeoutENoticeTask($message, $textFilePath);
188+
$task = new AsyncTask($timeoutTask);
189+
$task->withTimeLimit(1)->start();
190+
// we wait for it to timeout
191+
$this->sleep(1);
192+
// should have timed out
193+
$this->assertFileDoesNotExist($textFilePath, "The async task timeout handler was inappropriately triggered (E_NOTICE should not trigger timeouts).");
194+
$this->assertNoNohupFile();
195+
}
148196
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Vectorial1024\LaravelProcessAsync\Tests\Tasks;
4+
5+
use Vectorial1024\LaravelProcessAsync\AsyncTaskInterface;
6+
7+
class TestTimeoutENoticeTask implements AsyncTaskInterface
8+
{
9+
// triggers an E_NOTICE error; the timeout handler should NOT trigger! this is because script execution can still continue (at least in PHP 8)
10+
11+
public function __construct(
12+
private string $message,
13+
private string $targetFilePath
14+
) {
15+
}
16+
17+
public function execute(): void
18+
{
19+
// eh eh
20+
$context;
21+
}
22+
23+
public function handleTimeout(): void
24+
{
25+
$fp = fopen($this->targetFilePath, "w");
26+
fwrite($fp, $this->message);
27+
fflush($fp);
28+
fclose($fp);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Vectorial1024\LaravelProcessAsync\Tests\Tasks;
4+
5+
use Vectorial1024\LaravelProcessAsync\AsyncTaskInterface;
6+
7+
class TestTimeoutErrorTask implements AsyncTaskInterface
8+
{
9+
// has a timeout handler, but throws a runtime exception (the timeout handler is not triggered)
10+
11+
public function __construct(
12+
private string $message,
13+
private string $targetFilePath
14+
) {
15+
}
16+
17+
public function execute(): void
18+
{
19+
// boom!
20+
1 / 0;
21+
}
22+
23+
public function handleTimeout(): void
24+
{
25+
$fp = fopen($this->targetFilePath, "w");
26+
fwrite($fp, $this->message);
27+
fflush($fp);
28+
fclose($fp);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Vectorial1024\LaravelProcessAsync\Tests\Tasks;
4+
5+
use Vectorial1024\LaravelProcessAsync\AsyncTaskInterface;
6+
7+
class TestTimeoutNoOpTask implements AsyncTaskInterface
8+
{
9+
// quite literally does nothing; the timeout handler is therefore only for decoration
10+
11+
public function __construct(
12+
private string $message,
13+
private string $targetFilePath
14+
) {
15+
}
16+
17+
public function execute(): void
18+
{
19+
// goodbye!
20+
exit();
21+
}
22+
23+
public function handleTimeout(): void
24+
{
25+
$fp = fopen($this->targetFilePath, "w");
26+
fwrite($fp, $this->message);
27+
fflush($fp);
28+
fclose($fp);
29+
}
30+
}

0 commit comments

Comments
 (0)