Skip to content

Commit a43eff3

Browse files
committed
test(worker): improve worker job counter scenarios
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 8b5f244 commit a43eff3

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

tests/php/Unit/Service/Worker/WorkerJobCounterTest.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,179 @@ public function testCountPendingJobsHandlesLargeNumbers(): void {
248248
$counter = $this->makeCounter();
249249
$this->assertSame(1003, $counter->countPendingJobs());
250250
}
251+
252+
public function testCountPendingJobsTreatsNonArraySignFileArgumentAsSingleWorkUnit(): void {
253+
$signFileJob = $this->createMock(SignFileJob::class);
254+
$signFileJob->method('getArgument')
255+
->willReturn('invalid');
256+
257+
$signFileJobs = [$signFileJob];
258+
$signSingleJobs = [];
259+
$callIndex = 0;
260+
$this->jobList->expects($this->exactly(2))
261+
->method('getJobsIterator')
262+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJobs, $signSingleJobs) {
263+
$callIndex++;
264+
if ($callIndex === 1) {
265+
$this->assertSame(SignFileJob::class, $class);
266+
return $signFileJobs;
267+
}
268+
$this->assertSame(SignSingleFileJob::class, $class);
269+
return $signSingleJobs;
270+
});
271+
272+
$this->fileMapper->expects($this->never())
273+
->method('getById');
274+
275+
$counter = $this->makeCounter();
276+
$this->assertSame(1, $counter->countPendingJobs());
277+
}
278+
279+
public function testCountPendingJobsTreatsMissingFileIdAsSingleWorkUnit(): void {
280+
$signFileJob = $this->makeSignFileJob([]);
281+
282+
$signFileJobs = [$signFileJob];
283+
$signSingleJobs = [];
284+
$callIndex = 0;
285+
$this->jobList->expects($this->exactly(2))
286+
->method('getJobsIterator')
287+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJobs, $signSingleJobs) {
288+
$callIndex++;
289+
if ($callIndex === 1) {
290+
$this->assertSame(SignFileJob::class, $class);
291+
return $signFileJobs;
292+
}
293+
$this->assertSame(SignSingleFileJob::class, $class);
294+
return $signSingleJobs;
295+
});
296+
297+
$this->fileMapper->expects($this->never())
298+
->method('getById');
299+
300+
$counter = $this->makeCounter();
301+
$this->assertSame(1, $counter->countPendingJobs());
302+
}
303+
304+
public function testCountPendingJobsTreatsMapperLookupFailureAsSingleWorkUnit(): void {
305+
$signFileJob = $this->makeSignFileJob(['fileId' => 77]);
306+
307+
$signFileJobs = [$signFileJob];
308+
$signSingleJobs = [];
309+
$callIndex = 0;
310+
$this->jobList->expects($this->exactly(2))
311+
->method('getJobsIterator')
312+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJobs, $signSingleJobs) {
313+
$callIndex++;
314+
if ($callIndex === 1) {
315+
$this->assertSame(SignFileJob::class, $class);
316+
return $signFileJobs;
317+
}
318+
$this->assertSame(SignSingleFileJob::class, $class);
319+
return $signSingleJobs;
320+
});
321+
322+
$this->fileMapper->expects($this->once())
323+
->method('getById')
324+
->with(77)
325+
->will($this->throwException(new \RuntimeException('not found')));
326+
327+
$counter = $this->makeCounter();
328+
$this->assertSame(1, $counter->countPendingJobs());
329+
}
330+
331+
public function testCountPendingJobsKeepsAtLeastOneWorkUnitForEmptyEnvelope(): void {
332+
$signFileJob = $this->makeSignFileJob(['fileId' => 33]);
333+
$envelope = new FileEntity();
334+
$envelope->setId(33);
335+
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
336+
337+
$signFileJobs = [$signFileJob];
338+
$signSingleJobs = [];
339+
$callIndex = 0;
340+
$this->jobList->expects($this->exactly(2))
341+
->method('getJobsIterator')
342+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJobs, $signSingleJobs) {
343+
$callIndex++;
344+
if ($callIndex === 1) {
345+
$this->assertSame(SignFileJob::class, $class);
346+
return $signFileJobs;
347+
}
348+
$this->assertSame(SignSingleFileJob::class, $class);
349+
return $signSingleJobs;
350+
});
351+
352+
$this->fileMapper->expects($this->once())
353+
->method('getById')
354+
->with(33)
355+
->willReturn($envelope);
356+
357+
$this->fileMapper->expects($this->once())
358+
->method('countChildrenFiles')
359+
->with(33)
360+
->willReturn(0);
361+
362+
$counter = $this->makeCounter();
363+
$this->assertSame(1, $counter->countPendingJobs());
364+
}
365+
366+
public function testCountPendingJobsCastsStringFileIdToInt(): void {
367+
$signFileJob = $this->makeSignFileJob(['fileId' => '123']);
368+
$envelope = new FileEntity();
369+
$envelope->setId(123);
370+
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
371+
372+
$signFileJobs = [$signFileJob];
373+
$signSingleJobs = [];
374+
$callIndex = 0;
375+
$this->jobList->expects($this->exactly(2))
376+
->method('getJobsIterator')
377+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJobs, $signSingleJobs) {
378+
$callIndex++;
379+
if ($callIndex === 1) {
380+
$this->assertSame(SignFileJob::class, $class);
381+
return $signFileJobs;
382+
}
383+
$this->assertSame(SignSingleFileJob::class, $class);
384+
return $signSingleJobs;
385+
});
386+
387+
$this->fileMapper->expects($this->once())
388+
->method('getById')
389+
->with(123)
390+
->willReturn($envelope);
391+
392+
$this->fileMapper->expects($this->once())
393+
->method('countChildrenFiles')
394+
->with(123)
395+
->willReturn(2);
396+
397+
$counter = $this->makeCounter();
398+
$this->assertSame(2, $counter->countPendingJobs());
399+
}
400+
401+
public function testCountPendingJobsReturnsZeroWhenSecondIteratorFails(): void {
402+
$signFileJob = $this->makeSignFileJob([]);
403+
$callIndex = 0;
404+
$this->jobList->expects($this->exactly(2))
405+
->method('getJobsIterator')
406+
->willReturnCallback(function (string $class) use (&$callIndex, $signFileJob) {
407+
$callIndex++;
408+
if ($callIndex === 1) {
409+
$this->assertSame(SignFileJob::class, $class);
410+
return [$signFileJob];
411+
}
412+
$this->assertSame(SignSingleFileJob::class, $class);
413+
throw new \RuntimeException('queue unavailable');
414+
});
415+
416+
$this->logger->expects($this->once())
417+
->method('debug')
418+
->with(
419+
$this->stringContains('Failed to count pending jobs'),
420+
$this->arrayHasKey('error')
421+
);
422+
423+
$counter = $this->makeCounter();
424+
$this->assertSame(0, $counter->countPendingJobs());
425+
}
251426
}

0 commit comments

Comments
 (0)