|
| 1 | +'use strict' |
| 2 | +const net = require('net') |
| 3 | +const buffers = require('../../test-buffers') |
| 4 | +const helper = require('./test-helper') |
| 5 | + |
| 6 | +const suite = new helper.Suite() |
| 7 | + |
| 8 | +const options = { |
| 9 | + host: 'localhost', |
| 10 | + port: 54321, |
| 11 | + connectionTimeoutMillis: 2000, |
| 12 | + user: 'not', |
| 13 | + database: 'existing' |
| 14 | +} |
| 15 | + |
| 16 | +const serverWithConnectionTimeout = (timeout, callback) => { |
| 17 | + const sockets = new Set() |
| 18 | + |
| 19 | + const server = net.createServer(socket => { |
| 20 | + sockets.add(socket) |
| 21 | + socket.once('end', () => sockets.delete(socket)) |
| 22 | + |
| 23 | + socket.on('data', data => { |
| 24 | + // deny request for SSL |
| 25 | + if (data.length === 8) { |
| 26 | + socket.write(Buffer.from('N', 'utf8')) |
| 27 | + // consider all authentication requests as good |
| 28 | + } else if (!data[0]) { |
| 29 | + socket.write(buffers.authenticationOk()) |
| 30 | + // send ReadyForQuery `timeout` ms after authentication |
| 31 | + setTimeout(() => socket.write(buffers.readyForQuery()), timeout).unref() |
| 32 | + // respond with our canned response |
| 33 | + } else { |
| 34 | + socket.write(buffers.readyForQuery()) |
| 35 | + } |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + let closing = false |
| 40 | + const closeServer = done => { |
| 41 | + if (closing) return |
| 42 | + closing = true |
| 43 | + |
| 44 | + server.close(done) |
| 45 | + for (const socket of sockets) { |
| 46 | + socket.destroy() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + server.listen(options.port, options.host, () => callback(closeServer)) |
| 51 | +} |
| 52 | + |
| 53 | +suite.test('successful connection', done => { |
| 54 | + serverWithConnectionTimeout(0, closeServer => { |
| 55 | + const timeoutId = setTimeout(() => { |
| 56 | + throw new Error('Client should have connected successfully but it did not.') |
| 57 | + }, 3000) |
| 58 | + |
| 59 | + const client = new helper.Client(options) |
| 60 | + client.connect() |
| 61 | + .then(() => client.end()) |
| 62 | + .then(() => closeServer(done)) |
| 63 | + .catch(err => closeServer(() => done(err))) |
| 64 | + .then(() => clearTimeout(timeoutId)) |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +suite.test('expired connection timeout', done => { |
| 69 | + serverWithConnectionTimeout(options.connectionTimeoutMillis * 2, closeServer => { |
| 70 | + const timeoutId = setTimeout(() => { |
| 71 | + throw new Error('Client should have emitted an error but it did not.') |
| 72 | + }, 3000) |
| 73 | + |
| 74 | + const client = new helper.Client(options) |
| 75 | + client.connect() |
| 76 | + .then(() => client.end()) |
| 77 | + .then(() => closeServer(() => done(new Error('Connection timeout should have expired but it did not.')))) |
| 78 | + .catch(err => { |
| 79 | + assert(err instanceof Error) |
| 80 | + assert(/timeout expired\s*/.test(err.message)) |
| 81 | + closeServer(done) |
| 82 | + }) |
| 83 | + .then(() => clearTimeout(timeoutId)) |
| 84 | + }) |
| 85 | +}) |
0 commit comments