Skip to content

Commit 5859889

Browse files
committed
test(NODE-3188): refactor transaction spec tests:
- Adds new unified tests for mongos unpinning. - Refactors legacy tests into legacy directory.
1 parent 03737b6 commit 5859889

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+438
-17
lines changed

src/sessions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,17 @@ function endTransaction(session: ClientSession, commandName: string, callback: C
538538
function commandHandler(e?: MongoError, r?: Document) {
539539
if (commandName === 'commitTransaction') {
540540
session.transaction.transition(TxnState.TRANSACTION_COMMITTED);
541-
542-
if (
543-
e &&
544-
(e instanceof MongoNetworkError ||
545-
e instanceof MongoWriteConcernError ||
546-
isRetryableError(e) ||
547-
isMaxTimeMSExpiredError(e))
548-
) {
549-
if (isUnknownTransactionCommitResult(e)) {
541+
if (e) {
542+
if (e.hasErrorLabel('TransientTransactionError')) {
543+
session.transaction.unpinServer();
544+
} else if (
545+
(e instanceof MongoNetworkError ||
546+
e instanceof MongoWriteConcernError ||
547+
isRetryableError(e) ||
548+
isMaxTimeMSExpiredError(e)) &&
549+
isUnknownTransactionCommitResult(e)
550+
) {
550551
e.addErrorLabel('UnknownTransactionCommitResult');
551-
552552
// per txns spec, must unpin session in this case
553553
session.transaction.unpinServer();
554554
}

src/transactions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ export class Transaction {
144144
const nextStates = stateMachine[this.state];
145145
if (nextStates && nextStates.includes(nextState)) {
146146
this.state = nextState;
147-
if (this.state === TxnState.NO_TRANSACTION || this.state === TxnState.STARTING_TRANSACTION) {
147+
if (
148+
this.state === TxnState.NO_TRANSACTION ||
149+
this.state === TxnState.STARTING_TRANSACTION ||
150+
this.state === TxnState.TRANSACTION_ABORTED
151+
) {
148152
this.unpinServer();
149153
}
150154
return;

test/functional/transactions.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3+
const path = require('path');
34
const { expect } = require('chai');
45
const { Topology } = require('../../src/sdam/topology');
56
const { ClientSession } = require('../../src/sessions');
67
const { TestRunnerContext, generateTopologyTests } = require('./spec-runner');
78
const { loadSpecTests } = require('../spec');
9+
const { runUnifiedTest } = require('./unified-spec-runner/runner');
810
const { MongoNetworkError } = require('../../src/error');
911

1012
function ignoreNsNotFoundForListIndexes(err) {
@@ -79,14 +81,30 @@ class TransactionsRunnerContext extends TestRunnerContext {
7981
}
8082
}
8183

84+
describe('Transactions Spec Unified Tests', function () {
85+
for (const transactionTest of loadSpecTests(path.join('transactions', 'unified'))) {
86+
expect(transactionTest).to.exist;
87+
context(String(transactionTest.description), function () {
88+
for (const test of transactionTest.tests) {
89+
it(String(test.description), {
90+
metadata: { sessions: { skipLeakTests: true } },
91+
test: async function () {
92+
await runUnifiedTest(this, transactionTest, test);
93+
}
94+
});
95+
}
96+
});
97+
}
98+
});
99+
82100
describe('Transactions', function () {
83101
const testContext = new TransactionsRunnerContext();
84102

85103
[
86-
{ name: 'spec tests', specPath: 'transactions' },
104+
{ name: 'spec tests', specPath: path.join('transactions', 'legacy') },
87105
{
88106
name: 'withTransaction spec tests',
89-
specPath: 'transactions/convenient-api'
107+
specPath: path.join('transactions', 'convenient-api')
90108
}
91109
].forEach(suiteSpec => {
92110
describe(suiteSpec.name, function () {

test/spec/transactions/README.rst

Lines changed: 8 additions & 4 deletions

0 commit comments

Comments
 (0)