|
| 1 | +import { Duplex } from 'node:stream'; |
| 2 | + |
1 | 3 | import { expect } from 'chai';
|
2 | 4 | import * as sinon from 'sinon';
|
3 | 5 |
|
4 |
| -import { ConnectionPool, type MongoClient, MongoNetworkError } from '../../mongodb'; |
| 6 | +import { Connection, ConnectionPool, type MongoClient, MongoNetworkError, ns } from '../../mongodb'; |
5 | 7 | import { clearFailPoint, configureFailPoint } from '../../tools/utils';
|
6 | 8 |
|
7 | 9 | 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 | + |
8 | 40 | describe('when destroyed after write', () => {
|
9 | 41 | let client: MongoClient;
|
10 | 42 | let collection;
|
|
0 commit comments