Skip to content

Commit 3cc4741

Browse files
committed
test(retryable-writes): fix failpoint setup and teardown
NODE-1105
1 parent 5ce3437 commit 3cc4741

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

test/functional/retryable_writes_tests.js

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict';
22

3-
var fs = require('fs'),
3+
const fs = require('fs'),
44
path = require('path'),
55
expect = require('chai').expect;
66

77
let testContext = {};
88
describe('Retryable Writes', function() {
99
before(function() {
10-
var configuration = this.configuration;
11-
var MongoClient = configuration.require.MongoClient;
12-
return MongoClient.connect(configuration.url()).then(function(client) {
10+
const configuration = this.configuration;
11+
const MongoClient = configuration.require.MongoClient;
12+
const url = `${configuration.url()}&retryWrites=true`;
13+
14+
return MongoClient.connect(url, { minSize: 1 }).then(function(client) {
1315
testContext.client = client;
1416
testContext.db = client.db(configuration.db);
1517
});
@@ -30,32 +32,32 @@ describe('Retryable Writes', function() {
3032

3133
describe(methodName, function() {
3234
scenario.tests.forEach(test => {
33-
beforeEach(function() {
34-
if (test.failpoint) {
35-
return testContext.db.command({
36-
configureFailPoint: 'onPrimaryTransactionalWrite',
37-
mode: test.failpoint.mode,
38-
data: test.failpoint.data
39-
});
40-
}
41-
});
42-
43-
afterEach(function() {
44-
if (test.failpoint) {
45-
return testContext.db.command({
46-
configureFailPoint: 'onPrimaryTransactionalWrite',
47-
mode: 'off'
48-
});
49-
}
50-
});
51-
5235
it(test.description, {
5336
metadata: {
5437
requires: { topology: ['single'], mongodb: '>=' + scenario.minServerVersion }
5538
},
5639

5740
test: function() {
58-
return executeScenarioTest(scenario, test, this.configuration, testContext);
41+
let setupPromise;
42+
if (test.failPoint) {
43+
const command = { configureFailPoint: 'onPrimaryTransactionalWrite' };
44+
if (test.failPoint.mode) command.mode = test.failPoint.mode;
45+
if (test.failPoint.data) command.data = test.failPoint.data;
46+
return testContext.db.executeDbAdminCommand(command);
47+
} else {
48+
setupPromise = Promise.resolve();
49+
}
50+
51+
return setupPromise
52+
.then(() => executeScenarioTest(scenario, test, this.configuration, testContext))
53+
.then(() => {
54+
if (test.failPoint) {
55+
return testContext.db.executeDbAdminCommand({
56+
configureFailPoint: 'onPrimaryTransactionalWrite',
57+
mode: 'off'
58+
});
59+
}
60+
});
5961
}
6062
});
6163
});
@@ -70,7 +72,7 @@ const convertBulkWriteOperation = op => {
7072
};
7173

7274
function executeScenarioTest(scenario, test, configuration, context) {
73-
var collection = context.db.collection(
75+
const collection = context.db.collection(
7476
'retryable_writes_test_' + scenario.name + '_' + test.operation.name
7577
);
7678

@@ -104,7 +106,10 @@ function executeScenarioTest(scenario, test, configuration, context) {
104106

105107
let result = collection[test.operation.name].apply(collection, args);
106108
if (test.outcome.error) {
107-
result = result.then(() => expect(false).to.be.true).catch(err => expect(err).to.exist);
109+
result = result.then(() => expect(false).to.be.true).catch(err => {
110+
expect(err).to.exist;
111+
expect(err.message).to.not.match(/expected false to be true/);
112+
});
108113
} else if (test.outcome.result) {
109114
result = result.then(r => expect(r).to.deep.include(test.outcome.result));
110115
}
@@ -116,7 +121,7 @@ function executeScenarioTest(scenario, test, configuration, context) {
116121
return collection
117122
.find({})
118123
.toArray()
119-
.then(function(collectionResults) {
124+
.then(collectionResults => {
120125
expect(collectionResults).to.eql(test.outcome.collection.data);
121126
});
122127
}

0 commit comments

Comments
 (0)