Skip to content

Commit 6573ef0

Browse files
committed
Pass timeout in milliseconds
1 parent 42e0100 commit 6573ef0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Diff for: 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;

Diff for: tests/QueryBuilderTest.php

+40
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,41 @@ 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+
{
143+
public function commandStarted(CommandStartedEvent $event)
144+
{
145+
if ($event->getCommandName() !== 'find') {
146+
return;
147+
}
148+
149+
Assert::assertObjectHasAttribute('maxTimeMS', $event->getCommand());
150+
151+
// Expect the timeout to be converted to milliseconds
152+
Assert::assertSame(1000, $event->getCommand()->maxTimeMS);
153+
}
154+
155+
public function commandFailed(CommandFailedEvent $event)
156+
{
157+
}
158+
159+
public function commandSucceeded(CommandSucceededEvent $event)
160+
{
161+
}
162+
};
163+
164+
DB::getMongoClient()->getManager()->addSubscriber($subscriber);
165+
try {
166+
DB::collection('users')->timeout(1)->find($id);
167+
} finally {
168+
DB::getMongoClient()->getManager()->removeSubscriber($subscriber);
169+
}
170+
}
171+
132172
public function testFindNull()
133173
{
134174
$user = DB::collection('users')->find(null);

0 commit comments

Comments
 (0)