Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the missing connect_timeout and keepalives_idle config paramete…
…rs (#1847) * Added the missing connect_timeout and keepalives_idle config parameters * Implementation and tests for keepAliveInitialDelayMillis and connectionTimeoutMillis [squashed 4]
- Loading branch information
Showing
7 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict' | ||
const net = require('net') | ||
const buffers = require('../../test-buffers') | ||
const helper = require('./test-helper') | ||
|
||
const suite = new helper.Suite() | ||
|
||
const options = { | ||
host: 'localhost', | ||
port: 54321, | ||
connectionTimeoutMillis: 2000, | ||
user: 'not', | ||
database: 'existing' | ||
} | ||
|
||
const serverWithConnectionTimeout = (timeout, callback) => { | ||
const sockets = new Set() | ||
|
||
const server = net.createServer(socket => { | ||
sockets.add(socket) | ||
socket.once('end', () => sockets.delete(socket)) | ||
|
||
socket.on('data', data => { | ||
// deny request for SSL | ||
if (data.length === 8) { | ||
socket.write(Buffer.from('N', 'utf8')) | ||
// consider all authentication requests as good | ||
} else if (!data[0]) { | ||
socket.write(buffers.authenticationOk()) | ||
// send ReadyForQuery `timeout` ms after authentication | ||
setTimeout(() => socket.write(buffers.readyForQuery()), timeout).unref() | ||
// respond with our canned response | ||
} else { | ||
socket.write(buffers.readyForQuery()) | ||
} | ||
}) | ||
}) | ||
|
||
let closing = false | ||
const closeServer = done => { | ||
if (closing) return | ||
closing = true | ||
|
||
server.close(done) | ||
for (const socket of sockets) { | ||
socket.destroy() | ||
} | ||
} | ||
|
||
server.listen(options.port, options.host, () => callback(closeServer)) | ||
} | ||
|
||
suite.test('successful connection', done => { | ||
serverWithConnectionTimeout(0, closeServer => { | ||
const timeoutId = setTimeout(() => { | ||
throw new Error('Client should have connected successfully but it did not.') | ||
}, 3000) | ||
|
||
const client = new helper.Client(options) | ||
client.connect() | ||
.then(() => client.end()) | ||
.then(() => closeServer(done)) | ||
.catch(err => closeServer(() => done(err))) | ||
.then(() => clearTimeout(timeoutId)) | ||
}) | ||
}) | ||
|
||
suite.test('expired connection timeout', done => { | ||
serverWithConnectionTimeout(options.connectionTimeoutMillis * 2, closeServer => { | ||
const timeoutId = setTimeout(() => { | ||
throw new Error('Client should have emitted an error but it did not.') | ||
}, 3000) | ||
|
||
const client = new helper.Client(options) | ||
client.connect() | ||
.then(() => client.end()) | ||
.then(() => closeServer(() => done(new Error('Connection timeout should have expired but it did not.')))) | ||
.catch(err => { | ||
assert(err instanceof Error) | ||
assert(/timeout expired\s*/.test(err.message)) | ||
closeServer(done) | ||
}) | ||
.then(() => clearTimeout(timeoutId)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
const net = require('net') | ||
const pg = require('../../../lib/index.js') | ||
const helper = require('./test-helper') | ||
|
||
const suite = new helper.Suite() | ||
|
||
suite.test('setting keep alive', done => { | ||
const server = net.createServer(c => { | ||
c.destroy() | ||
server.close() | ||
}) | ||
|
||
server.listen(7777, () => { | ||
const stream = new net.Socket() | ||
stream.setKeepAlive = (enable, initialDelay) => { | ||
assert(enable === true) | ||
assert(initialDelay === 10000) | ||
done() | ||
} | ||
|
||
const client = new pg.Client({ | ||
host: 'localhost', | ||
port: 7777, | ||
keepAlive: true, | ||
keepAliveInitialDelayMillis: 10000, | ||
stream | ||
}) | ||
|
||
client.connect().catch(() => {}) | ||
}) | ||
}) |