Skip to content

Commit b626b11

Browse files
committed
redis new implementation
1 parent a25f83e commit b626b11

9 files changed

Lines changed: 265 additions & 92 deletions

‎pkg/redis/RedisConsumerHelperTrait.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ protected function receiveMessage(array $queues, int $timeout, int $redeliveryDe
3535
while ($thisTimeout > 0) {
3636
$this->migrateExpiredMessages($this->queueNames);
3737

38-
if ($result = $this->getContext()->getRedis()->brpop($this->queueNames, $thisTimeout)) {
39-
$this->pushQueueNameBack($result->getKey());
38+
if (false == $result = $this->getContext()->getRedis()->brpop($this->queueNames, $thisTimeout)) {
39+
return null;
40+
}
41+
42+
$this->pushQueueNameBack($result->getKey());
4043

41-
if ($message = $this->processResult($result, $redeliveryDelay)) {
42-
return $message;
43-
}
44+
if ($message = $this->processResult($result, $redeliveryDelay)) {
45+
return $message;
4446
}
4547

4648
$thisTimeout -= time() - $startAt;

‎pkg/redis/RedisSubscriptionConsumer.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function subscribe(Consumer $consumer, callable $callback): void
102102
}
103103

104104
$this->subscribers[$queueName] = [$consumer, $callback];
105+
$this->queueNames = null;
105106
}
106107

107108
/**
@@ -124,11 +125,13 @@ public function unsubscribe(Consumer $consumer): void
124125
}
125126

126127
unset($this->subscribers[$queueName]);
128+
$this->queueNames = null;
127129
}
128130

129131
public function unsubscribeAll(): void
130132
{
131133
$this->subscribers = [];
134+
$this->queueNames = null;
132135
}
133136

134137
private function getContext(): RedisContext

‎pkg/redis/Tests/RedisConnectionFactoryConfigTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static function provideConfigs()
9090
'read_write_timeout' => null,
9191
'predis_options' => null,
9292
'ssl' => null,
93+
'redelivery_delay' => 300,
9394
],
9495
];
9596

@@ -110,6 +111,7 @@ public static function provideConfigs()
110111
'read_write_timeout' => null,
111112
'predis_options' => null,
112113
'ssl' => null,
114+
'redelivery_delay' => 300,
113115
],
114116
];
115117

@@ -130,6 +132,7 @@ public static function provideConfigs()
130132
'read_write_timeout' => null,
131133
'predis_options' => null,
132134
'ssl' => null,
135+
'redelivery_delay' => 300,
133136
],
134137
];
135138

@@ -151,6 +154,7 @@ public static function provideConfigs()
151154
'predis_options' => null,
152155
'ssl' => null,
153156
'foo' => 'bar',
157+
'redelivery_delay' => 300,
154158
],
155159
];
156160

@@ -172,6 +176,7 @@ public static function provideConfigs()
172176
'predis_options' => null,
173177
'ssl' => null,
174178
'foo' => 'bar',
179+
'redelivery_delay' => 300,
175180
],
176181
];
177182

@@ -193,6 +198,7 @@ public static function provideConfigs()
193198
'predis_options' => null,
194199
'ssl' => null,
195200
'foo' => 'bar',
201+
'redelivery_delay' => 300,
196202
],
197203
];
198204

@@ -215,6 +221,7 @@ public static function provideConfigs()
215221
'predis_options' => null,
216222
'ssl' => null,
217223
'foo' => 'bar',
224+
'redelivery_delay' => 300,
218225
],
219226
];
220227

@@ -237,6 +244,7 @@ public static function provideConfigs()
237244
'predis_options' => null,
238245
'ssl' => null,
239246
'foo' => 'bar',
247+
'redelivery_delay' => 300,
240248
],
241249
];
242250

@@ -259,6 +267,7 @@ public static function provideConfigs()
259267
'predis_options' => null,
260268
'ssl' => null,
261269
'foo' => 'bar',
270+
'redelivery_delay' => 300,
262271
],
263272
];
264273

@@ -280,6 +289,7 @@ public static function provideConfigs()
280289
'predis_options' => null,
281290
'ssl' => null,
282291
'foo' => 'bar',
292+
'redelivery_delay' => 300,
283293
],
284294
];
285295

@@ -301,6 +311,7 @@ public static function provideConfigs()
301311
'read_write_timeout' => null,
302312
'predis_options' => null,
303313
'ssl' => null,
314+
'redelivery_delay' => 300,
304315
],
305316
];
306317

@@ -326,6 +337,7 @@ public static function provideConfigs()
326337
'cafile' => 'private.pem',
327338
'verify_peer' => '1',
328339
],
340+
'redelivery_delay' => 300,
329341
],
330342
];
331343
}

‎pkg/redis/Tests/RedisConsumerTest.php‎

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Redis\Tests;
44

5+
use Enqueue\Redis\JsonSerializer;
56
use Enqueue\Redis\Redis;
67
use Enqueue\Redis\RedisConsumer;
78
use Enqueue\Redis\RedisContext;
@@ -35,41 +36,85 @@ public function testShouldReturnDestinationSetInConstructorOnGetQueue()
3536
$this->assertSame($destination, $consumer->getQueue());
3637
}
3738

38-
public function testShouldDoNothingOnAcknowledge()
39+
public function testShouldAcknowledgeMessage()
3940
{
40-
$consumer = new RedisConsumer($this->createContextMock(), new RedisDestination('aQueue'));
41+
$redisMock = $this->createRedisMock();
42+
$redisMock
43+
->expects($this->once())
44+
->method('zrem')
45+
->with('aQueue:reserved', 'reserved-key')
46+
->willReturn(1)
47+
;
4148

42-
$consumer->acknowledge(new RedisMessage());
43-
}
49+
$contextMock = $this->createContextMock();
50+
$contextMock
51+
->expects($this->once())
52+
->method('getRedis')
53+
->willReturn($redisMock)
54+
;
4455

45-
public function testShouldDoNothingOnReject()
46-
{
47-
$consumer = new RedisConsumer($this->createContextMock(), new RedisDestination('aQueue'));
56+
$message = new RedisMessage();
57+
$message->setReservedKey('reserved-key');
58+
59+
$consumer = new RedisConsumer($contextMock, new RedisDestination('aQueue'));
4860

49-
$consumer->reject(new RedisMessage());
61+
$consumer->acknowledge($message);
5062
}
5163

52-
public function testShouldSendSameMessageToDestinationOnReQueue()
64+
public function testShouldRejectMessage()
5365
{
66+
$redisMock = $this->createRedisMock();
67+
$redisMock
68+
->expects($this->once())
69+
->method('zrem')
70+
->with('aQueue:reserved', 'reserved-key')
71+
->willReturn(1)
72+
;
73+
74+
$contextMock = $this->createContextMock();
75+
$contextMock
76+
->expects($this->once())
77+
->method('getRedis')
78+
->willReturn($redisMock)
79+
;
80+
5481
$message = new RedisMessage();
82+
$message->setReservedKey('reserved-key');
5583

56-
$destination = new RedisDestination('aQueue');
84+
$consumer = new RedisConsumer($contextMock, new RedisDestination('aQueue'));
5785

58-
$producerMock = $this->createProducerMock();
59-
$producerMock
86+
$consumer->reject($message);
87+
}
88+
89+
public function testShouldSendSameMessageToDestinationOnReQueue()
90+
{
91+
$redisMock = $this->createRedisMock();
92+
$redisMock
6093
->expects($this->once())
61-
->method('send')
62-
->with($this->identicalTo($destination), $this->identicalTo($message))
94+
->method('lpush')
95+
->with('aQueue', '{"body":"text","properties":[],"headers":{"attempts":0}}')
96+
->willReturn(1)
6397
;
6498

99+
$serializer = new JsonSerializer();
100+
65101
$contextMock = $this->createContextMock();
66102
$contextMock
67-
->expects($this->once())
68-
->method('createProducer')
69-
->willReturn($producerMock)
103+
->expects($this->any())
104+
->method('getRedis')
105+
->willReturn($redisMock)
106+
;
107+
$contextMock
108+
->expects($this->any())
109+
->method('getSerializer')
110+
->willReturn($serializer)
70111
;
71112

72-
$consumer = new RedisConsumer($contextMock, $destination);
113+
$message = new RedisMessage();
114+
$message->setBody('text');
115+
$message->setReservedKey($serializer->toString($message));
116+
117+
$consumer = new RedisConsumer($contextMock, new RedisDestination('aQueue'));
73118

74119
$consumer->reject($message, true);
75120
}
@@ -88,7 +133,7 @@ public function testShouldCallRedisBRPopAndReturnNullIfNothingInQueueOnReceive()
88133

89134
$contextMock = $this->createContextMock();
90135
$contextMock
91-
->expects($this->once())
136+
->expects($this->any())
92137
->method('getRedis')
93138
->willReturn($redisMock)
94139
;
@@ -102,20 +147,27 @@ public function testShouldCallRedisBRPopAndReturnMessageIfOneInQueueOnReceive()
102147
{
103148
$destination = new RedisDestination('aQueue');
104149

150+
$serializer = new JsonSerializer();
151+
105152
$redisMock = $this->createRedisMock();
106153
$redisMock
107154
->expects($this->once())
108155
->method('brpop')
109156
->with(['aQueue'], 2)
110-
->willReturn(new RedisResult('aQueue', json_encode(new RedisMessage('aBody'))))
157+
->willReturn(new RedisResult('aQueue', $serializer->toString(new RedisMessage('aBody'))))
111158
;
112159

113160
$contextMock = $this->createContextMock();
114161
$contextMock
115-
->expects($this->once())
162+
->expects($this->any())
116163
->method('getRedis')
117164
->willReturn($redisMock)
118165
;
166+
$contextMock
167+
->expects($this->any())
168+
->method('getSerializer')
169+
->willReturn($serializer)
170+
;
119171

120172
$consumer = new RedisConsumer($contextMock, $destination);
121173

@@ -131,24 +183,26 @@ public function testShouldCallRedisBRPopSeveralTimesWithFiveSecondTimeoutIfZeroT
131183

132184
$expectedTimeout = 5;
133185

186+
$serializer = new JsonSerializer();
187+
134188
$redisMock = $this->createRedisMock();
135189
$redisMock
136-
->expects($this->at(0))
190+
->expects($this->at(2))
137191
->method('brpop')
138192
->with(['aQueue'], $expectedTimeout)
139193
->willReturn(null)
140194
;
141195
$redisMock
142-
->expects($this->at(1))
196+
->expects($this->at(5))
143197
->method('brpop')
144198
->with(['aQueue'], $expectedTimeout)
145199
->willReturn(null)
146200
;
147201
$redisMock
148-
->expects($this->at(2))
202+
->expects($this->at(8))
149203
->method('brpop')
150204
->with(['aQueue'], $expectedTimeout)
151-
->willReturn(new RedisResult('aQueue', json_encode(new RedisMessage('aBody'))))
205+
->willReturn(new RedisResult('aQueue', $serializer->toString(new RedisMessage('aBody'))))
152206
;
153207

154208
$contextMock = $this->createContextMock();
@@ -157,6 +211,11 @@ public function testShouldCallRedisBRPopSeveralTimesWithFiveSecondTimeoutIfZeroT
157211
->method('getRedis')
158212
->willReturn($redisMock)
159213
;
214+
$contextMock
215+
->expects($this->atLeastOnce())
216+
->method('getSerializer')
217+
->willReturn($serializer)
218+
;
160219

161220
$consumer = new RedisConsumer($contextMock, $destination);
162221

@@ -170,6 +229,8 @@ public function testShouldCallRedisRPopAndReturnNullIfNothingInQueueOnReceiveNoW
170229
{
171230
$destination = new RedisDestination('aQueue');
172231

232+
$serializer = new JsonSerializer();
233+
173234
$redisMock = $this->createRedisMock();
174235
$redisMock
175236
->expects($this->once())
@@ -180,10 +241,15 @@ public function testShouldCallRedisRPopAndReturnNullIfNothingInQueueOnReceiveNoW
180241

181242
$contextMock = $this->createContextMock();
182243
$contextMock
183-
->expects($this->once())
244+
->expects($this->any())
184245
->method('getRedis')
185246
->willReturn($redisMock)
186247
;
248+
$contextMock
249+
->expects($this->any())
250+
->method('getSerializer')
251+
->willReturn($serializer)
252+
;
187253

188254
$consumer = new RedisConsumer($contextMock, $destination);
189255

@@ -194,20 +260,27 @@ public function testShouldCallRedisRPopAndReturnMessageIfOneInQueueOnReceiveNoWa
194260
{
195261
$destination = new RedisDestination('aQueue');
196262

263+
$serializer = new JsonSerializer();
264+
197265
$redisMock = $this->createRedisMock();
198266
$redisMock
199267
->expects($this->once())
200268
->method('rpop')
201269
->with('aQueue')
202-
->willReturn(new RedisResult('aQueue', json_encode(new RedisMessage('aBody'))))
270+
->willReturn(new RedisResult('aQueue', $serializer->toString(new RedisMessage('aBody'))))
203271
;
204272

205273
$contextMock = $this->createContextMock();
206274
$contextMock
207-
->expects($this->once())
275+
->expects($this->atLeastOnce())
208276
->method('getRedis')
209277
->willReturn($redisMock)
210278
;
279+
$contextMock
280+
->expects($this->any())
281+
->method('getSerializer')
282+
->willReturn($serializer)
283+
;
211284

212285
$consumer = new RedisConsumer($contextMock, $destination);
213286

0 commit comments

Comments
 (0)