Skip to content

Conversation

tadjik1
Copy link
Member

@tadjik1 tadjik1 commented Oct 13, 2025

Description

Summary of Changes

This PR migrates the integration tests for crud/find_cursor_methods. The changes include:

  • Convert file in typescript
  • Refactor callbacks to async/await
  • Direct import from the src folder
Notes for Reviewers

To simplify the review process there is a separate commit for .js -> .ts conversion.

What is the motivation for this change?

This work is part of a larger, ongoing initiative to convert all tests to use async/await, with the ultimate goal of removing the legacy driver wrapper.

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@tadjik1 tadjik1 marked this pull request as ready for review October 13, 2025 12:19
@tadjik1 tadjik1 requested a review from a team as a code owner October 13, 2025 12:19
@baileympearson baileympearson self-assigned this Oct 13, 2025
@baileympearson baileympearson added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Oct 13, 2025
Comment on lines 175 to 186
context('iterator', function () {
it('should iterate each document in a cursor', async function () {
const coll = client.db().collection('abstract_cursor');
const cursor = coll.find({}, { batchSize: 2 });

const bag = [];
cursor.forEach(
doc => bag.push(doc),
err => {
expect(err).to.not.exist;
expect(bag).to.have.lengthOf(6);
done();
}
);
for await (const doc of cursor) {
bag.push(doc);
}
expect(bag).to.have.lengthOf(6);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this method changed? We still support AbstractCursor.forEach().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have adequate coverage elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, sorry for that confusion, I misinterpreted deprecated notation

Comment on lines 418 to 421
let doc: any;
while ((doc = (await cursor.next()) && doc != null)) {
/** empty */
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of casting, why not:

while ((await cursor.next()) != null) {
  /** empty */
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is much easier, thanks!

await cursor.next();

let doc;
let doc: any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same comment)

await cursor.next();

let doc;
let doc: any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same comment)

addaleax
addaleax previously approved these changes Oct 14, 2025

const coll = client.db().collection('abstract_cursor');
const cursor = coll.find({}, { batchSize: 2 });
this.defer(() => cursor.close());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These .close() calls are gone without replacement, right?

I think they were superfluous anyway because the tests exhausted the respective cursors, maybe it's worth and possible to check that they are already closed afterwards?

Copy link
Member Author

@tadjik1 tadjik1 Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question! Yes, we are re-creating client for every test within beforeEach hook and close client in afterEach, something like:

let client: MongoClient;

beforeEach(function () {
  client = this.configuration.newClient();
});

afterEach(async function () {
  await client.close();
});

And in addition to that we do check for opened clients after tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I wasn't concerned about resource leakage here – I guess what I'm saying is that previously, we verified that it's acceptable to close the cursor after exhausting it, and now we don't do this anymore (at least not in these tests). No strong feelings, but I was wondering if it's something worth keeping in some form, either by verifying that cursor.close() still works (not just client.close()), or by verifying that the cursor is already closed anyway

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that was a misunderstanding, after all these tests refactoring I think I lost a perspective and misinterpreted cursor with client (as it's also very common in our tests to close client in tests and not hooks). Thanks for paying attention to this.

I don't have opinion on this, down the file we have a dedicated describe('#close', function () { which tests cursor.close() in different contexts (before initialization, after iterations, etc.), so I think it would be ok to remove it from other places and close the entire client in other tests (like this one). What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong feelings about whether to keep it or drop it – just wanted to make sure this change was intentional 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Primary Review In Review with primary reviewer, not yet ready for team's eyes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants