[Messenger] Stop worker when it should stop

This commit is contained in:
tien.xuan.vo 2019-08-23 10:48:20 +07:00
parent aeba22c4ad
commit 5c1f3a2414
2 changed files with 29 additions and 0 deletions

View File

@ -27,6 +27,7 @@ use Symfony\Component\Messenger\Stamp\SentStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Worker;
use Symfony\Component\Messenger\Worker\StopWhenMessageCountIsExceededWorker;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
@ -361,6 +362,30 @@ class WorkerTest extends TestCase
// make sure they were processed in the correct order
$this->assertSame([$envelope1, $envelope2, $envelope3, $envelope4, $envelope5, $envelope6], $processedEnvelopes);
}
public function testWorkerWithDecorator()
{
$envelope1 = new Envelope(new DummyMessage('message1'));
$envelope2 = new Envelope(new DummyMessage('message2'));
$envelope3 = new Envelope(new DummyMessage('message3'));
$receiver = new DummyReceiver([
[$envelope1, $envelope2, $envelope3],
]);
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
$worker = new Worker([$receiver], $bus);
$workerWithDecorator = new StopWhenMessageCountIsExceededWorker($worker, 2);
$processedEnvelopes = [];
$workerWithDecorator->run([], function (?Envelope $envelope) use ($worker, &$processedEnvelopes) {
if (null !== $envelope) {
$processedEnvelopes[] = $envelope;
}
});
$this->assertSame([$envelope1, $envelope2], $processedEnvelopes);
}
}
class DummyReceiver implements ReceiverInterface

View File

@ -92,6 +92,10 @@ class Worker implements WorkerInterface
$this->handleMessage($envelope, $receiver, $transportName, $this->retryStrategies[$transportName] ?? null);
$onHandled($envelope);
if ($this->shouldStop) {
break 2;
}
}
// after handling a single receiver, quit and start the loop again