From d52a8e1cd2b27a2df3f389d5d31b8ffd09edf9a6 Mon Sep 17 00:00:00 2001 From: Natalie Wolfe Date: Fri, 23 Sep 2016 11:31:12 -0700 Subject: [PATCH] tests: added check for agent reset on restart --- lib/agent.js | 37 ++++++++----- lib/metrics/normalizer.js | 1 + lib/transaction/index.js | 1 - test/unit/collector/api.test.js | 97 +++++++++++++++++++++------------ 4 files changed, 86 insertions(+), 50 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index e4d6fad6c0..05d68e7161 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -64,6 +64,16 @@ function Agent(config) { // Reset the agent to add all the sub-objects it needs. These object are the // ones that get re-created if the agent is told to restart from the collector. + this.events = null + this.customEvents = null + this.errors = null + this.mapper = null + this.metricNameNormalizer = null + this.metrics = null + this.transactionNameNormalizer = null + this.urlNormalizer = null + this.txSegmentNormalizer = null + this.userNormalizer = null this.reset() // Transaction tracing. @@ -74,23 +84,23 @@ function Agent(config) { this.queries = new QueryTracer(this.config) // Set up all the configuration events the agent needs to listen for. + var self = this this.config.on('apdex_t', this._apdexTChange.bind(this)) this.config.on('data_report_period', this._harvesterIntervalChange.bind(this)) this.config.on('agent_enabled', this._enabledChange.bind(this)) this.config.on('change', this._configChange.bind(this)) - this.config.on( - 'metric_name_rules', - this.metricNameNormalizer.load.bind(this.metricNameNormalizer) - ) - this.config.on( - 'transaction_name_rules', - this.transactionNameNormalizer.load.bind(this.transactionNameNormalizer) - ) - this.config.on('url_rules', this.urlNormalizer.load.bind(this.urlNormalizer)) - this.config.on( - 'transaction_segment_terms', - this.txSegmentNormalizer.load.bind(this.txSegmentNormalizer) - ) + this.config.on('metric_name_rules', function updateMetricNameNormalizer() { + self.metricNameNormalizer.load.apply(self.metricNameNormalizer, arguments) + }) + this.config.on('transaction_name_rules', function updateTransactionNameNormalizer() { + self.transactionNameNormalizer.load.apply(self.transactionNameNormalizer, arguments) + }) + this.config.on('url_rules', function updateUrlNormalizer() { + self.urlNormalizer.load.apply(self.urlNormalizer, arguments) + }) + this.config.on('transaction_segment_terms', function updateSegmentNormalizer() { + self.txSegmentNormalizer.load.apply(self.txSegmentNormalizer, arguments) + }) // Entity tracking metrics. this.totalActiveSegments = 0 @@ -308,7 +318,6 @@ Agent.prototype.reconfigure = function reconfigure(configuration) { if (!configuration) throw new TypeError("must pass configuration") this.config.onConnect(configuration) - this.reset() } /** diff --git a/lib/metrics/normalizer.js b/lib/metrics/normalizer.js index db76a3d566..544b75cea2 100644 --- a/lib/metrics/normalizer.js +++ b/lib/metrics/normalizer.js @@ -77,6 +77,7 @@ util.inherits(MetricNormalizer, EventEmitter) */ MetricNormalizer.prototype.load = function load(json) { if (json) { + this.rules = [] logger.debug("Received %s %s normalization rule(s) from the server", json.length, this.type) diff --git a/lib/transaction/index.js b/lib/transaction/index.js index 38c81053d6..2ce59eea60 100644 --- a/lib/transaction/index.js +++ b/lib/transaction/index.js @@ -185,7 +185,6 @@ Transaction.prototype.setName = function setName(requestURL, statusCode) { transactionName: this.name}, 'Setting transaction name') this._partialName = this.nameState.getName() - this.url = urltils.scrub(requestURL) this.statusCode = statusCode diff --git a/test/unit/collector/api.test.js b/test/unit/collector/api.test.js index 402fb9b172..ebe5d4dacc 100644 --- a/test/unit/collector/api.test.js +++ b/test/unit/collector/api.test.js @@ -1378,49 +1378,76 @@ describe("CollectorAPI", function() { api._runLifecycle(method, null, tested) }) - it("should reconnect and resubmit on ForceRestartException", function(done) { - var exception = { - exception: { - message: "Yo, break off a piece of that Irish Sprang!", - error_type: 'NewRelic::Agent::ForceRestartException' + describe('on ForceRestartException', function() { + var restart = null + var shutdown = null + var redirect = null + var connect = null + var succeed = null + + beforeEach(function() { + var exception = { + exception: { + message: 'Yo, break off a piece of that Irish Sprang!', + error_type: 'NewRelic::Agent::ForceRestartException' + } } - } - - var restart = nock(URL) - .post(generate('metric_data', 31337)) - .reply(200, exception) - var shutdown = nock(URL) - .post(generate('shutdown', 31337)) - .reply(200, {return_value: null}) - var redirect = nock(URL) - .post(generate('get_redirect_host')) - .reply(200, {return_value: "collector.newrelic.com"}) - var connect = nock(URL) - .post(generate('connect')) - .reply(200, {return_value: {agent_run_id: 31338}}) - var succeed = nock(URL) - .post(generate('metric_data', 31338)) - .reply(200, {return_value: {}}) - function tested(error) { - if (error) { - console.error(error.stack) - } - should.not.exist(error) - expect(api._agent.config.run_id).equal(31338) // has new run ID + restart = nock(URL) + .post(generate('metric_data', 31337)) + .reply(200, exception) + shutdown = nock(URL) + .post(generate('shutdown', 31337)) + .reply(200, {return_value: null}) + redirect = nock(URL) + .post(generate('get_redirect_host')) + .reply(200, {return_value: 'collector.newrelic.com'}) + connect = nock(URL) + .post(generate('connect')) + .reply(200, {return_value: {agent_run_id: 31338}}) + succeed = nock(URL) + .post(generate('metric_data', 31338)) + .reply(200, {return_value: {}}) + }) + function nockDone() { restart.done() shutdown.done() redirect.done() connect.done() succeed.done() - done() } - api._runLifecycle(method, null, tested) + it('should reconnect and resubmit', function(done) { + api._runLifecycle(method, null, function(error) { + if (error) { + console.error(error.stack) + } + expect(error).to.not.exist + expect(api._agent.config.run_id).equal(31338) // has new run ID + nockDone() + done() + }) + }) + + it('should reconfigure the agent', function(done) { + var reconfigureCalled = false + var oldReconfigure = agent.reconfigure + agent.reconfigure = function() { + reconfigureCalled = true + return oldReconfigure.apply(this, arguments) + } + + api._runLifecycle(method, null, function(err) { + expect(err).to.not.exist + expect(reconfigureCalled).to.be.true + nockDone() + done() + }) + }) }) - it("should stop the agent on ForceDisconnectException", function (done) { + it("should stop the agent on ForceDisconnectException", function(done) { var exception = { exception: { message: "Wake up! Time to die!", @@ -1429,11 +1456,11 @@ describe("CollectorAPI", function() { } var restart = nock(URL) - .post(generate('metric_data', 31337)) - .reply(200, exception) + .post(generate('metric_data', 31337)) + .reply(200, exception) var shutdown = nock(URL) - .post(generate('shutdown', 31337)) - .reply(200, {return_value: null}) + .post(generate('shutdown', 31337)) + .reply(200, {return_value: null}) function tested(error) { expect(error.message).equal("Wake up! Time to die!")