Skip to content

Commit

Permalink
Merge pull request #1308 from bugsnag/in-flight-across-deliveries
Browse files Browse the repository at this point in the history
in-flight: track requests when delivery is changed
  • Loading branch information
imjoehaines authored Feb 25, 2021
2 parents 1eca4ce + f16b116 commit daa4936
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/in-flight/src/in-flight.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,35 @@ module.exports = {
}
}

const delivery = client._delivery
const originalSendSession = delivery.sendSession
const patchDelivery = (delivery) => {
const originalSendSession = delivery.sendSession

delivery.sendSession = function (session, callback = noop) {
const id = cuid()
inFlightRequests.set(id, true)
delivery.sendSession = function (session, callback = noop) {
const id = cuid()
inFlightRequests.set(id, true)

const _callback = function () {
inFlightRequests.delete(id)
callback.apply(null, arguments)
}
const _callback = function () {
inFlightRequests.delete(id)
callback.apply(null, arguments)
}

originalSendSession.call(delivery, session, _callback)
originalSendSession.call(delivery, session, _callback)
}
}

let delivery = client._delivery
patchDelivery(delivery)

// ensure we also monkey-patch any new delivery that might be set
Object.defineProperty(client, '_delivery', {
get () {
return delivery
},
set (newDeliviery) {
patchDelivery(newDeliviery)
delivery = newDeliviery
}
})
},

flush (timeoutMs) {
Expand Down
75 changes: 75 additions & 0 deletions packages/in-flight/test/in-flight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,79 @@ describe('@bugsnag/in-flight', () => {
expect(eventPayloads.length).toBe(1)
expect(sessionPayloads.length).toBe(1)
})

it('can track requests when delivery is changed', async () => {
const client = new Client({ apiKey: 'AN_API_KEY' })
const originalEventPayloads: EventDeliveryPayload[] = []
const originalSessionPayloads: SessionDeliveryPayload[] = []

client._sessionDelegate = {
startSession (client, session) {
client._delivery.sendSession(session, () => {})
},
pauseSession: () => {},
resumeSession: () => {}
}

client._setDelivery(() => ({
sendEvent (payload, cb) {
setTimeout(function () {
originalEventPayloads.push(payload)
cb()
}, 100)
},
sendSession (payload, cb) {
setTimeout(function () {
originalSessionPayloads.push(payload)
cb()
}, 100)
}
}))

bugsnagInFlight.trackInFlight(client)

client.notify(new Error('xyz'))
client.startSession()

expect(originalEventPayloads.length).toBe(0)
expect(originalSessionPayloads.length).toBe(0)

await bugsnagInFlight.flush(1000)

expect(originalEventPayloads.length).toBe(1)
expect(originalSessionPayloads.length).toBe(1)

const newEventPayloads: EventDeliveryPayload[] = []
const newSessionPayloads: SessionDeliveryPayload[] = []

client._setDelivery(() => ({
sendEvent (payload, cb) {
setTimeout(function () {
newEventPayloads.push(payload)
cb()
}, 100)
},
sendSession (payload, cb) {
setTimeout(function () {
newSessionPayloads.push(payload)
cb()
}, 100)
}
}))

client.notify(new Error('xyz'))
client.startSession()

expect(originalEventPayloads.length).toBe(1)
expect(originalSessionPayloads.length).toBe(1)
expect(newEventPayloads.length).toBe(0)
expect(newSessionPayloads.length).toBe(0)

await bugsnagInFlight.flush(1000)

expect(originalEventPayloads.length).toBe(1)
expect(originalSessionPayloads.length).toBe(1)
expect(newEventPayloads.length).toBe(1)
expect(newSessionPayloads.length).toBe(1)
})
})

0 comments on commit daa4936

Please sign in to comment.