Skip to content

Commit

Permalink
feat: retry sending events to inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Sep 23, 2022
1 parent c6f196e commit 584eb69
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

35 changes: 31 additions & 4 deletions src/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export const sendBatch = () => {
if (skipTracking || batch.length === 0) {
return;
}
const currentBatch = [...batch];
const resetBatch = () => {
batch = [...currentBatch, ...batch];
};
logger.debug('sending tracking...');
if (platform() === PLATFORMS.NODE && http) {
const req = originals.http.request(
Expand All @@ -69,20 +73,43 @@ export const sendBatch = () => {
method: 'POST',
headers: {'content-type': 'application/json'},
},
() => {},
(res) => {
if (res.statusCode !== 200) {
logger.debug('error tracking call to inspector: got status', res.statusCode);
resetBatch();
}
},
);
req.on('error', (error) => logger.debug('error tracking call to inspector:', error.message));
req.end(JSON.stringify(batch, getCircularReplacer()));
req.on('error', (error) => {
logger.debug('error tracking call to inspector:', error.message);
resetBatch();
});
req.end(JSON.stringify(currentBatch, getCircularReplacer()));
batch = [];
} else if (hasXMLHTTPRequest) {
const request = new originals.xmlhttprequest.XMLHttpRequest();
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status !== 200) {
logger.debug('error tracking call to inspector:', request.statusText);
resetBatch();
}
}
};
request.addEventListener('error', () => {
logger.debug('error tracking call to inspector');
resetBatch();
});
originals.xmlhttprequest.open.call(request, 'POST', `http://${host}:${port}/ingest`, true);
originals.xmlhttprequest.setRequestHeader.call(
request,
'content-type',
'application/json;charset=UTF-8',
);
originals.xmlhttprequest.send.call(request, JSON.stringify(batch, getCircularReplacer()));
originals.xmlhttprequest.send.call(
request,
JSON.stringify(currentBatch, getCircularReplacer()),
);
batch = [];
}
} catch (error) {
Expand Down
17 changes: 0 additions & 17 deletions tests/unit/track.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,4 @@ describe('track', () => {
setSkipTracking(false);
expect(spy).not.toBeCalled();
});

test('should track and send batch', () => {
track({});
sendBatch();
expect(spy).toBeCalledTimes(1);
});

test('should batch', async () => {
track({});
// Request should not be sent immediately
expect(spy).toBeCalledTimes(0);
// Batch should go out one second later
await new Promise((r) => {
setTimeout(r, 1200);
});
expect(spy).toBeCalledTimes(1);
});
});
31 changes: 31 additions & 0 deletions tests/unit/track.test.jsdom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// eslint-disable-next-line no-underscore-dangle
global.__non_webpack_require__ = require;

const spy = jest.fn();
window.XMLHttpRequest = jest.fn().mockImplementation(() => ({
addEventListener: jest.fn(),
}));
window.XMLHttpRequest.prototype.open = jest.fn();
window.XMLHttpRequest.prototype.send = spy;
window.XMLHttpRequest.prototype.setRequestHeader = jest.fn();

const {default: track, sendBatch} = require('../../src/track');

describe('track', () => {
test('should track and send batch', () => {
track({});
sendBatch();
expect(spy).toBeCalledTimes(1);
});

test('should batch', async () => {
track({});
// Request should not be sent immediately
expect(spy).toBeCalledTimes(0);
// Batch should go out one second later
await new Promise((r) => {
setTimeout(r, 1200);
});
expect(spy).toBeCalledTimes(1);
});
});
19 changes: 19 additions & 0 deletions tests/unit/track.test.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ global.__non_webpack_require__ = require;

const http = require('http');

jest.mock('http');
http.request.mockReturnValue({on: () => {}, end: () => {}});
const spy = jest.spyOn(http, 'request');

const {default: track, sendBatch, setTrackingServer} = require('../../src/track');
Expand All @@ -20,4 +22,21 @@ describe('track', () => {
expect.any(Function),
);
});

test('should track and send batch', () => {
track({});
sendBatch();
expect(spy).toBeCalledTimes(1);
});

test('should batch', async () => {
track({});
// Request should not be sent immediately
expect(spy).toBeCalledTimes(0);
// Batch should go out one second later
await new Promise((r) => {
setTimeout(r, 1200);
});
expect(spy).toBeCalledTimes(1);
});
});

0 comments on commit 584eb69

Please sign in to comment.