Skip to content

Commit 3b205e0

Browse files
Swap priority of producer and message values
Don’t reset producer values after send
1 parent 16797d6 commit 3b205e0

File tree

2 files changed

+17
-214
lines changed

2 files changed

+17
-214
lines changed

pkg/pheanstalk/PheanstalkProducer.php

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ public function send(Destination $destination, Message $message): void
5252
throw new \InvalidArgumentException(sprintf('Could not encode value into json. Error %s and message %s', json_last_error(), json_last_error_msg()));
5353
}
5454

55+
if (null !== $this->priority && null === $message->getHeader('priority')) {
56+
$message->setPriority($this->priority);
57+
}
58+
if (null !== $this->deliveryDelay && null === $message->getHeader('delay')) {
59+
$message->setDelay($this->deliveryDelay / 1000);
60+
}
61+
if (null !== $this->timeToLive && null === $message->getHeader('ttr')) {
62+
$message->setTimeToRun($this->timeToLive / 1000);
63+
}
64+
5565
$this->pheanstalk->useTube($destination->getName())->put(
5666
$rawMessage,
57-
$this->resolvePriority($message),
58-
$this->resolveDelay($message),
59-
$this->resolveTimeToLive($message)
67+
$message->getPriority(),
68+
$message->getDelay(),
69+
$message->getTimeToRun()
6070
);
6171
}
6272

@@ -104,40 +114,4 @@ public function getTimeToLive(): ?int
104114
{
105115
return $this->timeToLive;
106116
}
107-
108-
private function resolvePriority(PheanstalkMessage $message): ?int
109-
{
110-
if (null === $this->priority) {
111-
return $message->getPriority();
112-
}
113-
114-
$priority = $this->priority;
115-
$this->priority = null;
116-
117-
return $priority;
118-
}
119-
120-
private function resolveDelay(PheanstalkMessage $message): ?int
121-
{
122-
if (null === $this->deliveryDelay) {
123-
return $message->getDelay();
124-
}
125-
126-
$delay = $this->deliveryDelay;
127-
$this->deliveryDelay = null;
128-
129-
return $delay / 1000;
130-
}
131-
132-
private function resolveTimeToLive(PheanstalkMessage $message): ?int
133-
{
134-
if (null === $this->timeToLive) {
135-
return $message->getTimeToRun();
136-
}
137-
138-
$ttl = $this->timeToLive;
139-
$this->timeToLive = null;
140-
141-
return $ttl / 1000;
142-
}
143117
}

pkg/pheanstalk/Tests/PheanstalkProducerTest.php

Lines changed: 4 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,7 @@ public function testShouldJsonEncodeMessageAndPutToExpectedTube()
6666
);
6767
}
6868

69-
public function testPriorityPrecedesMessagePriority()
70-
{
71-
$message = new PheanstalkMessage('theBody');
72-
$message->setPriority(100);
73-
74-
$pheanstalk = $this->createPheanstalkMock();
75-
$pheanstalk
76-
->expects($this->once())
77-
->method('useTube')
78-
->with('theQueueName')
79-
->willReturnSelf()
80-
;
81-
$pheanstalk
82-
->expects($this->once())
83-
->method('put')
84-
->with('{"body":"theBody","properties":[],"headers":{"priority":100}}', 50, Pheanstalk::DEFAULT_DELAY, Pheanstalk::DEFAULT_TTR)
85-
;
86-
87-
$producer = new PheanstalkProducer($pheanstalk);
88-
$producer->setPriority(50);
89-
90-
$producer->send(
91-
new PheanstalkDestination('theQueueName'),
92-
$message
93-
);
94-
}
95-
96-
public function testNullPriorityFallsBackToMessagePriority()
69+
public function testMessagePriorityPrecedesPriority()
9770
{
9871
$message = new PheanstalkMessage('theBody');
9972
$message->setPriority(100);
@@ -112,42 +85,12 @@ public function testNullPriorityFallsBackToMessagePriority()
11285
;
11386

11487
$producer = new PheanstalkProducer($pheanstalk);
115-
$producer->setPriority(null);
116-
117-
$producer->send(
118-
new PheanstalkDestination('theQueueName'),
119-
$message
120-
);
121-
}
122-
123-
public function testPriorityDoesNotPersist()
124-
{
125-
$message = new PheanstalkMessage('theBody');
126-
127-
$pheanstalk = $this->createPheanstalkMock();
128-
$pheanstalk
129-
->expects($this->once())
130-
->method('useTube')
131-
->with('theQueueName')
132-
->willReturnSelf()
133-
;
134-
$pheanstalk
135-
->expects($this->once())
136-
->method('put')
137-
->with('{"body":"theBody","properties":[],"headers":[]}', 100, Pheanstalk::DEFAULT_DELAY, Pheanstalk::DEFAULT_TTR)
138-
;
139-
140-
$producer = new PheanstalkProducer($pheanstalk);
141-
$producer->setPriority(100);
142-
143-
$this->assertEquals(100, $producer->getPriority());
88+
$producer->setPriority(50);
14489

14590
$producer->send(
14691
new PheanstalkDestination('theQueueName'),
14792
$message
14893
);
149-
150-
$this->assertNull($producer->getPriority());
15194
}
15295

15396
public function testAccessDeliveryDelayAsMilliseconds()
@@ -184,34 +127,7 @@ public function testDeliveryDelayResolvesToSeconds()
184127
);
185128
}
186129

187-
public function testDeliveryDelayPrecedesMessageDelay()
188-
{
189-
$message = new PheanstalkMessage('theBody');
190-
$message->setDelay(25);
191-
192-
$pheanstalk = $this->createPheanstalkMock();
193-
$pheanstalk
194-
->expects($this->once())
195-
->method('useTube')
196-
->with('theQueueName')
197-
->willReturnSelf()
198-
;
199-
$pheanstalk
200-
->expects($this->once())
201-
->method('put')
202-
->with('{"body":"theBody","properties":[],"headers":{"delay":25}}', Pheanstalk::DEFAULT_PRIORITY, 1, Pheanstalk::DEFAULT_TTR)
203-
;
204-
205-
$producer = new PheanstalkProducer($pheanstalk);
206-
$producer->setDeliveryDelay(1000);
207-
208-
$producer->send(
209-
new PheanstalkDestination('theQueueName'),
210-
$message
211-
);
212-
}
213-
214-
public function testNullDeliveryDelayFallsBackToMessageDelay()
130+
public function testMessageDelayPrecedesDeliveryDelay()
215131
{
216132
$message = new PheanstalkMessage('theBody');
217133
$message->setDelay(25);
@@ -229,43 +145,13 @@ public function testNullDeliveryDelayFallsBackToMessageDelay()
229145
->with('{"body":"theBody","properties":[],"headers":{"delay":25}}', Pheanstalk::DEFAULT_PRIORITY, 25, Pheanstalk::DEFAULT_TTR)
230146
;
231147

232-
$producer = new PheanstalkProducer($pheanstalk);
233-
$producer->setDeliveryDelay(null);
234-
235-
$producer->send(
236-
new PheanstalkDestination('theQueueName'),
237-
$message
238-
);
239-
}
240-
241-
public function testDeliveryDelayDoesNotPersist()
242-
{
243-
$message = new PheanstalkMessage('theBody');
244-
245-
$pheanstalk = $this->createPheanstalkMock();
246-
$pheanstalk
247-
->expects($this->once())
248-
->method('useTube')
249-
->with('theQueueName')
250-
->willReturnSelf()
251-
;
252-
$pheanstalk
253-
->expects($this->once())
254-
->method('put')
255-
->with('{"body":"theBody","properties":[],"headers":[]}', Pheanstalk::DEFAULT_PRIORITY, 1, Pheanstalk::DEFAULT_TTR)
256-
;
257-
258148
$producer = new PheanstalkProducer($pheanstalk);
259149
$producer->setDeliveryDelay(1000);
260150

261-
$this->assertEquals(1000, $producer->getDeliveryDelay());
262-
263151
$producer->send(
264152
new PheanstalkDestination('theQueueName'),
265153
$message
266154
);
267-
268-
$this->assertNull($producer->getDeliveryDelay());
269155
}
270156

271157
public function testAccessTimeToLiveAsMilliseconds()
@@ -302,34 +188,7 @@ public function testTimeToLiveResolvesToSeconds()
302188
);
303189
}
304190

305-
public function testTimeToLivePrecedesMessageTimeToRun()
306-
{
307-
$message = new PheanstalkMessage('theBody');
308-
$message->setTimeToRun(25);
309-
310-
$pheanstalk = $this->createPheanstalkMock();
311-
$pheanstalk
312-
->expects($this->once())
313-
->method('useTube')
314-
->with('theQueueName')
315-
->willReturnSelf()
316-
;
317-
$pheanstalk
318-
->expects($this->once())
319-
->method('put')
320-
->with('{"body":"theBody","properties":[],"headers":{"ttr":25}}', Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY, 1)
321-
;
322-
323-
$producer = new PheanstalkProducer($pheanstalk);
324-
$producer->setTimeToLive(1000);
325-
326-
$producer->send(
327-
new PheanstalkDestination('theQueueName'),
328-
$message
329-
);
330-
}
331-
332-
public function testNullTimeToLiveFallsBackToMessageTimeToRun()
191+
public function testMessageTimeToRunPrecedesTimeToLive()
333192
{
334193
$message = new PheanstalkMessage('theBody');
335194
$message->setTimeToRun(25);
@@ -347,43 +206,13 @@ public function testNullTimeToLiveFallsBackToMessageTimeToRun()
347206
->with('{"body":"theBody","properties":[],"headers":{"ttr":25}}', Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY, 25)
348207
;
349208

350-
$producer = new PheanstalkProducer($pheanstalk);
351-
$producer->setTimeToLive(null);
352-
353-
$producer->send(
354-
new PheanstalkDestination('theQueueName'),
355-
$message
356-
);
357-
}
358-
359-
public function testTimeToLiveDoesNotPersist()
360-
{
361-
$message = new PheanstalkMessage('theBody');
362-
363-
$pheanstalk = $this->createPheanstalkMock();
364-
$pheanstalk
365-
->expects($this->once())
366-
->method('useTube')
367-
->with('theQueueName')
368-
->willReturnSelf()
369-
;
370-
$pheanstalk
371-
->expects($this->once())
372-
->method('put')
373-
->with('{"body":"theBody","properties":[],"headers":[]}', Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY, 1)
374-
;
375-
376209
$producer = new PheanstalkProducer($pheanstalk);
377210
$producer->setTimeToLive(1000);
378211

379-
$this->assertEquals(1000, $producer->getTimeToLive());
380-
381212
$producer->send(
382213
new PheanstalkDestination('theQueueName'),
383214
$message
384215
);
385-
386-
$this->assertNull($producer->getTimeToLive());
387216
}
388217

389218
/**

0 commit comments

Comments
 (0)