Skip to content

test(NODE-6980): refactor flaky legacy find test to async-await #4559

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 4 commits into from
Jun 11, 2025
Merged
Changes from 3 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
103 changes: 41 additions & 62 deletions test/integration/crud/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { setTimeout } = require('timers');
const { Code, ObjectId, Long, Binary, ReturnDocument, CursorResponse } = require('../../mongodb');

describe('Find', function () {
/** @type(import('../../mongodb').MongoClient */
let client;

beforeEach(async function () {
Expand Down Expand Up @@ -496,70 +497,48 @@ describe('Find', function () {
}
});

it('shouldCorrectlyPerformFindsWithHintTurnedOn', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
it('shouldCorrectlyPerformFindsWithHintTurnedOn', async function () {
const configuration = this.configuration;
client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
await client.connect();

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
db.createCollection('test_hint', function (err, collection) {
collection.insert({ a: 1 }, configuration.writeConcernMax(), function (err) {
expect(err).to.not.exist;
db.createIndex(
collection.collectionName,
'a',
configuration.writeConcernMax(),
function (err) {
expect(err).to.not.exist;
collection.find({ a: 1 }, { hint: 'a' }).toArray(function (err) {
test.ok(err != null);
const db = client.db(configuration.db);
const collection = await db.createCollection('test_hint');

collection.find({ a: 1 }, { hint: ['a'] }).toArray(function (err, items) {
expect(err).to.not.exist;
test.equal(1, items.length);

collection.find({ a: 1 }, { hint: { a: 1 } }).toArray(function (err, items) {
test.equal(1, items.length);

// Modify hints
collection.hint = 'a_1';
test.equal('a_1', collection.hint);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = ['a'];
test.equal(1, collection.hint['a']);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = { a: 1 };
test.equal(1, collection.hint['a']);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);

collection.hint = null;
test.ok(collection.hint == null);
collection.find({ a: 1 }).toArray(function (err, items) {
test.equal(1, items.length);
// Let's close the db
client.close(done);
});
});
});
});
});
});
});
}
);
});
});
});
}
await collection.deleteMany({});
await collection.insert({ a: 1 }, configuration.writeConcernMax());

await db.createIndex(collection.collectionName, 'a', configuration.writeConcernMax());

expect(
await collection
.find({ a: 1 }, { hint: 'a' })
.toArray()
.catch(e => e)
).to.be.instanceOf(Error);

// Test with hint as array
expect(await collection.find({ a: 1 }, { hint: ['a'] }).toArray()).to.have.lengthOf(1);

// Test with hint as object
expect(await collection.find({ a: 1 }, { hint: { a: 1 } }).toArray()).to.have.lengthOf(1);

// Modify hints
collection.hint = 'a_1';
expect(collection.hint).to.equal('a_1');
expect(await collection.find({ a: 1 }).toArray()).to.have.lengthOf(1);

collection.hint = ['a'];
expect(collection.hint['a']).to.equal(1);
expect(await collection.find({ a: 1 }).toArray()).to.have.lengthOf(1);

collection.hint = { a: 1 };
expect(collection.hint['a']).to.equal(1);
expect(await collection.find({ a: 1 }).toArray()).to.have.lengthOf(1);

collection.hint = null;
expect(collection.hint).to.be.undefined;
expect(await collection.find({ a: 1 }).toArray()).to.have.lengthOf(1);
});

it('shouldCorrectlyPerformFindByObjectId', {
Expand Down