Skip to content

Commit

Permalink
tests: abort AWS query when agent shuts down
Browse files Browse the repository at this point in the history
  • Loading branch information
NatalieWolfe authored and Martin Kuba committed Jun 14, 2016
1 parent d8970aa commit 9ebe86e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
18 changes: 15 additions & 3 deletions lib/aws-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,30 @@ module.exports._awsQuery = function awsQuery(key, agent, callback) {
} else {
data = data.toString('utf8')
}

agent.removeListener('errored', abortRequest)
agent.removeListener('stopped', abortRequest)
callback(data)
}
})
req.setTimeout(1000, function awsTimeout() {
logger.debug('Request for attribute ' + key + ' timed out')
logger.debug('Request for attribute %s timed out', key)
callback(null)
})
req.on('error', function awsError(err) {
logger.debug('Message for attribute ' + key + ': %s'
, err.message)
logger.debug('Message for attribute %s: %s', key, err.message)
callback(null)
})

agent.once('errored', abortRequest)
agent.once('stopped', abortRequest)

function abortRequest() {
logger.debug('Abborting request for attribute %s', key)
req.abort()
agent.removeListener('errored', abortRequest)
agent.removeListener('stopped', abortRequest)
}
}

function checkResponseString(str) {
Expand Down
1 change: 0 additions & 1 deletion lib/util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,3 @@ function getPropertiesToLog(extra) {
}
return obj
}

13 changes: 7 additions & 6 deletions test/integration/pricing/system-info.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var nock = require('nock')
var assert = require('chai').assert
var fetchSystemInfo = require('../../../lib/system-info')
var EventEmitter = require('events').EventEmitter

var awsHost = "http://169.254.169.254"

Expand All @@ -17,12 +18,12 @@ for (var awsPath in awsResponses) {
awsRedirect.get('/2008-02-01/meta-data/' + awsPath).reply(200, awsResponses[awsPath])
}

var fakeAgent = {
config: {
utilization: {
detect_aws: true,
detect_docker: false
}

var fakeAgent = new EventEmitter()
fakeAgent.config = {
utilization: {
detect_aws: true,
detect_docker: false
}
}

Expand Down

0 comments on commit 9ebe86e

Please sign in to comment.