-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-2014)!: return executor result from withSession and withTransaction #3783
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
Changes from 6 commits
028360b
f3cc100
6bf8cd3
ace3011
070ac00
6225441
2c1cdb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |||||
| MongoNetworkError, | ||||||
| type ServerSessionPool | ||||||
| } from '../../mongodb'; | ||||||
| import { type FailPoint } from '../../tools/utils'; | ||||||
|
|
||||||
| describe('Transactions', function () { | ||||||
| describe('withTransaction', function () { | ||||||
|
|
@@ -104,19 +105,18 @@ describe('Transactions', function () { | |||||
| expect(withTransactionResult).to.be.undefined; | ||||||
| }); | ||||||
|
|
||||||
| it('should return raw command when transaction is successfully committed', async () => { | ||||||
| it('should return result of executor when transaction is successfully committed', async () => { | ||||||
| const session = client.startSession(); | ||||||
|
|
||||||
| const withTransactionResult = await session | ||||||
| .withTransaction(async session => { | ||||||
| await collection.insertOne({ a: 1 }, { session }); | ||||||
| await collection.findOne({ a: 1 }, { session }); | ||||||
| return 'committed!'; | ||||||
| }) | ||||||
| .finally(async () => await session.endSession()); | ||||||
|
|
||||||
| expect(withTransactionResult).to.exist; | ||||||
| expect(withTransactionResult).to.be.an('object'); | ||||||
| expect(withTransactionResult).to.have.property('ok', 1); | ||||||
| expect(withTransactionResult).to.equal('committed!'); | ||||||
| }); | ||||||
|
|
||||||
| it('should throw when transaction is aborted due to an error', async () => { | ||||||
|
|
@@ -136,6 +136,47 @@ describe('Transactions', function () { | |||||
| }); | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| context('when retried', { requires: { mongodb: '>=4.2.0', topology: '!single' } }, () => { | ||||||
| let client: MongoClient; | ||||||
| let collection: Collection<{ a: number }>; | ||||||
|
|
||||||
| beforeEach(async function () { | ||||||
| client = this.configuration.newClient(); | ||||||
|
|
||||||
| await client.db('admin').command({ | ||||||
| configureFailPoint: 'failCommand', | ||||||
| mode: { times: 2 }, | ||||||
| data: { | ||||||
| failCommands: ['commitTransaction'], | ||||||
| errorCode: 24, | ||||||
| errorLabels: ['TransientTransactionError'], | ||||||
| closeConnection: false | ||||||
| } | ||||||
| } as FailPoint); | ||||||
|
|
||||||
| collection = await client.db('withTransaction').createCollection('withTransactionRetry'); | ||||||
| }); | ||||||
|
|
||||||
| afterEach(async () => { | ||||||
| await client?.close(); | ||||||
| }); | ||||||
|
|
||||||
| it('returns the value of the final call to the executor', async () => { | ||||||
| const session = client.startSession(); | ||||||
|
|
||||||
| let counter = 0; | ||||||
| const withTransactionResult = await session | ||||||
| .withTransaction(async session => { | ||||||
| await collection.insertOne({ a: 1 }, { session }); | ||||||
| counter += 1; | ||||||
| return counter; | ||||||
| }) | ||||||
| .finally(async () => await session.endSession()); | ||||||
|
|
||||||
| expect(withTransactionResult).to.equal(counter); | ||||||
|
||||||
| expect(withTransactionResult).to.equal(counter); | |
| expect(withTransactionResult).to.equal(3); |
either we do this, or we somehow assert that the transaction was retried. this test would pass as-is if the failpoint were never triggered
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.
fixed this and the manual abort test
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.
Why'd you revert the
throw?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.
I guess it ends up being equivalent only because the caller is now an
async function, but throwing here is incorrect since it changes the expectation that this function is always promise returning. I don't feel strongly about keeping this, esp with the ticket to refactor this properly coming up.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.
I was just curious, this change is fine. It'll be moot once Malik gets to this code anyways