|
5 | 5 | use Illuminate\Support\Facades\Date;
|
6 | 6 | use Illuminate\Support\Facades\DB;
|
7 | 7 | use Illuminate\Support\LazyCollection;
|
| 8 | +use Illuminate\Testing\Assert; |
8 | 9 | use Jenssegers\Mongodb\Collection;
|
9 | 10 | use Jenssegers\Mongodb\Query\Builder;
|
10 | 11 | use MongoDB\BSON\ObjectId;
|
11 | 12 | use MongoDB\BSON\Regex;
|
12 | 13 | use MongoDB\BSON\UTCDateTime;
|
13 | 14 | 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; |
14 | 19 |
|
15 | 20 | class QueryBuilderTest extends TestCase
|
16 | 21 | {
|
@@ -129,6 +134,41 @@ public function testFind()
|
129 | 134 | $this->assertEquals('John Doe', $user['name']);
|
130 | 135 | }
|
131 | 136 |
|
| 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 | + |
132 | 172 | public function testFindNull()
|
133 | 173 | {
|
134 | 174 | $user = DB::collection('users')->find(null);
|
|
0 commit comments