Skip to content

Commit 60a8c66

Browse files
committed
test: network error wrapping
1 parent 7b0caf9 commit 60a8c66

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

test/integration/node-specific/convert_socket_errors.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1+
import { Duplex } from 'node:stream';
2+
13
import { expect } from 'chai';
24
import * as sinon from 'sinon';
35

4-
import { ConnectionPool, type MongoClient, MongoNetworkError } from '../../mongodb';
6+
import { Connection, ConnectionPool, type MongoClient, MongoNetworkError, ns } from '../../mongodb';
57
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
68

79
describe('Socket Errors', () => {
10+
describe('when a socket emits an error', () => {
11+
it('command throws a MongoNetworkError', async () => {
12+
const socket = new Duplex();
13+
// @ts-expect-error: not a real socket
14+
const connection = new Connection(socket, {});
15+
const testError = new Error('blah');
16+
socket.emit('error', testError);
17+
const commandRes = connection.command(ns('a.b'), { ping: 1 }, {});
18+
19+
const error = await commandRes.catch(error => error);
20+
expect(error).to.be.instanceOf(MongoNetworkError);
21+
expect(error.cause).to.equal(testError);
22+
});
23+
});
24+
25+
describe('when the sized message stream emits an error', () => {
26+
it('command throws the same error', async () => {
27+
const socket = new Duplex();
28+
// @ts-expect-error: not a real socket
29+
const connection = new Connection(socket, {});
30+
const testError = new Error('blah');
31+
// @ts-expect-error: private field
32+
connection.messageStream.emit('error', testError);
33+
const commandRes = connection.command(ns('a.b'), { ping: 1 }, {});
34+
35+
const error = await commandRes.catch(error => error);
36+
expect(error).to.equal(testError);
37+
});
38+
});
39+
840
describe('when destroyed after write', () => {
941
let client: MongoClient;
1042
let collection;

0 commit comments

Comments
 (0)