Skip to content

Conversation

matthewdale
Copy link
Collaborator

@matthewdale matthewdale commented Aug 27, 2025

Summary

  • Move subtests in TestCursor that test specific Cursor methods into separate test functions.

Background & Motivation

Very long test functions with deeply-nested subtests are difficult to read, maintain, and run. It's better to split up some subtests into separate test functions.

Copy link
Contributor

API Change Report

No changes found!

Copy link
Contributor

🧪 Performance Results

Commit SHA: 274e85d

The following benchmark tests for version 68af5c4a782d900007408db1 had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkMultiFindMany total_time_seconds 20.7604 1.3402 Avg: 1.1098
Med: 1.0551
Stdev: 0.1054
0.7560 2.1869
BenchmarkLargeDocInsertOne total_time_seconds 5.7963 1.2508 Avg: 1.1823
Med: 1.1821
Stdev: 0.0235
0.8085 2.9112

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@matthewdale matthewdale added enhancement review-priority-low Low Priority PR for Review: within 3 business days ignore-for-release labels Aug 27, 2025
@matthewdale matthewdale marked this pull request as ready for review August 27, 2025 20:08
@Copilot Copilot AI review requested due to automatic review settings August 27, 2025 20:09
@matthewdale matthewdale requested a review from a team as a code owner August 27, 2025 20:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request refactors the TestCursor function by splitting its nested subtests into separate, independent test functions. This improves test maintainability and readability by reducing the complexity of deeply-nested test structures.

Key changes:

  • Extracted four subtests from TestCursor into standalone test functions: TestCursor_TryNext, TestCursor_RemainingBatchLength, TestCursor_All, and TestCursor_Close
  • Moved the "set batchSize" subtest to remain within the original TestCursor function
  • Relocated variable declarations to their appropriate scopes in the new test functions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


var docs []bson.D
err = cursor.All(context.Background(), &docs)
assert.NotNil(mt, err, "expected change stream error, got nil")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.NotNil(mt, err, "expected change stream error, got nil")
require.Error(mt, err, "expected change stream error, got nil")

// Call All with the canceled context and expect context.Canceled.
var docs []bson.D
err = cur.All(canceledCtx, &docs)
assert.NotNil(mt, err, "expected error for All, got nil")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.NotNil(mt, err, "expected error for All, got nil")
require.Error(mt, err, "expected error for All, got nil")

// Find with batchSize 2 so All will run getMore for next 3 docs and error.
cur, err := mt.Coll.Find(context.Background(), bson.D{},
options.Find().SetBatchSize(2))
assert.Nil(mt, err, "Find error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "Find error: %v", err)
require.NoError(mt, err, "Find error: %v", err)

assert.Nil(mt, err, "Find error: %v", err)

err = cursor.Close(context.Background())
assert.NotNil(mt, err, "expected change stream error, got nil")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.NotNil(mt, err, "expected change stream error, got nil")
require.Error(mt, err, "expected change stream error, got nil")

})
initCollection(mt, mt.Coll)
cursor, err := mt.Coll.Find(context.Background(), bson.D{}, options.Find().SetBatchSize(2))
assert.Nil(mt, err, "Find error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "Find error: %v", err)
require.NoError(mt, err, "Find error: %v", err)

findRes := mtest.CreateCursorResponse(50, "foo.bar", mtest.FirstBatch)
mt.AddMockResponses(findRes)
cursor, err := mt.Coll.Find(context.Background(), bson.D{})
assert.Nil(mt, err, "Find error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "Find error: %v", err)
require.NoError(mt, err, "Find error: %v", err)

assert.Nil(mt, err, "InsertOne error: %v", err)

cursor, err := mt.Coll.Find(context.Background(), bson.D{}, options.Find().SetCursorType(options.Tailable))
assert.Nil(mt, err, "Find error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "Find error: %v", err)
require.NoError(mt, err, "Find error: %v", err)

// insert a document because a tailable cursor will only have a non-zero ID if the initial Find matches
// at least one document
_, err := mt.Coll.InsertOne(context.Background(), bson.D{{"x", 1}})
assert.Nil(mt, err, "InsertOne error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "InsertOne error: %v", err)
require.NoError(mt, err, "InsertOne error: %v", err)


initCollection(mt, mt.Coll)
cursor, err := mt.Coll.Find(context.Background(), bson.D{})
assert.Nil(mt, err, "Find error: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert.Nil(mt, err, "Find error: %v", err)
require.NoError(mt, err, "Find error: %v", err)


// The initial batch length should be equal to the batchSize. Do batchSize Next calls to exhaust the current
// batch and assert that no getMore was done.
assertCursorBatchLength(mt, cursor, batchSize)
Copy link
Member

Choose a reason for hiding this comment

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

It's not part of the changes in this PR but assertCursorBatchLength should call mt.Helper().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ignore-for-release review-priority-low Low Priority PR for Review: within 3 business days
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants