|
| 1 | +import { Duplex } from 'node:stream'; |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | + |
| 6 | +import { Connection, type MongoClient, MongoNetworkError, ns } from '../../mongodb'; |
| 7 | +import { clearFailPoint, configureFailPoint } from '../../tools/utils'; |
| 8 | + |
| 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 | + |
| 40 | + describe('when destroyed by failpoint', () => { |
| 41 | + let client: MongoClient; |
| 42 | + let collection; |
| 43 | + |
| 44 | + const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } }; |
| 45 | + |
| 46 | + beforeEach(async function () { |
| 47 | + if (!this.configuration.filters.NodeVersionFilter.filter({ metadata })) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + await configureFailPoint(this.configuration, { |
| 52 | + configureFailPoint: 'failCommand', |
| 53 | + mode: 'alwaysOn', |
| 54 | + data: { |
| 55 | + appName: 'failInserts2', |
| 56 | + failCommands: ['insert'], |
| 57 | + closeConnection: true |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + client = this.configuration.newClient({}, { appName: 'failInserts2' }); |
| 62 | + await client.connect(); |
| 63 | + const db = client.db('closeConn'); |
| 64 | + collection = db.collection('closeConn'); |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(async function () { |
| 68 | + sinon.restore(); |
| 69 | + await clearFailPoint(this.configuration); |
| 70 | + await client.close(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('throws a MongoNetworkError', metadata, async () => { |
| 74 | + const error = await collection.insertOne({ name: 'test' }).catch(error => error); |
| 75 | + expect(error, error.stack).to.be.instanceOf(MongoNetworkError); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments