Skip to content

Commit d53582b

Browse files
Marick van TuilMarick van Tuil
Marick van Tuil
authored and
Marick van Tuil
committed
Add timeout test
1 parent afbe4e5 commit d53582b

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.idea/
33
.phpunit.result.cache
44
.phpunit.cache
5-
.env
5+
.env
6+
/coverage

phpunit.xml

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
<env name="QUEUE_DRIVER" value="cloudtasks"/>
1717
<env name="DB_DRIVER" value="mysql"/>
1818
</php>
19+
<source>
20+
<include>
21+
<directory suffix=".php">./src</directory>
22+
</include>
23+
</source>
1924
</phpunit>

tests/Support/SimpleJobWithTimeout.php

+6-13
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@
44

55
namespace Tests\Support;
66

7+
use Error;
78
use Illuminate\Support\Facades\Event;
89
use Illuminate\Queue\Events\WorkerStopping;
10+
use Symfony\Component\ErrorHandler\Error\FatalError;
911

1012
class SimpleJobWithTimeout extends SimpleJob
1113
{
1214
public $timeout = 3;
1315

1416
public function handle()
1517
{
16-
Event::listen(WorkerStopping::class, function () {
17-
event(new JobOutput('SimpleJobWithTimeout:worker-stopping'));
18-
});
19-
20-
event(new JobOutput('SimpleJobWithTimeout:1'));
21-
sleep(1);
22-
event(new JobOutput('SimpleJobWithTimeout:2'));
23-
sleep(1);
24-
event(new JobOutput('SimpleJobWithTimeout:3'));
25-
sleep(1);
26-
event(new JobOutput('SimpleJobWithTimeout:4'));
27-
sleep(1);
28-
event(new JobOutput('SimpleJobWithTimeout:5'));
18+
throw new FatalError('Maximum execution time of 30 seconds exceeded', 500, [
19+
'file' => __FILE__,
20+
'line' => __LINE__,
21+
]);
2922
}
3023
}

tests/TaskHandlerTest.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,13 @@ public function retried_jobs_get_a_new_name()
289289
#[Test]
290290
public function test_job_timeout()
291291
{
292-
$this->markTestSkipped('Currently seemingly impossible to test job timeouts.');
293-
294292
// Arrange
295-
Event::fake(JobOutput::class);
293+
Event::fake(JobReleasedAfterException::class);
296294

297295
// Act
298296
$this->dispatch(new SimpleJobWithTimeout)->run();
299297

300298
// Assert
301-
$events = Event::dispatched(JobOutput::class)->map(fn ($event) => $event[0]->output)->toArray();
302-
$this->assertEquals([
303-
'SimpleJobWithTimeout:1',
304-
'SimpleJobWithTimeout:2',
305-
'SimpleJobWithTimeout:3',
306-
'SimpleJobWithTimeout:worker-stopping',
307-
'SimpleJobWithTimeout:4',
308-
'SimpleJobWithTimeout:5',
309-
], $events);
299+
Event::assertDispatched(JobReleasedAfterException::class);
310300
}
311301
}

0 commit comments

Comments
 (0)