Skip to content

Commit 69c6678

Browse files
committed
chore: fixes
1 parent e784d37 commit 69c6678

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

packages/collector/src/announceCycle/agentHostLookup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function handleResponse(host, res, cb) {
230230
);
231231
return;
232232
}
233+
233234
if (responseJson.version !== undefined) {
234235
cb(null);
235236
} else {

packages/collector/test/announceCycle/agentHostLookup_test.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const sinonChai = require('sinon-chai');
1111

1212
const { FakeRequestHandler, FakeRequest, FakeResponse } = require('../test_util/fake_http');
1313
const testUtils = require('@instana/core/test/test_util');
14+
const normalizeConfig = require('../../src/util/normalizeConfig');
1415

1516
const { expect } = chai;
1617
chai.use(sinonChai);
@@ -72,10 +73,13 @@ describe('agent host lookup state', () => {
7273
let defaultGatewayParserMock;
7374
let transitionTo;
7475
let ctxStub;
76+
let config;
7577

76-
before(() => {
77-
// set up stubs for HTTP communication and file system access (for reading from /proc/self/net/route).
78+
beforeEach(() => {
79+
config = normalizeConfig({ agentHost: host, agentPort: port });
80+
config.logger = testUtils.createFakeLogger();
7881

82+
// set up stubs for HTTP communication and file system access (for reading from /proc/self/net/route).
7983
requestStub = sinon.stub();
8084
httpStub = {
8185
request: requestStub,
@@ -99,19 +103,27 @@ describe('agent host lookup state', () => {
99103
'./defaultGatewayParser': defaultGatewayParserMock
100104
});
101105

102-
agentHostLookupState.init({ logger: testUtils.createFakeLogger() });
106+
agentHostLookupState.init(config);
103107
transitionTo = sinon.stub();
104108
ctxStub = { transitionTo };
105109
});
106110

107111
afterEach(() => {
108-
// reset stubs after each test
109-
sinon.restore();
110-
fakeRequest.reset();
112+
requestStub.reset();
113+
parseProcSelfNetRouteFileStub.reset();
114+
configuredHostLookupOk.reset();
115+
configuredHostLookupConnectionRefused.reset();
116+
configuredHostLookupNotAnAgent.reset();
117+
defaultGatewayLookupOk.reset();
118+
defaultGatewayLookupConnectionRefused.reset();
119+
120+
// the agentHostLookup overrides agentHost! We have to reset it.
121+
config.agentHost = host;
122+
config.agentPort = port;
111123
});
112124

113125
describe('via configured IP/configured port', () => {
114-
before(() => {
126+
beforeEach(() => {
115127
requestStub.callsFake((opt, cb) => {
116128
fakeRequest = new FakeRequest([configuredHostLookupOk], opt, cb);
117129
return fakeRequest;
@@ -130,7 +142,7 @@ describe('agent host lookup state', () => {
130142
});
131143

132144
describe('via default gateway IP', () => {
133-
before(() => {
145+
beforeEach(() => {
134146
parseProcSelfNetRouteFileStub.callsFake(async () => defaultGatewayIp);
135147
requestStub.callsFake((opt, cb) => {
136148
fakeRequest = new FakeRequest(
@@ -160,7 +172,7 @@ describe('agent host lookup state', () => {
160172
});
161173

162174
describe('via default gateway IP when something else listens on 127.0.0.1:42699', () => {
163-
before(() => {
175+
beforeEach(() => {
164176
parseProcSelfNetRouteFileStub.callsFake(async () => defaultGatewayIp);
165177
requestStub.callsFake((opt, cb) => {
166178
fakeRequest = new FakeRequest(
@@ -189,20 +201,19 @@ describe('agent host lookup state', () => {
189201
});
190202
});
191203

192-
describe('retry when default gateway IP cannot be determined', () => {
204+
describe('retry when default gateway IP cannot be determined', function () {
193205
let sinonFakeTimers;
206+
this.timeout(5000);
194207

195-
before(() => {
208+
beforeEach(() => {
196209
parseProcSelfNetRouteFileStub.callsFake(async () => {
197210
throw new Error('Failed to determine the default gateway: The file /proc/self/net/route does not exist');
198211
});
199212
requestStub.callsFake((opt, cb) => {
200213
fakeRequest = new FakeRequest([configuredHostLookupConnectionRefused, configuredHostLookupOk], opt, cb);
201214
return fakeRequest;
202215
});
203-
});
204216

205-
beforeEach(() => {
206217
sinonFakeTimers = sinon.useFakeTimers();
207218
});
208219

@@ -229,11 +240,12 @@ describe('agent host lookup state', () => {
229240
}));
230241
});
231242

232-
describe('retry when default gateway IP can be determined', () => {
243+
describe('retry when default gateway IP can be determined', function () {
233244
let sinonFakeTimers;
234-
245+
this.timeout(5000);
235246
const configuredHostLookupConnectionRefusedSecondAttempt = configuredHostLookupConnectionRefused.clone();
236-
before(() => {
247+
248+
beforeEach(() => {
237249
parseProcSelfNetRouteFileStub.callsFake(async () => defaultGatewayIp);
238250
requestStub.callsFake((opt, cb) => {
239251
fakeRequest = new FakeRequest(
@@ -249,10 +261,8 @@ describe('agent host lookup state', () => {
249261
);
250262
return fakeRequest;
251263
});
252-
});
253264

254-
beforeEach(() => {
255-
sinonFakeTimers = sinon.useFakeTimers();
265+
sinonFakeTimers = sinon.useFakeTimers({ shouldClearNativeTimers: true });
256266
});
257267

258268
afterEach(() => {

packages/core/test/test_util/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ module.exports = {
2020
getTestAppLogger: require('./log').getLogger,
2121
createFakeLogger: () => {
2222
return {
23-
debug: () => {},
23+
debug: msg => {
24+
console.log(msg);
25+
},
2426
info: () => {},
25-
warn: () => {},
27+
warn: msg => {
28+
console.log(msg);
29+
},
2630
error: () => {},
2731
trace: () => {}
2832
};

0 commit comments

Comments
 (0)