#How to test jobs and queues with delays?

1 messages · Page 1 of 1 (latest)

trim needle
#

I'm not seeing where you're calling the cancelOrder method.
It's going to take 4 minutes (12*20/60) if you force the wait of 20 seconds per order item.

$orders = [
    ['type' => 'bread', 'amount' => 2],
    ['type' => 'bread', 'amount' => 10],
];
$bakingService->sendOrder($orders);

You want to test that code and check that the total ends up as 12, correct?

Edit:

queue up 10 units two times, then cancel after creating 2 on the first order

Wait, why is it being cancelled after creating 2 of the first order? I'm not seeing that in the code. Is that supposed to be round robin?


That aside many places I would suggest refactoring to a more declarative. e.g.:

//before
$bakery->{$this->order->type} += 1;
$bakery->save();
$this->order->remaining_amount -= 1;
$this->order->save();
//after
$bakery->increment($this->order->type);
$this->order->decrement('remaining_amount');

And:

$nextOrder->setStatus('in_progress');
compact hedge
#

Ahh, I see I forgot to write code for the whole example.


$orders = [
  ['type' => 'bread', 'amount' => 10]
];
$bakingService->sendOrder($orders);
$bakingService->sendOrder($orders);

// after 2 units is added, from other endpoint I call
$bakingService->cancelOrder(1)

This should add 2, then cancel the first order, continue the second order. I wanted to know how i could test this, since there needs to be a delay. I found some way to test things seperately, but there is so much setup and the test really needs to know everything about the code. It was almost like write the code again.

Does this make it clearer?

trim needle
#

This should add 2, then cancel the first order,
I'm not seeing that in the code. But perhaps I missed it.

Start writing the test. If the connection is sync in the test I think you'll be able to test it.