-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathconvert_socket_errors.test.ts
138 lines (113 loc) · 4.29 KB
/
convert_socket_errors.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ConnectionPool, type MongoClient, MongoNetworkError } from '../../mongodb';
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
describe('Socket Errors', () => {
describe('when destroyed after write', () => {
let client: MongoClient;
let collection;
beforeEach(async function () {
client = this.configuration.newClient({}, { appName: 'failInserts' });
await client.connect();
const db = client.db('closeConn');
collection = db.collection('closeConn');
const checkOut = sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(fakeCheckout);
async function fakeCheckout(...args) {
const connection = await checkOut.wrappedMethod.call(this, ...args);
const write = sinon.stub(connection.socket, 'write').callsFake(function (...args) {
queueMicrotask(() => {
this.destroy(new Error('read ECONNRESET'));
});
return write.wrappedMethod.call(this, ...args);
});
return connection;
}
});
afterEach(async function () {
sinon.restore();
await client.close();
});
it('throws a MongoNetworkError', async () => {
const error = await collection.insertOne({ name: 'test' }).catch(error => error);
expect(error).to.be.instanceOf(MongoNetworkError);
});
});
describe('when destroyed after read', () => {
let client: MongoClient;
let collection;
const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };
beforeEach(async function () {
if (!this.configuration.filters.NodeVersionFilter.filter({ metadata })) {
return;
}
await configureFailPoint(this.configuration, {
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
appName: 'failInserts',
failCommands: ['insert'],
blockConnection: true,
blockTimeMS: 1000 // just so the server doesn't reply super fast.
}
});
client = this.configuration.newClient({}, { appName: 'failInserts' });
await client.connect();
const db = client.db('closeConn');
collection = db.collection('closeConn');
const checkOut = sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(fakeCheckout);
async function fakeCheckout(...args) {
const connection = await checkOut.wrappedMethod.call(this, ...args);
const on = sinon.stub(connection.messageStream, 'on').callsFake(function (...args) {
if (args[0] === 'data') {
queueMicrotask(() => {
connection.socket.destroy(new Error('read ECONNRESET'));
});
}
return on.wrappedMethod.call(this, ...args);
});
return connection;
}
});
afterEach(async function () {
sinon.restore();
await clearFailPoint(this.configuration);
await client.close();
});
it('throws a MongoNetworkError', metadata, async () => {
const error = await collection.insertOne({ name: 'test' }).catch(error => error);
expect(error).to.be.instanceOf(MongoNetworkError);
});
});
describe('when destroyed by failpoint', () => {
let client: MongoClient;
let collection;
const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };
beforeEach(async function () {
if (!this.configuration.filters.NodeVersionFilter.filter({ metadata })) {
return;
}
await configureFailPoint(this.configuration, {
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
appName: 'failInserts2',
failCommands: ['insert'],
closeConnection: true
}
});
client = this.configuration.newClient({}, { appName: 'failInserts2' });
await client.connect();
const db = client.db('closeConn');
collection = db.collection('closeConn');
});
afterEach(async function () {
sinon.restore();
await clearFailPoint(this.configuration);
await client.close();
});
it('throws a MongoNetworkError', metadata, async () => {
const error = await collection.insertOne({ name: 'test' }).catch(error => error);
expect(error, error.stack).to.be.instanceOf(MongoNetworkError);
});
});
});