-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathconvert_socket_errors.test.ts
More file actions
78 lines (65 loc) · 2.56 KB
/
convert_socket_errors.test.ts
File metadata and controls
78 lines (65 loc) · 2.56 KB
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
import { Duplex } from 'node:stream';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Connection, type MongoClient, MongoNetworkError, ns } from '../../mongodb';
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
describe('Socket Errors', () => {
describe('when a socket emits an error', () => {
it('command throws a MongoNetworkError', async () => {
const socket = new Duplex();
// @ts-expect-error: not a real socket
const connection = new Connection(socket, {});
const testError = new Error('blah');
socket.emit('error', testError);
const commandRes = connection.command(ns('a.b'), { ping: 1 }, {});
const error = await commandRes.catch(error => error);
expect(error).to.be.instanceOf(MongoNetworkError);
expect(error.cause).to.equal(testError);
});
});
describe('when the sized message stream emits an error', () => {
it('command throws the same error', async () => {
const socket = new Duplex();
// @ts-expect-error: not a real socket
const connection = new Connection(socket, {});
const testError = new Error('blah');
// @ts-expect-error: private field
connection.messageStream.emit('error', testError);
const commandRes = connection.command(ns('a.b'), { ping: 1 }, {});
const error = await commandRes.catch(error => error);
expect(error).to.equal(testError);
});
});
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);
});
});
});