-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1304 from bugsnag/in-flight-package
Create @bugsnag/in-flight internal package
- Loading branch information
Showing
10 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) Bugsnag, https://www.bugsnag.com/ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the Software | ||
is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @bugsnag/in-flight | ||
|
||
Internal [@bugsnag/js](https://github.com/bugsnag/bugsnag-js) package to keep track of in-flight requests | ||
|
||
## License | ||
|
||
This package is free software released under the MIT License. See [LICENSE.txt](./LICENSE.txt) for details. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@bugsnag/in-flight", | ||
"version": "7.7.0", | ||
"main": "src/in-flight.js", | ||
"types": "types/bugsnag-in-flight.d.ts", | ||
"description": "Internal package to keep track of in-flight requests to Bugsnag", | ||
"homepage": "https://www.bugsnag.com/", | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:bugsnag/bugsnag-js.git" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"src", | ||
"types" | ||
], | ||
"author": "Bugsnag", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@bugsnag/cuid": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@bugsnag/core": "^7.7.0" | ||
}, | ||
"peerDependencies": { | ||
"@bugsnag/core": "^7.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const cuid = require('@bugsnag/cuid') | ||
|
||
const FLUSH_POLL_INTERVAL_MS = 50 | ||
const inFlightRequests = new Map() | ||
|
||
const noop = () => {} | ||
|
||
module.exports = { | ||
trackInFlight (client) { | ||
const originalNotify = client._notify | ||
|
||
client._notify = function (event, onError, callback = noop) { | ||
const id = cuid() | ||
inFlightRequests.set(id, true) | ||
|
||
const _callback = function () { | ||
inFlightRequests.delete(id) | ||
callback.apply(null, arguments) | ||
} | ||
|
||
client._depth += 1 | ||
|
||
try { | ||
originalNotify.call(client, event, onError, _callback) | ||
} finally { | ||
client._depth -= 1 | ||
} | ||
} | ||
|
||
const delivery = client._delivery | ||
const originalSendSession = delivery.sendSession | ||
|
||
delivery.sendSession = function (session, callback = noop) { | ||
const id = cuid() | ||
inFlightRequests.set(id, true) | ||
|
||
const _callback = function () { | ||
inFlightRequests.delete(id) | ||
callback.apply(null, arguments) | ||
} | ||
|
||
originalSendSession.call(delivery, session, _callback) | ||
} | ||
}, | ||
|
||
flush (timeoutMs) { | ||
return new Promise(function (resolve, reject) { | ||
let resolveTimeout | ||
const rejectTimeout = setTimeout( | ||
() => { | ||
if (resolveTimeout) clearTimeout(resolveTimeout) | ||
|
||
reject(new Error(`flush timed out after ${timeoutMs}ms`)) | ||
}, | ||
timeoutMs | ||
) | ||
|
||
const resolveIfNoRequests = function () { | ||
if (inFlightRequests.size === 0) { | ||
clearTimeout(rejectTimeout) | ||
resolve() | ||
|
||
return | ||
} | ||
|
||
resolveTimeout = setTimeout(resolveIfNoRequests, FLUSH_POLL_INTERVAL_MS) | ||
} | ||
|
||
resolveIfNoRequests() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
import Client, { EventDeliveryPayload, SessionDeliveryPayload } from '@bugsnag/core/client' | ||
|
||
// The in-flight package has module level state which can leak between tests | ||
// We can avoid this using jest's 'isolateModules' but need to type the | ||
// 'bugsnagInFlight' variable for this test to compile | ||
import BugsnagInFlightJustForTypescript from '../types/bugsnag-in-flight' | ||
|
||
let bugsnagInFlight: BugsnagInFlightJustForTypescript | ||
jest.isolateModules(() => { bugsnagInFlight = require('../src/in-flight') }) | ||
|
||
describe('@bugsnag/in-flight', () => { | ||
it('tracks in-flight events', () => { | ||
const client = new Client({ apiKey: 'AN_API_KEY' }) | ||
const payloads: EventDeliveryPayload[] = [] | ||
const sendSession = jest.fn() | ||
|
||
client._setDelivery(() => ({ | ||
sendEvent: (payload, cb) => { | ||
expect(client._depth).toBe(2) | ||
payloads.push(payload) | ||
cb() | ||
}, | ||
sendSession | ||
})) | ||
|
||
bugsnagInFlight.trackInFlight(client) | ||
|
||
expect(payloads.length).toBe(0) | ||
|
||
const onError = jest.fn() | ||
const callback = jest.fn() | ||
|
||
expect(client._depth).toBe(1) | ||
|
||
client.notify(new Error('xyz'), onError, callback) | ||
|
||
expect(client._depth).toBe(1) | ||
expect(onError).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(payloads.length).toBe(1) | ||
expect(sendSession).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('tracks in-flight sessions', () => { | ||
const client = new Client({ apiKey: 'AN_API_KEY' }) | ||
const payloads: SessionDeliveryPayload[] = [] | ||
const sendEvent = jest.fn() | ||
const callback = jest.fn() | ||
|
||
client._sessionDelegate = { | ||
startSession: jest.fn(function (client, session) { | ||
client._delivery.sendSession(session, callback) | ||
}), | ||
pauseSession: jest.fn(), | ||
resumeSession: jest.fn() | ||
} | ||
|
||
client._setDelivery(() => ({ | ||
sendEvent, | ||
sendSession: (payload, cb) => { | ||
payloads.push(payload) | ||
cb() | ||
} | ||
})) | ||
|
||
bugsnagInFlight.trackInFlight(client) | ||
|
||
expect(payloads.length).toBe(0) | ||
expect(callback).not.toHaveBeenCalled() | ||
expect(client._sessionDelegate.startSession).not.toHaveBeenCalled() | ||
expect(client._sessionDelegate.pauseSession).not.toHaveBeenCalled() | ||
expect(client._sessionDelegate.resumeSession).not.toHaveBeenCalled() | ||
|
||
client.startSession() | ||
|
||
expect(payloads.length).toBe(1) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(client._sessionDelegate.startSession).toHaveBeenCalledTimes(1) | ||
expect(client._sessionDelegate.pauseSession).not.toHaveBeenCalled() | ||
expect(client._sessionDelegate.resumeSession).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('tracks all in-flight requests', () => { | ||
const client = new Client({ apiKey: 'AN_API_KEY' }) | ||
const eventPayloads: EventDeliveryPayload[] = [] | ||
const sessionPayloads: SessionDeliveryPayload[] = [] | ||
const sessionCallback = jest.fn() | ||
|
||
client._sessionDelegate = { | ||
startSession: jest.fn(function (client, session) { | ||
client._delivery.sendSession(session, sessionCallback) | ||
}), | ||
pauseSession: jest.fn(), | ||
resumeSession: jest.fn() | ||
} | ||
|
||
client._setDelivery(() => ({ | ||
sendEvent: (payload, cb) => { | ||
expect(client._depth).toBe(2) | ||
eventPayloads.push(payload) | ||
cb() | ||
}, | ||
sendSession: (payload, cb) => { | ||
sessionPayloads.push(payload) | ||
cb() | ||
} | ||
})) | ||
|
||
bugsnagInFlight.trackInFlight(client) | ||
|
||
expect(eventPayloads.length).toBe(0) | ||
expect(sessionPayloads.length).toBe(0) | ||
|
||
const onError = jest.fn() | ||
const notifyCallback = jest.fn() | ||
|
||
expect(client._depth).toBe(1) | ||
|
||
client.notify(new Error('xyz'), onError, notifyCallback) | ||
client.startSession() | ||
|
||
expect(client._depth).toBe(1) | ||
expect(onError).toHaveBeenCalledTimes(1) | ||
expect(notifyCallback).toHaveBeenCalledTimes(1) | ||
expect(sessionCallback).toHaveBeenCalledTimes(1) | ||
expect(eventPayloads.length).toBe(1) | ||
expect(sessionPayloads.length).toBe(1) | ||
}) | ||
|
||
it('can flush successfully', async () => { | ||
const client = new Client({ apiKey: 'AN_API_KEY' }) | ||
const eventPayloads: EventDeliveryPayload[] = [] | ||
const sessionPayloads: SessionDeliveryPayload[] = [] | ||
|
||
client._sessionDelegate = { | ||
startSession (client, session) { | ||
client._delivery.sendSession(session, () => {}) | ||
}, | ||
pauseSession: () => {}, | ||
resumeSession: () => {} | ||
} | ||
|
||
client._setDelivery(() => ({ | ||
sendEvent (payload, cb) { | ||
setTimeout(function () { | ||
eventPayloads.push(payload) | ||
cb() | ||
}, 100) | ||
}, | ||
sendSession (payload, cb) { | ||
setTimeout(function () { | ||
sessionPayloads.push(payload) | ||
cb() | ||
}, 100) | ||
} | ||
})) | ||
|
||
bugsnagInFlight.trackInFlight(client) | ||
|
||
client.notify(new Error('xyz')) | ||
client.startSession() | ||
|
||
expect(eventPayloads.length).toBe(0) | ||
expect(sessionPayloads.length).toBe(0) | ||
|
||
await bugsnagInFlight.flush(1000) | ||
|
||
expect(eventPayloads.length).toBe(1) | ||
expect(sessionPayloads.length).toBe(1) | ||
}) | ||
|
||
it('will timeout if flush takes too long', async () => { | ||
const client = new Client({ apiKey: 'AN_API_KEY' }) | ||
const eventPayloads: EventDeliveryPayload[] = [] | ||
const sessionPayloads: SessionDeliveryPayload[] = [] | ||
|
||
client._sessionDelegate = { | ||
startSession: (client, session) => { | ||
client._delivery.sendSession(session, () => {}) | ||
}, | ||
pauseSession: () => {}, | ||
resumeSession: () => {} | ||
} | ||
|
||
client._setDelivery(() => ({ | ||
sendEvent (payload, cb) { | ||
setTimeout(() => { | ||
eventPayloads.push(payload) | ||
cb() | ||
}, 250) | ||
}, | ||
sendSession (payload, cb) { | ||
setTimeout(() => { | ||
sessionPayloads.push(payload) | ||
cb() | ||
}, 250) | ||
} | ||
})) | ||
|
||
bugsnagInFlight.trackInFlight(client) | ||
|
||
client.notify(new Error('xyz')) | ||
client.startSession() | ||
|
||
expect(eventPayloads.length).toBe(0) | ||
expect(sessionPayloads.length).toBe(0) | ||
|
||
const expected = new Error('flush timed out after 10ms') | ||
await expect(() => bugsnagInFlight.flush(10)).rejects.toThrow(expected) | ||
|
||
expect(eventPayloads.length).toBe(0) | ||
expect(sessionPayloads.length).toBe(0) | ||
|
||
await bugsnagInFlight.flush(1000) | ||
|
||
expect(eventPayloads.length).toBe(1) | ||
expect(sessionPayloads.length).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Client } from '@bugsnag/core' | ||
|
||
interface BugsnagInFlight { | ||
trackInFlight (client: Client): void | ||
flush (timeoutMs: number): Promise<void> | ||
} | ||
|
||
export default BugsnagInFlight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters