Skip to content

Commit 718a736

Browse files
authored
Fix QueueWorkTest cases asserting nothing (#32958)
Setting up PHPUnit's $this->expectException(), calls to $worker->daemon() throw that exception so the test case immediately stops there. Subsquent assertions are being skipped. Fixing this shows testJobRunsIfAppIsNotInMaintenanceMode actually never runs any jobs on the queue. When running the whole laravael/framework test suite, Worker::memoryExceeded() can also return true from previous test cases being run. So both of these tests may skip running the queue, instead silently calling exit(12) for no more memory.
1 parent b3f2c40 commit 718a736

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

tests/Queue/QueueWorkerTest.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,21 @@ public function testWorkerCanWorkUntilQueueIsEmpty()
5959
$secondJob = new WorkerFakeJob,
6060
]]);
6161

62-
$this->expectException(LoopBreakerException::class);
62+
try {
63+
$worker->daemon('default', 'queue', $workerOptions);
6364

64-
$worker->daemon('default', 'queue', $workerOptions);
65+
$this->fail('Expected LoopBreakerException to be thrown.');
66+
} catch (LoopBreakerException $e) {
67+
$this->assertTrue($firstJob->fired);
6568

66-
$this->assertTrue($firstJob->fired);
69+
$this->assertTrue($secondJob->fired);
6770

68-
$this->assertTrue($secondJob->fired);
71+
$this->assertSame(0, $worker->stoppedWithStatus);
6972

70-
$this->assertSame(0, $worker->stoppedWithStatus);
73+
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessing::class))->twice();
7174

72-
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessing::class))->twice();
73-
74-
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessed::class))->twice();
75+
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessed::class))->twice();
76+
}
7577
}
7678

7779
public function testJobCanBeFiredBasedOnPriority()
@@ -276,21 +278,23 @@ public function testJobRunsIfAppIsNotInMaintenanceMode()
276278

277279
$maintenanceModeChecker = function () {
278280
if ($this->maintenanceFlags) {
279-
return array_pop($this->maintenanceFlags);
281+
return array_shift($this->maintenanceFlags);
280282
}
281283

282284
throw new LoopBreakerException;
283285
};
284286

285-
$this->expectException(LoopBreakerException::class);
286-
287287
$worker = $this->getWorker('default', ['queue' => [$firstJob, $secondJob]], $maintenanceModeChecker);
288288

289-
$worker->daemon('default', 'queue', $this->workerOptions());
289+
try {
290+
$worker->daemon('default', 'queue', $this->workerOptions());
290291

291-
$this->assertEquals($firstJob->attempts, 1);
292+
$this->fail('Expected LoopBreakerException to be thrown');
293+
} catch (LoopBreakerException $e) {
294+
$this->assertSame(1, $firstJob->attempts);
292295

293-
$this->assertEquals($firstJob->attempts, 0);
296+
$this->assertSame(0, $secondJob->attempts);
297+
}
294298
}
295299

296300
public function testJobDoesNotFireIfDeleted()
@@ -349,6 +353,7 @@ private function workerOptions(array $overrides = [])
349353
class InsomniacWorker extends Worker
350354
{
351355
public $sleptFor;
356+
public $stopOnMemoryExceeded = false;
352357

353358
public function sleep($seconds)
354359
{
@@ -366,6 +371,11 @@ public function daemonShouldRun(WorkerOptions $options, $connectionName, $queue)
366371
{
367372
return ! ($this->isDownForMaintenance)();
368373
}
374+
375+
public function memoryExceeded($memoryLimit)
376+
{
377+
return $this->stopOnMemoryExceeded;
378+
}
369379
}
370380

371381
class WorkerFakeManager extends QueueManager

0 commit comments

Comments
 (0)