Skip to content

Commit

Permalink
Pass timeout in milliseconds (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus authored Nov 10, 2022
1 parent 42e0100 commit dbde512
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dbde512

Please sign in to comment.