-
Notifications
You must be signed in to change notification settings - Fork 917
Split TestCursor into separate test functions. #2179
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
base: master
Are you sure you want to change the base?
Split TestCursor into separate test functions. #2179
Conversation
API Change ReportNo changes found! |
🧪 Performance ResultsCommit SHA: 274e85dThe following benchmark tests for version 68af5c4a782d900007408db1 had statistically significant changes (i.e., |z-score| > 1.96):
For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch. |
There was a problem hiding this 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
, andTestCursor_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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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()
.
Summary
TestCursor
that test specificCursor
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.