|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const { expect } = require('chai'); |
| 5 | +const { TestRunnerContext, generateTopologyTests } = require('../../tools/spec-runner'); |
| 6 | +const { runUnifiedTest } = require('../../tools/unified-spec-runner/runner'); |
| 7 | +const { loadSpecTests } = require('../../spec'); |
| 8 | + |
| 9 | +function ignoreNsNotFoundForListIndexes(err) { |
| 10 | + if (err.code !== 26) { |
| 11 | + throw err; |
| 12 | + } |
| 13 | + |
| 14 | + return []; |
| 15 | +} |
| 16 | + |
| 17 | +class TransactionsRunnerContext extends TestRunnerContext { |
| 18 | + assertCollectionExists(options) { |
| 19 | + const client = this.sharedClient; |
| 20 | + const db = client.db(options.database); |
| 21 | + const collectionName = options.collection; |
| 22 | + |
| 23 | + return db |
| 24 | + .listCollections() |
| 25 | + .toArray() |
| 26 | + .then(collections => expect(collections.some(coll => coll.name === collectionName)).to.be.ok); |
| 27 | + } |
| 28 | + |
| 29 | + assertCollectionNotExists(options) { |
| 30 | + const client = this.sharedClient; |
| 31 | + const db = client.db(options.database); |
| 32 | + const collectionName = options.collection; |
| 33 | + |
| 34 | + return db |
| 35 | + .listCollections() |
| 36 | + .toArray() |
| 37 | + .then( |
| 38 | + collections => expect(collections.every(coll => coll.name !== collectionName)).to.be.ok |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + assertIndexExists(options) { |
| 43 | + const client = this.sharedClient; |
| 44 | + const collection = client.db(options.database).collection(options.collection); |
| 45 | + const indexName = options.index; |
| 46 | + |
| 47 | + return collection |
| 48 | + .listIndexes() |
| 49 | + .toArray() |
| 50 | + .catch(ignoreNsNotFoundForListIndexes) |
| 51 | + .then(indexes => expect(indexes.some(idx => idx.name === indexName)).to.be.ok); |
| 52 | + } |
| 53 | + |
| 54 | + assertIndexNotExists(options) { |
| 55 | + const client = this.sharedClient; |
| 56 | + const collection = client.db(options.database).collection(options.collection); |
| 57 | + const indexName = options.index; |
| 58 | + |
| 59 | + return collection |
| 60 | + .listIndexes() |
| 61 | + .toArray() |
| 62 | + .catch(ignoreNsNotFoundForListIndexes) |
| 63 | + .then(indexes => expect(indexes.every(idx => idx.name !== indexName)).to.be.ok); |
| 64 | + } |
| 65 | + |
| 66 | + assertSessionPinned(options) { |
| 67 | + expect(options).to.have.property('session'); |
| 68 | + |
| 69 | + const session = options.session; |
| 70 | + expect(session.isPinned).to.be.true; |
| 71 | + } |
| 72 | + |
| 73 | + assertSessionUnpinned(options) { |
| 74 | + expect(options).to.have.property('session'); |
| 75 | + |
| 76 | + const session = options.session; |
| 77 | + expect(session.isPinned).to.be.false; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +describe('Transactions Spec Unified Tests', function () { |
| 82 | + for (const transactionTest of loadSpecTests(path.join('transactions', 'unified'))) { |
| 83 | + expect(transactionTest).to.exist; |
| 84 | + context(String(transactionTest.description), function () { |
| 85 | + for (const test of transactionTest.tests) { |
| 86 | + it(String(test.description), { |
| 87 | + metadata: { sessions: { skipLeakTests: true } }, |
| 88 | + test: async function () { |
| 89 | + await runUnifiedTest(this, transactionTest, test); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +const SKIP_TESTS = [ |
| 98 | + // commitTransaction retry seems to be swallowed by mongos in these three cases |
| 99 | + 'commitTransaction retry succeeds on new mongos', |
| 100 | + 'commitTransaction retry fails on new mongos', |
| 101 | + 'unpin after transient error within a transaction and commit', |
| 102 | + // FIXME(NODE-3074): unskip count tests when spec tests have been updated |
| 103 | + 'count', |
| 104 | + // This test needs there to be multiple mongoses |
| 105 | + // 'increment txnNumber', |
| 106 | + // Skipping this until SPEC-1320 is resolved |
| 107 | + // 'remain pinned after non-transient error on commit', |
| 108 | + |
| 109 | + // Will be implemented as part of NODE-2034 |
| 110 | + 'Client side error in command starting transaction', |
| 111 | + 'Client side error when transaction is in progress' |
| 112 | +]; |
| 113 | + |
| 114 | +describe('Transactions Spec Legacy Tests', function () { |
| 115 | + const testContext = new TransactionsRunnerContext(); |
| 116 | + const suitesToRun = [{ name: 'spec tests', specPath: path.join('transactions', 'legacy') }]; |
| 117 | + // Note: convenient-api tests are skipped for serverless |
| 118 | + if (!process.env.SERVERLESS) { |
| 119 | + suitesToRun.push({ |
| 120 | + name: 'withTransaction spec tests', |
| 121 | + specPath: path.join('transactions', 'convenient-api') |
| 122 | + }); |
| 123 | + } else { |
| 124 | + // FIXME(NODE-3550): these tests should pass on serverless but currently fail |
| 125 | + SKIP_TESTS.push( |
| 126 | + 'abortTransaction only performs a single retry', |
| 127 | + 'abortTransaction does not retry after Interrupted', |
| 128 | + 'abortTransaction does not retry after WriteConcernError Interrupted', |
| 129 | + 'commitTransaction does not retry error without RetryableWriteError label', |
| 130 | + 'commitTransaction is not retried after UnsatisfiableWriteConcern error', |
| 131 | + 'commitTransaction fails after Interrupted' |
| 132 | + ); |
| 133 | + } |
| 134 | + suitesToRun.forEach(suiteSpec => { |
| 135 | + describe(suiteSpec.name, function () { |
| 136 | + const testSuites = loadSpecTests(suiteSpec.specPath); |
| 137 | + after(() => testContext.teardown()); |
| 138 | + before(function () { |
| 139 | + return testContext.setup(this.configuration); |
| 140 | + }); |
| 141 | + |
| 142 | + function testFilter(spec) { |
| 143 | + return SKIP_TESTS.indexOf(spec.description) === -1; |
| 144 | + } |
| 145 | + |
| 146 | + generateTopologyTests(testSuites, testContext, testFilter); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments