Skip to content

Commit 6c4ba28

Browse files
author
William Armiros
committed
covered additional listener case
1 parent cfc4e8b commit 6c4ba28

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/core/lib/patchers/http_p.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,17 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
161161

162162
var cause = Utils.getCauseTypeFromHttpStatus(res.statusCode);
163163

164+
console.log('closing!!');
165+
164166
if (cause)
165167
subsegment[cause] = true;
166168

167169
subsegment.addRemoteRequestData(res.req, res, !!downstreamXRayEnabled);
168170
subsegment.close();
169171
});
170172

173+
// console.log(res.req.listenerCount('response'));
174+
171175
if (typeof callback === 'function') {
172176
if (contextUtils.isAutomaticMode()) {
173177
var session = contextUtils.getNamespace();
@@ -179,10 +183,11 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
179183
} else {
180184
callback(res);
181185
}
182-
// if no callback provided by user application, then we consume the response
183-
// so the 'end' event fires
186+
// if no callback provided by user application, AND no explicit response listener
187+
// added by user application, then we consume the response so the 'end' event fires
184188
// See: https://nodejs.org/api/http.html#http_class_http_clientrequest
185-
} else {
189+
} else if (res.req && res.req.listenerCount('response') === 0) {
190+
console.log('resuming!');
186191
res.resume();
187192
}
188193
});

packages/core/test/unit/patchers/http_p.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('HTTP/S', function() {
137137

138138
fakeRequest = buildFakeRequest();
139139
fakeResponse = buildFakeResponse();
140+
fakeResponse.req = fakeRequest;
140141

141142
httpClient = { request: function(...args) {
142143
const callback = args[typeof args[1] === 'object' ? 2 : 1];
@@ -171,6 +172,12 @@ describe('HTTP/S', function() {
171172
resumeSpy.should.not.have.been.called;
172173
});
173174

175+
it('should not consume the response if a response listener is provided by user', function() {
176+
fakeRequest.on('response', () => {});
177+
capturedHttp.request(httpOptions);
178+
resumeSpy.should.not.have.been.called;
179+
});
180+
174181
it('should create a new subsegment with name as hostname', function() {
175182
var options = {hostname: 'hostname', path: '/'};
176183
capturedHttp.request(options);

0 commit comments

Comments
 (0)