|
8 | 8 | type MongoClient,
|
9 | 9 | MongoServerError
|
10 | 10 | } from '../../mongodb';
|
| 11 | +import { filterForCommands } from '../shared'; |
11 | 12 |
|
12 | 13 | const explain = [true, false, 'queryPlanner', 'allPlansExecution', 'executionStats', 'invalid'];
|
13 | 14 |
|
@@ -117,6 +118,184 @@ describe('CRUD API explain option', function () {
|
117 | 118 | });
|
118 | 119 | }
|
119 | 120 | }
|
| 121 | + |
| 122 | + describe('explain helpers w/ maxTimeMS', function () { |
| 123 | + let client: MongoClient; |
| 124 | + const commands: CommandStartedEvent[] = []; |
| 125 | + let collection: Collection; |
| 126 | + |
| 127 | + beforeEach(async function () { |
| 128 | + client = this.configuration.newClient({}, { monitorCommands: true }); |
| 129 | + await client.connect(); |
| 130 | + |
| 131 | + await client.db('explain-test').dropDatabase(); |
| 132 | + collection = await client.db('explain-test').createCollection('bar'); |
| 133 | + |
| 134 | + client.on('commandStarted', filterForCommands('explain', commands)); |
| 135 | + commands.length = 0; |
| 136 | + }); |
| 137 | + |
| 138 | + afterEach(async function () { |
| 139 | + await client.close(); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('maxTimeMS provided to explain, not to command', function () { |
| 143 | + describe('cursor commands', function () { |
| 144 | + describe('options API', function () { |
| 145 | + beforeEach(async function () { |
| 146 | + await collection |
| 147 | + .find({}, { explain: { maxTimeMS: 1000, verbosity: 'queryPlanner' } }) |
| 148 | + .toArray(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('attaches maxTimeMS to the explain command', expectOnExplain(1000)); |
| 152 | + |
| 153 | + it('does not attach maxTimeMS to the find command', expectNotOnCommand()); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('fluent API', function () { |
| 157 | + beforeEach(async function () { |
| 158 | + await collection.find({}).explain({ maxTimeMS: 1000, verbosity: 'queryPlanner' }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('attaches maxTimeMS to the explain command', expectOnExplain(1000)); |
| 162 | + |
| 163 | + it('does not attach maxTimeMS to the find command', expectNotOnCommand()); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('non-cursor commands', function () { |
| 168 | + beforeEach(async function () { |
| 169 | + await collection.deleteMany( |
| 170 | + {}, |
| 171 | + { explain: { maxTimeMS: 1000, verbosity: 'queryPlanner' } } |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it('attaches maxTimeMS to the explain command', expectOnExplain(1000)); |
| 176 | + |
| 177 | + it('does not attach maxTimeMS to the explained command', expectNotOnCommand()); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('maxTimeMS provided to command, not explain', function () { |
| 182 | + describe('cursor commands', function () { |
| 183 | + describe('options API', function () { |
| 184 | + beforeEach(async function () { |
| 185 | + await collection |
| 186 | + .find({}, { maxTimeMS: 1000, explain: { verbosity: 'queryPlanner' } }) |
| 187 | + .toArray(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('does not attach maxTimeMS to the explain command', expectNotOnExplain()); |
| 191 | + |
| 192 | + it('attaches maxTimeMS to the find command', expectOnCommand(1000)); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('fluent API', function () { |
| 196 | + beforeEach(async function () { |
| 197 | + await collection.find({}, { maxTimeMS: 1000 }).explain({ verbosity: 'queryPlanner' }); |
| 198 | + }); |
| 199 | + |
| 200 | + it('does not attach maxTimeMS to the explain command', expectNotOnExplain()); |
| 201 | + |
| 202 | + it('attaches maxTimeMS to the find command', expectOnCommand(1000)); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('non-cursor commands', function () { |
| 207 | + beforeEach(async function () { |
| 208 | + await collection.deleteMany( |
| 209 | + {}, |
| 210 | + { maxTimeMS: 1000, explain: { verbosity: 'queryPlanner' } } |
| 211 | + ); |
| 212 | + }); |
| 213 | + |
| 214 | + it('does nto attach maxTimeMS to the explain command', expectNotOnExplain()); |
| 215 | + |
| 216 | + it('attaches maxTimeMS to the explained command', expectOnCommand(1000)); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + describe('maxTimeMS specified in command options and explain options', function () { |
| 221 | + describe('cursor commands', function () { |
| 222 | + describe('options API', function () { |
| 223 | + beforeEach(async function () { |
| 224 | + await collection |
| 225 | + .find( |
| 226 | + {}, |
| 227 | + { maxTimeMS: 1000, explain: { maxTimeMS: 2000, verbosity: 'queryPlanner' } } |
| 228 | + ) |
| 229 | + .toArray(); |
| 230 | + }); |
| 231 | + |
| 232 | + it('attaches maxTimeMS from the explain options to explain', expectOnExplain(2000)); |
| 233 | + |
| 234 | + it('attaches maxTimeMS from the find options to the find command', expectOnCommand(1000)); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('fluent API', function () { |
| 238 | + beforeEach(async function () { |
| 239 | + await collection |
| 240 | + .find({}, { maxTimeMS: 1000 }) |
| 241 | + .explain({ maxTimeMS: 2000, verbosity: 'queryPlanner' }); |
| 242 | + }); |
| 243 | + |
| 244 | + it('attaches maxTimeMS from the explain options to explain', expectOnExplain(2000)); |
| 245 | + |
| 246 | + it('attaches maxTimeMS from the find options to the find command', expectOnCommand(1000)); |
| 247 | + }); |
| 248 | + }); |
| 249 | + |
| 250 | + describe('non-cursor commands', function () { |
| 251 | + beforeEach(async function () { |
| 252 | + await collection.deleteMany( |
| 253 | + {}, |
| 254 | + { maxTimeMS: 1000, explain: { maxTimeMS: 2000, verbosity: 'queryPlanner' } } |
| 255 | + ); |
| 256 | + }); |
| 257 | + |
| 258 | + it('attaches maxTimeMS to the explain command', expectOnExplain(2000)); |
| 259 | + |
| 260 | + it('attaches maxTimeMS to the explained command', expectOnCommand(1000)); |
| 261 | + }); |
| 262 | + }); |
| 263 | + |
| 264 | + function expectOnExplain(value: number) { |
| 265 | + return function () { |
| 266 | + const [{ command }] = commands; |
| 267 | + expect(command).to.have.property('maxTimeMS', value); |
| 268 | + }; |
| 269 | + } |
| 270 | + |
| 271 | + function expectNotOnExplain() { |
| 272 | + return function () { |
| 273 | + const [{ command }] = commands; |
| 274 | + expect(command).not.to.have.property('maxTimeMS'); |
| 275 | + }; |
| 276 | + } |
| 277 | + |
| 278 | + function expectOnCommand(value: number) { |
| 279 | + return function () { |
| 280 | + const [ |
| 281 | + { |
| 282 | + command: { explain } |
| 283 | + } |
| 284 | + ] = commands; |
| 285 | + expect(explain).to.have.property('maxTimeMS', value); |
| 286 | + }; |
| 287 | + } |
| 288 | + function expectNotOnCommand() { |
| 289 | + return function () { |
| 290 | + const [ |
| 291 | + { |
| 292 | + command: { explain } |
| 293 | + } |
| 294 | + ] = commands; |
| 295 | + expect(explain).not.to.have.property('maxTimeMS'); |
| 296 | + }; |
| 297 | + } |
| 298 | + }); |
120 | 299 | });
|
121 | 300 |
|
122 | 301 | function explainValueToExpectation(explainValue: boolean | string) {
|
|
0 commit comments