Skip to content

Pass timeout in milliseconds #2461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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