|
| 1 | + |
| 2 | +"use strict" |
| 3 | +var helper = require('./../test-helper') |
| 4 | +var assert = require('assert') |
| 5 | + |
| 6 | +const suite = new helper.Suite() |
| 7 | + |
| 8 | +// makes a backend server that responds with a non 'S' ssl response buffer |
| 9 | +let makeTerminatingBackend = (byte) => { |
| 10 | + const { createServer } = require('net') |
| 11 | + |
| 12 | + const server = createServer((socket) => { |
| 13 | + // attach a listener so the socket can drain |
| 14 | + // https://www.postgresql.org/docs/9.3/protocol-message-formats.html |
| 15 | + socket.on('data', (buff) => { |
| 16 | + const code = buff.readInt32BE(4) |
| 17 | + // I don't see anything in the docs about 80877104 |
| 18 | + // but libpq is sending it... |
| 19 | + if (code === 80877103 || code === 80877104) { |
| 20 | + const packet = Buffer.from(byte, 'utf-8') |
| 21 | + socket.write(packet) |
| 22 | + } |
| 23 | + }) |
| 24 | + socket.on('close', () => { |
| 25 | + server.close() |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + server.listen() |
| 30 | + const { port } = server.address() |
| 31 | + return port |
| 32 | +} |
| 33 | + |
| 34 | +suite.test('SSL connection error allows event loop to exit', (done) => { |
| 35 | + const port = makeTerminatingBackend('N') |
| 36 | + const client = new helper.pg.Client({ ssl: 'require', port }) |
| 37 | + // since there was a connection error the client's socket should be closed |
| 38 | + // and the event loop will have no refs and exit cleanly |
| 39 | + client.connect((err) => { |
| 40 | + assert(err instanceof Error) |
| 41 | + done() |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | + |
| 46 | +suite.test('Non "S" response code allows event loop to exit', (done) => { |
| 47 | + const port = makeTerminatingBackend('X') |
| 48 | + const client = new helper.pg.Client({ ssl: 'require', port }) |
| 49 | + // since there was a connection error the client's socket should be closed |
| 50 | + // and the event loop will have no refs and exit cleanly |
| 51 | + client.connect((err) => { |
| 52 | + assert(err instanceof Error) |
| 53 | + done() |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
0 commit comments