|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Tests\Transport\RedisExt; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Messenger\Envelope; |
| 16 | +use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; |
| 17 | +use Symfony\Component\Messenger\Transport\RedisExt\Connection; |
| 18 | +use Symfony\Component\Messenger\Transport\RedisExt\RedisReceiver; |
| 19 | +use Symfony\Component\Messenger\Transport\RedisExt\RedisSender; |
| 20 | +use Symfony\Component\Messenger\Transport\Serialization\Serializer; |
| 21 | +use Symfony\Component\Process\PhpProcess; |
| 22 | +use Symfony\Component\Process\Process; |
| 23 | +use Symfony\Component\Serializer as SerializerComponent; |
| 24 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 25 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 26 | + |
| 27 | +/** |
| 28 | + * @requires extension redis |
| 29 | + */ |
| 30 | +class RedisExtIntegrationTest extends TestCase |
| 31 | +{ |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + if (!getenv('MESSENGER_REDIS_DSN')) { |
| 37 | + $this->markTestSkipped('The "MESSENGER_REDIS_DSN" environment variable is required.'); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function testItSendsAndReceivesMessages() |
| 42 | + { |
| 43 | + $serializer = new Serializer( |
| 44 | + new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) |
| 45 | + ); |
| 46 | + |
| 47 | + $connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN')); |
| 48 | + |
| 49 | + $sender = new RedisSender($connection, $serializer); |
| 50 | + $receiver = new RedisReceiver($connection, $serializer); |
| 51 | + |
| 52 | + $sender->send($first = Envelope::wrap(new DummyMessage('First'))); |
| 53 | + $sender->send($second = Envelope::wrap(new DummyMessage('Second'))); |
| 54 | + |
| 55 | + $receivedMessages = 0; |
| 56 | + $receiver->receive(function (?Envelope $envelope) use ($receiver, &$receivedMessages, $first, $second) { |
| 57 | + $this->assertEquals(0 == $receivedMessages ? $first : $second, $envelope); |
| 58 | + |
| 59 | + if (2 === ++$receivedMessages) { |
| 60 | + $receiver->stop(); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + public function testItReceivesSignals() |
| 66 | + { |
| 67 | + $serializer = new Serializer( |
| 68 | + new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) |
| 69 | + ); |
| 70 | + |
| 71 | + $connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN')); |
| 72 | + |
| 73 | + $sender = new RedisSender($connection, $serializer); |
| 74 | + $sender->send(Envelope::wrap(new DummyMessage('Hello'))); |
| 75 | + |
| 76 | + $amqpReadTimeout = 30; |
| 77 | + $dsn = getenv('MESSENGER_REDIS_DSN').'?read_timeout='.$amqpReadTimeout; |
| 78 | + $process = new PhpProcess(file_get_contents(__DIR__.'/Fixtures/long_receiver.php'), null, array( |
| 79 | + 'COMPONENT_ROOT' => __DIR__.'/../../../', |
| 80 | + 'DSN' => $dsn, |
| 81 | + )); |
| 82 | + |
| 83 | + $process->start(); |
| 84 | + |
| 85 | + $this->waitForOutput($process, $expectedOutput = "Receiving messages...\n"); |
| 86 | + |
| 87 | + $signalTime = microtime(true); |
| 88 | + $timedOutTime = time() + 10; |
| 89 | + |
| 90 | + $process->signal(15); |
| 91 | + |
| 92 | + while ($process->isRunning() && time() < $timedOutTime) { |
| 93 | + usleep(100 * 1000); // 100ms |
| 94 | + } |
| 95 | + |
| 96 | + $this->assertFalse($process->isRunning()); |
| 97 | + $this->assertLessThan($amqpReadTimeout, microtime(true) - $signalTime); |
| 98 | + $this->assertSame($expectedOutput.<<<'TXT' |
| 99 | +Get envelope with message: Symfony\Component\Messenger\Tests\Fixtures\DummyMessage |
| 100 | +with items: [ |
| 101 | + "Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage" |
| 102 | +] |
| 103 | +Done. |
| 104 | + |
| 105 | +TXT |
| 106 | + , $process->getOutput()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @runInSeparateProcess |
| 111 | + */ |
| 112 | + public function testItSupportsTimeoutAndTicksNullMessagesToTheHandler() |
| 113 | + { |
| 114 | + $serializer = new Serializer( |
| 115 | + new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) |
| 116 | + ); |
| 117 | + |
| 118 | + $connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), array('blocking_timeout' => '1')); |
| 119 | + |
| 120 | + $receiver = new RedisReceiver($connection, $serializer); |
| 121 | + |
| 122 | + $receivedMessages = 0; |
| 123 | + $receiver->receive(function (?Envelope $envelope) use ($receiver, &$receivedMessages) { |
| 124 | + $this->assertNull($envelope); |
| 125 | + |
| 126 | + if (2 === ++$receivedMessages) { |
| 127 | + $receiver->stop(); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private function waitForOutput(Process $process, string $output, $timeoutInSeconds = 10) |
| 133 | + { |
| 134 | + $timedOutTime = time() + $timeoutInSeconds; |
| 135 | + |
| 136 | + while (time() < $timedOutTime) { |
| 137 | + if (0 === strpos($process->getOutput(), $output)) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + usleep(100 * 1000); // 100ms |
| 142 | + } |
| 143 | + |
| 144 | + throw new \RuntimeException('Expected output never arrived. Got "'.$process->getOutput().'" instead.'); |
| 145 | + } |
| 146 | +} |
0 commit comments