diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 3c60a071f..6412ab603 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -380,7 +380,7 @@ public function getFresh($columns = [], $returnLazy = false) // Apply order, offset, limit and projection if ($this->timeout) { - $options['maxTimeMS'] = $this->timeout; + $options['maxTimeMS'] = $this->timeout * 1000; } if ($this->orders) { $options['sort'] = $this->orders; diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 11b7404f9..c169071d0 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,12 +5,17 @@ use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; use Illuminate\Support\LazyCollection; +use Illuminate\Testing\Assert; use Jenssegers\Mongodb\Collection; use Jenssegers\Mongodb\Query\Builder; use MongoDB\BSON\ObjectId; use MongoDB\BSON\Regex; use MongoDB\BSON\UTCDateTime; use MongoDB\Driver\Cursor; +use MongoDB\Driver\Monitoring\CommandFailedEvent; +use MongoDB\Driver\Monitoring\CommandStartedEvent; +use MongoDB\Driver\Monitoring\CommandSubscriber; +use MongoDB\Driver\Monitoring\CommandSucceededEvent; class QueryBuilderTest extends TestCase { @@ -129,6 +134,41 @@ public function testFind() $this->assertEquals('John Doe', $user['name']); } + public function testFindWithTimeout() + { + $id = DB::collection('users')->insertGetId(['name' => 'John Doe']); + + $subscriber = new class implements CommandSubscriber + { + public function commandStarted(CommandStartedEvent $event) + { + if ($event->getCommandName() !== 'find') { + return; + } + + Assert::assertObjectHasAttribute('maxTimeMS', $event->getCommand()); + + // Expect the timeout to be converted to milliseconds + Assert::assertSame(1000, $event->getCommand()->maxTimeMS); + } + + public function commandFailed(CommandFailedEvent $event) + { + } + + public function commandSucceeded(CommandSucceededEvent $event) + { + } + }; + + DB::getMongoClient()->getManager()->addSubscriber($subscriber); + try { + DB::collection('users')->timeout(1)->find($id); + } finally { + DB::getMongoClient()->getManager()->removeSubscriber($subscriber); + } + } + public function testFindNull() { $user = DB::collection('users')->find(null);