Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make other connect timeout test more reliable too #2448

Merged
merged 1 commit into from Nov 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions tests/test-timeout.js
Expand Up @@ -150,15 +150,25 @@ var nonRoutable = [
'172.16.0.0',
'172.31.255.255'
]
var nrIndex = 0
function getNonRoutable() {
var ip = nonRoutable[nrIndex]
if (!ip) {
throw new Error('No more non-routable addresses')
}
++nrIndex
return ip
}
tape('connect timeout', function tryConnect(t) {
var tarpitHost = 'http://' + nonRoutable.shift()
var tarpitHost = 'http://' + getNonRoutable()
var shouldConnectTimeout = {
url: tarpitHost + '/timeout',
timeout: 100
}
var socket
request(shouldConnectTimeout, function(err) {
if (err.code === 'ENETUNREACH' && nonRoutable.length) {
t.notEqual(err, null)
if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) {
// With some network configurations, some addresses will be reported as
// unreachable immediately (before the timeout occurs). In those cases,
// try other non-routable addresses before giving up.
Expand All @@ -167,28 +177,33 @@ tape('connect timeout', function tryConnect(t) {
checkErrCode(t, err)
t.ok(err.connect === true, 'Connect Timeout Error should set \'connect\' property to true')
checkEventHandlers(t, socket)
nrIndex = 0
t.end()
}).on('socket', function(socket_) {
socket = socket_
})
})

tape('connect timeout with non-timeout error', function(t) {
// We need a destination that will not immediately return a TCP Reset
// packet. StackOverflow suggests this host:
// https://stackoverflow.com/a/904609/329700
var tarpitHost = 'http://10.255.255.1'
tape('connect timeout with non-timeout error', function tryConnect(t) {
var tarpitHost = 'http://' + getNonRoutable()
var shouldConnectTimeout = {
url: tarpitHost + '/timeout',
timeout: 1000
}
var socket
request(shouldConnectTimeout, function(err) {
t.notEqual(err, null)
if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) {
// With some network configurations, some addresses will be reported as
// unreachable immediately (before the timeout occurs). In those cases,
// try other non-routable addresses before giving up.
return tryConnect(t)
}
// Delay the check since the 'connect' handler is removed in a separate
// 'error' handler which gets triggered after this callback
setImmediate(function() {
checkEventHandlers(t, socket)
nrIndex = 0
t.end()
})
}).on('socket', function(socket_) {
Expand Down