Skip to content

Commit 8e743b3

Browse files
committed
Pass timeout in milliseconds
1 parent 42e0100 commit 8e743b3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Query/Builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function getFresh($columns = [], $returnLazy = false)
380380

381381
// Apply order, offset, limit and projection
382382
if ($this->timeout) {
383-
$options['maxTimeMS'] = $this->timeout;
383+
$options['maxTimeMS'] = $this->timeout * 1000;
384384
}
385385
if ($this->orders) {
386386
$options['sort'] = $this->orders;

tests/QueryBuilderTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
use Illuminate\Support\Facades\Date;
66
use Illuminate\Support\Facades\DB;
77
use Illuminate\Support\LazyCollection;
8+
use Illuminate\Testing\Assert;
89
use Jenssegers\Mongodb\Collection;
910
use Jenssegers\Mongodb\Query\Builder;
1011
use MongoDB\BSON\ObjectId;
1112
use MongoDB\BSON\Regex;
1213
use MongoDB\BSON\UTCDateTime;
1314
use MongoDB\Driver\Cursor;
15+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
16+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
17+
use MongoDB\Driver\Monitoring\CommandSubscriber;
18+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
1419

1520
class QueryBuilderTest extends TestCase
1621
{
@@ -129,6 +134,40 @@ public function testFind()
129134
$this->assertEquals('John Doe', $user['name']);
130135
}
131136

137+
public function testFindWithTimeout()
138+
{
139+
$id = DB::collection('users')->insertGetId(['name' => 'John Doe']);
140+
141+
$subscriber = new class implements CommandSubscriber {
142+
public function commandStarted(CommandStartedEvent $event)
143+
{
144+
if ($event->getCommandName() !== 'find') {
145+
return;
146+
}
147+
148+
Assert::assertObjectHasAttribute('maxTimeMS', $event->getCommand());
149+
150+
// Expect the timeout to be converted to milliseconds
151+
Assert::assertSame(1000, $event->getCommand()->maxTimeMS);
152+
}
153+
154+
public function commandFailed(CommandFailedEvent $event)
155+
{
156+
}
157+
158+
public function commandSucceeded(CommandSucceededEvent $event)
159+
{
160+
}
161+
};
162+
163+
DB::getMongoClient()->getManager()->addSubscriber($subscriber);
164+
try {
165+
DB::collection('users')->timeout(1)->find($id);
166+
} finally {
167+
DB::getMongoClient()->getManager()->removeSubscriber($subscriber);
168+
}
169+
}
170+
132171
public function testFindNull()
133172
{
134173
$user = DB::collection('users')->find(null);

0 commit comments

Comments
 (0)