Skip to content

Commit 96c4a24

Browse files
committed
Fix millisecond / max int / second issue
1 parent 99bb398 commit 96c4a24

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/Queue.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
*/
1414
final class Queue implements QueueInterface
1515
{
16-
const MONGO_INT32_MAX = 2147483647;//2147483648 can overflow in php mongo without using the MongoInt64
16+
/**
17+
* Maximum millisecond value to use for UTCDateTime creation.
18+
*
19+
* @var integer
20+
*/
21+
const MONGO_INT32_MAX = PHP_INT_MAX;
1722

1823
/**
1924
* mongo collection to use for queue.
@@ -178,7 +183,9 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
178183
$resetTimestamp = $runningResetDuration > 0 ? self::MONGO_INT32_MAX : 0;
179184
}
180185

181-
$update = ['$set' => ['resetTimestamp' => new \MongoDB\BSON\UTCDateTime($resetTimestamp * 1000), 'running' => true]];
186+
$resetTimestamp = min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX);
187+
188+
$update = ['$set' => ['resetTimestamp' => new \MongoDB\BSON\UTCDateTime($resetTimestamp), 'running' => true]];
182189
$options = ['sort' => ['priority' => 1, 'created' => 1]];
183190

184191
//ints overflow to floats, should be fine
@@ -388,13 +395,13 @@ public function send(array $payload, $earliestGet = 0, $priority = 0.0)
388395
}
389396

390397
//Ensure $earliestGet is between 0 and MONGO_INT32_MAX
391-
$earliestGet = min(max(0, $earliestGet), self::MONGO_INT32_MAX);
398+
$earliestGet = min(max(0, $earliestGet * 1000), self::MONGO_INT32_MAX);
392399

393400
$message = [
394401
'payload' => $payload,
395402
'running' => false,
396-
'resetTimestamp' => new \MongoDB\BSON\UTCDateTime(self::MONGO_INT32_MAX * 1000),
397-
'earliestGet' => new \MongoDB\BSON\UTCDateTime($earliestGet * 1000),
403+
'resetTimestamp' => new \MongoDB\BSON\UTCDateTime(self::MONGO_INT32_MAX),
404+
'earliestGet' => new \MongoDB\BSON\UTCDateTime($earliestGet),
398405
'priority' => $priority,
399406
'created' => new \MongoDB\BSON\UTCDateTime((int)(microtime(true) * 1000)),
400407
];

tests/QueueTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace DominionEnterprises\Mongo;
44

5+
use MongoDB\BSON\UTCDateTime;
6+
57
/**
68
* @coversDefaultClass \DominionEnterprises\Mongo\Queue
79
* @covers ::<private>
@@ -603,6 +605,26 @@ public function ackSend()
603605
$this->assertSame($expected, $actual);
604606
}
605607

608+
/**
609+
* Verify earliestGet with ackSend.
610+
*
611+
* @test
612+
* @covers ::ackSend
613+
*
614+
* @return void
615+
*/
616+
public function ackSendWithEarliestGet()
617+
{
618+
$message = ['key1' => 0, 'key2' => true];
619+
$this->queue->send($message);
620+
$result = $this->queue->get([], PHP_INT_MAX, 0);
621+
$this->assertSame($message['key1'], $result['key1']);
622+
$this->assertSame($message['key2'], $result['key2']);
623+
$this->queue->ackSend($result, ['key1' => 1, 'key2' => 2], strtotime('+ 1 day'));
624+
$actual = $this->queue->get([], PHP_INT_MAX, 0);
625+
$this->assertNull($actual);
626+
}
627+
606628
/**
607629
* @test
608630
* @covers ::ackSend
@@ -765,7 +787,7 @@ public function send()
765787
$expected = [
766788
'payload' => $payload,
767789
'running' => false,
768-
'resetTimestamp' => Queue::MONGO_INT32_MAX,
790+
'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
769791
'earliestGet' => 34,
770792
'priority' => 0.8,
771793
];
@@ -823,8 +845,8 @@ public function sendWithHighEarliestGet()
823845
$expected = [
824846
'payload' => [],
825847
'running' => false,
826-
'resetTimestamp' => Queue::MONGO_INT32_MAX,
827-
'earliestGet' => Queue::MONGO_INT32_MAX,
848+
'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
849+
'earliestGet' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
828850
'priority' => 0.0,
829851
];
830852

@@ -851,7 +873,7 @@ public function sendWithLowEarliestGet()
851873
$expected = [
852874
'payload' => [],
853875
'running' => false,
854-
'resetTimestamp' => Queue::MONGO_INT32_MAX,
876+
'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
855877
'earliestGet' => 0,
856878
'priority' => 0.0,
857879
];
@@ -889,7 +911,7 @@ public function constructWithCollection()
889911
$expected = [
890912
'payload' => $payload,
891913
'running' => false,
892-
'resetTimestamp' => Queue::MONGO_INT32_MAX,
914+
'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
893915
'earliestGet' => 34,
894916
'priority' => 0.8,
895917
];

0 commit comments

Comments
 (0)