0

I'm trying to test that a job is batching the correct number of another job like so

Bus::fake();
...
(new App\Jobs\ProcessChunkOfProducts($this->productsAsChunk()))->handle();
Bus::assertBatched( function (PendingBatch $batch) {
   return $batch->jobs->count() === 49;
});

It passes most the time, but occasionally fails. Is it possible that I've created some sort of race condition where the jobs aren't batched before the assertion is made?

damask
  • 529
  • 4
  • 17
  • It depends what you have inside the `handle` and is it possible that `productsAsChunk` is maybe not returning `49` products? – matiaslauriti Jan 19 '22 at 03:25
  • `productsAsChunk` contains 50 products, one with a condition which means a job shouldn't be created for it (the purpose of the test). The `handle()` is large. The test either passes, or the `jobs->count() == 0`, so the issue isn't with the `productsAsChunk` I don't think. – damask Jan 19 '22 at 03:36
  • Oh, okay, if you then have `$jobs->count() === 0` then I am assuming there is something wrong in your code... Do you have `QUEUE_CONNECTION=sync` for your tests? – matiaslauriti Jan 19 '22 at 04:16
  • I sure do, in my phpunit.xml. I have 137 passing tests in my project, this is the only one that fails (approx 10% of the time) – damask Jan 19 '22 at 04:27
  • My question was really, is it a race condition problem, or a bug problem. By the sound of it, you believe it to be a bug? – damask Jan 19 '22 at 04:30
  • I believe it is a bug, because if you are not running asynchronously, then your code is doing something that it should not. Are those jobs tied to time? Can you add more details about the `handle`? What is happening with each job? – matiaslauriti Jan 19 '22 at 04:41

0 Answers0