|
1 | 1 | import { expect } from 'chai';
|
2 |
| -import * as semver from 'semver'; |
3 | 2 |
|
4 | 3 | import { MongoError, MongoServerError, TopologyType } from '../../../src';
|
5 | 4 |
|
6 |
| -const VALID_TOPOLOGIES = [ |
7 |
| - TopologyType.ReplicaSetWithPrimary, |
8 |
| - TopologyType.Sharded, |
9 |
| - TopologyType.LoadBalanced |
10 |
| -]; |
11 |
| - |
12 | 5 | describe('Retryable Writes Spec Prose', () => {
|
13 |
| - context('when checking against mmapv1', () => { |
14 |
| - /** |
15 |
| - * 1 Test that retryable writes raise an exception when using the MMAPv1 storage engine. |
16 |
| - * For this test, execute a write operation, such as insertOne, which should generate an exception and the error code is 20. |
17 |
| - * Assert that the error message is the replacement error message: |
18 |
| - * |
19 |
| - * ``` |
20 |
| - * This MongoDB deployment does not support retryable writes. Please add |
21 |
| - * retryWrites=false to your connection string. |
22 |
| - * ``` |
23 |
| - * Note: Drivers that rely on serverStatus to determine the storage engine in use MAY skip this test for sharded clusters, since mongos does not report this information in its serverStatus response. |
24 |
| - */ |
25 |
| - let client; |
26 |
| - |
27 |
| - beforeEach(async function () { |
28 |
| - if ( |
29 |
| - this.configuration.buildInfo.versionArray[0] < 4 || |
30 |
| - this.configuration.topologyType !== TopologyType.ReplicaSetWithPrimary |
31 |
| - ) { |
32 |
| - this.currentTest.skipReason = |
33 |
| - 'configureFailPoint only works on server versions greater than 4'; |
34 |
| - this.skip(); |
35 |
| - } |
36 |
| - client = this.configuration.newClient(); |
37 |
| - await client.connect(); |
38 |
| - }); |
39 |
| - |
40 |
| - afterEach(async () => { |
41 |
| - await client?.close(); |
42 |
| - }); |
43 |
| - |
44 |
| - it('retryable writes raise an exception when using the MMAPv1 storage engine', async () => { |
45 |
| - const failPoint = await client.db('admin').command({ |
46 |
| - configureFailPoint: 'failCommand', |
47 |
| - mode: { times: 1 }, |
48 |
| - data: { |
49 |
| - failCommands: ['insert'], |
50 |
| - errorCode: 20, // MMAP Error code, |
51 |
| - closeConnection: false |
52 |
| - } |
53 |
| - }); |
| 6 | + /** |
| 7 | + * 1 Test that retryable writes raise an exception when using the MMAPv1 storage engine. |
| 8 | + * For this test, execute a write operation, such as insertOne, which should generate an exception and the error code is 20. |
| 9 | + * Assert that the error message is the replacement error message: |
| 10 | + * |
| 11 | + * ``` |
| 12 | + * This MongoDB deployment does not support retryable writes. Please add |
| 13 | + * retryWrites=false to your connection string. |
| 14 | + * ``` |
| 15 | + * Note: Drivers that rely on serverStatus to determine the storage engine in use MAY skip this test for sharded clusters, since mongos does not report this information in its serverStatus response. |
| 16 | + */ |
| 17 | + let client; |
54 | 18 |
|
55 |
| - expect(failPoint).to.have.property('ok', 1); |
56 |
| - |
57 |
| - const error = await client |
58 |
| - .db('test') |
59 |
| - .collection('test') |
60 |
| - .insertOne({ a: 1 }) |
61 |
| - .catch(error => error); |
62 |
| - |
63 |
| - expect(error).to.exist; |
64 |
| - expect(error).that.is.instanceOf(MongoServerError); |
65 |
| - expect(error).to.have.property('originalError').that.instanceOf(MongoError); |
66 |
| - expect(error.originalError).to.have.property('code', 20); |
67 |
| - expect(error).to.have.property( |
68 |
| - 'message', |
69 |
| - 'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.' |
70 |
| - ); |
71 |
| - }); |
| 19 | + beforeEach(async function () { |
| 20 | + if ( |
| 21 | + this.configuration.buildInfo.versionArray[0] < 4 || |
| 22 | + this.configuration.topologyType !== TopologyType.ReplicaSetWithPrimary |
| 23 | + ) { |
| 24 | + this.currentTest.skipReason = |
| 25 | + 'configureFailPoint only works on server versions greater than 4'; |
| 26 | + this.skip(); |
| 27 | + } |
| 28 | + client = this.configuration.newClient(); |
| 29 | + await client.connect(); |
72 | 30 | });
|
73 | 31 |
|
74 |
| - context('when errors occur in the handshake', function () { |
75 |
| - const dbName = 'retryable-handshake-tests'; |
76 |
| - const collName = 'coll'; |
77 |
| - const docs = [{ _id: 1, x: 11 }]; |
78 |
| - let client; |
79 |
| - let db; |
80 |
| - let coll; |
| 32 | + afterEach(async () => { |
| 33 | + await client?.close(); |
| 34 | + }); |
81 | 35 |
|
82 |
| - beforeEach(function () { |
83 |
| - if ( |
84 |
| - semver.lt(this.configuration.buildInfo.version, '4.2.0') || |
85 |
| - !VALID_TOPOLOGIES.includes(this.configuration.topologyType) |
86 |
| - ) { |
87 |
| - this.currentTest.skipReason = |
88 |
| - 'Retryable writes tests require MongoDB 4.2 and higher and no standalone'; |
89 |
| - this.skip(); |
| 36 | + it('retryable writes raise an exception when using the MMAPv1 storage engine', async () => { |
| 37 | + const failPoint = await client.db('admin').command({ |
| 38 | + configureFailPoint: 'failCommand', |
| 39 | + mode: { times: 1 }, |
| 40 | + data: { |
| 41 | + failCommands: ['insert'], |
| 42 | + errorCode: 20, // MMAP Error code, |
| 43 | + closeConnection: false |
90 | 44 | }
|
91 |
| - client = this.configuration.newClient({}); |
92 |
| - db = client.db(dbName); |
93 |
| - coll = db.collection(collName); |
94 | 45 | });
|
95 | 46 |
|
96 |
| - afterEach(async function () { |
97 |
| - await db?.admin().command({ |
98 |
| - configureFailPoint: 'failCommand', |
99 |
| - mode: 'off' |
100 |
| - }); |
101 |
| - await coll?.drop(); |
102 |
| - await client?.close(); |
103 |
| - }); |
| 47 | + expect(failPoint).to.have.property('ok', 1); |
104 | 48 |
|
105 |
| - context('when the handshake fails with a network error', function () { |
106 |
| - it('retries the write', async function () { |
107 |
| - await client.connect(); |
108 |
| - await coll.insertMany(docs); |
109 |
| - await db.admin().command({ |
110 |
| - configureFailPoint: 'failCommand', |
111 |
| - mode: { times: 2 }, |
112 |
| - data: { |
113 |
| - failCommands: ['saslContinue', 'ping'], |
114 |
| - closeConnection: true |
115 |
| - } |
116 |
| - }); |
117 |
| - const result = await coll.insertOne({ _id: 2, x: 22 }); |
118 |
| - expect(result.insertedId).to.equal(2); |
119 |
| - }); |
120 |
| - }); |
| 49 | + const error = await client |
| 50 | + .db('test') |
| 51 | + .collection('test') |
| 52 | + .insertOne({ a: 1 }) |
| 53 | + .catch(error => error); |
121 | 54 |
|
122 |
| - context('when the handshake fails with shutdown in progress', function () { |
123 |
| - it('retries the write', async function () { |
124 |
| - await client.connect(); |
125 |
| - await coll.insertMany(docs); |
126 |
| - await db.admin().command({ |
127 |
| - configureFailPoint: 'failCommand', |
128 |
| - mode: { times: 2 }, |
129 |
| - data: { |
130 |
| - failCommands: ['saslContinue', 'ping'], |
131 |
| - errorCode: 91 // ShutdownInProgress |
132 |
| - } |
133 |
| - }); |
134 |
| - const result = await coll.insertOne({ _id: 2, x: 22 }); |
135 |
| - expect(result.insertedId).to.equal(2); |
136 |
| - }); |
137 |
| - }); |
| 55 | + expect(error).to.exist; |
| 56 | + expect(error).that.is.instanceOf(MongoServerError); |
| 57 | + expect(error).to.have.property('originalError').that.instanceOf(MongoError); |
| 58 | + expect(error.originalError).to.have.property('code', 20); |
| 59 | + expect(error).to.have.property( |
| 60 | + 'message', |
| 61 | + 'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.' |
| 62 | + ); |
138 | 63 | });
|
139 | 64 | });
|
0 commit comments