-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
AmqpExtIntegrationTest.php
249 lines (200 loc) · 8.92 KB
/
AmqpExtIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceivedStamp;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpStamp;
use Symfony\Component\Messenger\Transport\AmqpExt\Connection;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Process\PhpProcess;
use Symfony\Component\Process\Process;
use Symfony\Component\Serializer as SerializerComponent;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
/**
* @requires extension amqp
*/
class AmqpExtIntegrationTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
if (!getenv('MESSENGER_AMQP_DSN')) {
$this->markTestSkipped('The "MESSENGER_AMQP_DSN" environment variable is required.');
}
}
public function testItSendsAndReceivesMessages()
{
$serializer = $this->createSerializer();
$connection = Connection::fromDsn(getenv('MESSENGER_AMQP_DSN'));
$connection->setup();
$connection->purgeQueues();
$sender = new AmqpSender($connection, $serializer);
$receiver = new AmqpReceiver($connection, $serializer);
$sender->send($first = new Envelope(new DummyMessage('First')));
$sender->send($second = new Envelope(new DummyMessage('Second')));
$envelopes = iterator_to_array($receiver->get());
$this->assertCount(1, $envelopes);
/** @var Envelope $envelope */
$envelope = $envelopes[0];
$this->assertEquals($first->getMessage(), $envelope->getMessage());
$this->assertInstanceOf(AmqpReceivedStamp::class, $envelope->last(AmqpReceivedStamp::class));
$envelopes = iterator_to_array($receiver->get());
$this->assertCount(1, $envelopes);
/** @var Envelope $envelope */
$envelope = $envelopes[0];
$this->assertEquals($second->getMessage(), $envelope->getMessage());
$this->assertEmpty(iterator_to_array($receiver->get()));
}
public function testRetryAndDelay()
{
$serializer = $this->createSerializer();
$connection = Connection::fromDsn(getenv('MESSENGER_AMQP_DSN'));
$connection->setup();
$connection->purgeQueues();
$sender = new AmqpSender($connection, $serializer);
$receiver = new AmqpReceiver($connection, $serializer);
// send a first message
$sender->send($first = new Envelope(new DummyMessage('First')));
// receive it immediately and imitate a redeliver with 2 second delay
$envelopes = iterator_to_array($receiver->get());
/** @var Envelope $envelope */
$envelope = $envelopes[0];
$newEnvelope = $envelope
->with(new DelayStamp(2000))
->with(new RedeliveryStamp(1, 'not_important'));
$sender->send($newEnvelope);
$receiver->ack($envelope);
// send a 2nd message with a shorter delay and custom routing key
$customRoutingKeyMessage = new DummyMessage('custom routing key');
$envelopeCustomRoutingKey = new Envelope($customRoutingKeyMessage, [
new DelayStamp(1000),
new AmqpStamp('my_custom_routing_key'),
]);
$sender->send($envelopeCustomRoutingKey);
// wait for next message (but max at 3 seconds)
$startTime = microtime(true);
$envelopes = $this->receiveEnvelopes($receiver, 3);
// duration should be about 1 second
$this->assertApproximateDuration($startTime, 1);
// this should be the custom routing key message first
$this->assertCount(1, $envelopes);
/* @var Envelope $envelope */
$receiver->ack($envelopes[0]);
$this->assertEquals($customRoutingKeyMessage, $envelopes[0]->getMessage());
// wait for final message (but max at 3 seconds)
$envelopes = $this->receiveEnvelopes($receiver, 3);
// duration should be about 2 seconds
$this->assertApproximateDuration($startTime, 2);
/* @var RedeliveryStamp|null $retryStamp */
// verify the stamp still exists from the last send
$this->assertCount(1, $envelopes);
$retryStamp = $envelopes[0]->last(RedeliveryStamp::class);
$this->assertNotNull($retryStamp);
$this->assertSame(1, $retryStamp->getRetryCount());
$receiver->ack($envelope);
}
public function testItReceivesSignals()
{
$serializer = $this->createSerializer();
$connection = Connection::fromDsn(getenv('MESSENGER_AMQP_DSN'));
$connection->setup();
$connection->purgeQueues();
$sender = new AmqpSender($connection, $serializer);
$sender->send(new Envelope(new DummyMessage('Hello')));
$amqpReadTimeout = 30;
$dsn = getenv('MESSENGER_AMQP_DSN').'?read_timeout='.$amqpReadTimeout;
$process = new PhpProcess(file_get_contents(__DIR__.'/Fixtures/long_receiver.php'), null, [
'COMPONENT_ROOT' => __DIR__.'/../../../',
'DSN' => $dsn,
]);
$process->start();
$this->waitForOutput($process, $expectedOutput = "Receiving messages...\n");
$signalTime = microtime(true);
$timedOutTime = time() + 10;
// immediately after the process has started "booted", kill it
$process->signal(15);
while ($process->isRunning() && time() < $timedOutTime) {
usleep(100 * 1000); // 100ms
}
// make sure the process exited, after consuming only the 1 message
$this->assertFalse($process->isRunning());
$this->assertLessThan($amqpReadTimeout, microtime(true) - $signalTime);
$this->assertSame($expectedOutput.<<<'TXT'
Get envelope with message: Symfony\Component\Messenger\Tests\Fixtures\DummyMessage
with stamps: [
"Symfony\\Component\\Messenger\\Transport\\AmqpExt\\AmqpReceivedStamp",
"Symfony\\Component\\Messenger\\Stamp\\ReceivedStamp"
]
Done.
TXT
, $process->getOutput());
}
public function testItCountsMessagesInQueue()
{
$serializer = $this->createSerializer();
$connection = Connection::fromDsn(getenv('MESSENGER_AMQP_DSN'));
$connection->setup();
$connection->purgeQueues();
$sender = new AmqpSender($connection, $serializer);
$sender->send(new Envelope(new DummyMessage('First')));
$sender->send(new Envelope(new DummyMessage('Second')));
$sender->send(new Envelope(new DummyMessage('Third')));
sleep(1); // give amqp a moment to have the messages ready
$this->assertSame(3, $connection->countMessagesInQueues());
}
private function waitForOutput(Process $process, string $output, $timeoutInSeconds = 10)
{
$timedOutTime = time() + $timeoutInSeconds;
while (time() < $timedOutTime) {
if (0 === strpos($process->getOutput(), $output)) {
return;
}
usleep(100 * 1000); // 100ms
}
throw new \RuntimeException('Expected output never arrived. Got "'.$process->getOutput().'" instead.');
}
private function createSerializer(): SerializerInterface
{
return new Serializer(
new SerializerComponent\Serializer([new ObjectNormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()])
);
}
private function assertApproximateDuration($startTime, int $expectedDuration)
{
$actualDuration = microtime(true) - $startTime;
if (method_exists($this, 'assertEqualsWithDelta')) {
$this->assertEqualsWithDelta($expectedDuration, $actualDuration, .5, 'Duration was not within expected range');
} else {
$this->assertEquals($expectedDuration, $actualDuration, 'Duration was not within expected range', .5);
}
}
/**
* @return Envelope[]
*/
private function receiveEnvelopes(ReceiverInterface $receiver, int $timeout): array
{
$envelopes = [];
$startTime = microtime(true);
while (0 === \count($envelopes) && $startTime + $timeout > time()) {
$envelopes = iterator_to_array($receiver->get());
}
return $envelopes;
}
}