Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { expect } from 'chai'; | ||
import { inspect } from 'util'; | ||
|
||
import { | ||
Collection, | ||
CommandStartedEvent, | ||
MongoClient, | ||
MongoError, | ||
MongoServerError | ||
} from '../../../src'; | ||
import { runLater, sleep } from '../../tools/utils'; | ||
|
||
describe('MongoDB Cursors', () => { | ||
describe('tailable cursors', () => { | ||
const insertedDocs = [{ _id: 1 }]; | ||
let client: MongoClient; | ||
let cappedCollection: Collection<{ _id: number }>; | ||
beforeEach(async function () { | ||
client = this.configuration.newClient({ monitorCommands: true }); | ||
await client | ||
.db() | ||
.dropCollection('cappedAt3') | ||
.catch(() => null); | ||
await sleep(500); | ||
cappedCollection = await client | ||
.db() | ||
.createCollection('cappedAt3', { capped: true, size: 4096, max: 3 }); | ||
cappedCollection.insertMany(insertedDocs); | ||
}); | ||
|
||
afterEach(async function () { | ||
await client?.close(); | ||
}); | ||
|
||
const tailableValues = [true, false, undefined]; | ||
const awaitDataValues = [true, false, undefined]; | ||
const maxTimeMSValues = [800, 0, undefined]; | ||
const maxAwaitTimeMSValues = [800, 0, undefined]; | ||
|
||
const tests = tailableValues.flatMap(tailable => | ||
awaitDataValues.flatMap(awaitData => | ||
maxAwaitTimeMSValues.flatMap(maxAwaitTimeMS => | ||
maxTimeMSValues.flatMap(maxTimeMS => { | ||
const awaitDataSet = Boolean(awaitData) === true; | ||
const tailableSet = Boolean(tailable) === true; | ||
const timeIsSet = typeof maxAwaitTimeMS === 'number' || typeof maxTimeMS === 'number'; | ||
return [ | ||
{ | ||
options: { tailable, awaitData, maxAwaitTimeMS, maxTimeMS }, | ||
outcome: { | ||
isError: | ||
// Cannot set 'awaitData' without also setting 'tailable' | ||
(awaitDataSet && !tailableSet) || | ||
// cannot set maxTimeMS on getMore command for a non-awaitData cursor | ||
(timeIsSet && awaitData === false && !tailableSet) | ||
} | ||
} | ||
]; | ||
}) | ||
) | ||
) | ||
); | ||
|
||
for (const { options, outcome } of tests) { | ||
let optionsString = inspect(options, { breakLength: Infinity }); | ||
optionsString = optionsString | ||
.slice(2, optionsString.length - 2) | ||
.split('undefined') | ||
.join('omit'); | ||
|
||
it(`should create find cursor with ${optionsString}`, async () => { | ||
const events: CommandStartedEvent[] = []; | ||
client.on('commandStarted', event => events.push(event)); | ||
const cursor = cappedCollection.find({ _id: { $gt: 0 } }, { ...options, batchSize: 1 }); | ||
|
||
const docOrError: { _id: number } | Error = await cursor.next().catch(error => error); | ||
|
||
if (outcome.isError) { | ||
expect(docOrError).to.be.instanceOf(MongoServerError); | ||
} else { | ||
if (docOrError instanceof Error) { | ||
throw docOrError; | ||
} | ||
const doc = docOrError; | ||
|
||
expect(doc).to.have.property('_id', 1); | ||
|
||
await cursor.tryNext(); | ||
|
||
await cursor.close(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); |