|
| 1 | +import { PriorityQueue } from '../../../lib/priority-queue' |
| 2 | +import { MiddlewareParams } from '../../../plugins/middleware' |
| 3 | +import { Context } from '../../context' |
| 4 | +import { Plugin } from '../../plugin' |
| 5 | +import { EventQueue } from '../../queue/event-queue' |
| 6 | +import { Analytics } from '../index' |
| 7 | +import { |
| 8 | + AfterPlugin, |
| 9 | + BeforePlugin, |
| 10 | + DestinationPlugin, |
| 11 | + EnrichmentPlugin, |
| 12 | +} from './test-plugins' |
| 13 | + |
| 14 | +describe('Analytics', () => { |
| 15 | + describe('plugin error behavior', () => { |
| 16 | + const retriesEnabledScenarios = [true, false] |
| 17 | + retriesEnabledScenarios.forEach((retriesEnabled) => { |
| 18 | + describe(`with retries ${ |
| 19 | + retriesEnabled ? 'enabled' : 'disabled' |
| 20 | + }`, () => { |
| 21 | + let analytics: Analytics |
| 22 | + const attemptCount = retriesEnabled ? 2 : 1 |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + const queue = new EventQueue(new PriorityQueue(attemptCount, [])) |
| 26 | + analytics = new Analytics({ writeKey: 'writeKey' }, {}, queue) |
| 27 | + }) |
| 28 | + |
| 29 | + // Indicates plugins should throw |
| 30 | + const shouldThrow = true |
| 31 | + |
| 32 | + it(`"before" plugin errors should not throw (single dispatched event)`, async () => { |
| 33 | + const plugin = new BeforePlugin({ shouldThrow }) |
| 34 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 35 | + |
| 36 | + await analytics.register(plugin) |
| 37 | + const context = await analytics.track('test') |
| 38 | + expect(trackSpy).toHaveBeenCalledTimes(attemptCount) |
| 39 | + expect(context).toBeInstanceOf(Context) |
| 40 | + expect(context.failedDelivery()).toBeTruthy() |
| 41 | + }) |
| 42 | + |
| 43 | + it(`"before" plugin errors should not throw (multiple dispatched events)`, async () => { |
| 44 | + const plugin = new BeforePlugin({ shouldThrow }) |
| 45 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 46 | + |
| 47 | + await analytics.register(plugin) |
| 48 | + const contexts = await Promise.all([ |
| 49 | + analytics.track('test1'), |
| 50 | + analytics.track('test2'), |
| 51 | + analytics.track('test3'), |
| 52 | + ]) |
| 53 | + expect(trackSpy).toHaveBeenCalledTimes(3 * attemptCount) |
| 54 | + |
| 55 | + for (const context of contexts) { |
| 56 | + expect(context).toBeInstanceOf(Context) |
| 57 | + expect(context.failedDelivery()).toBeTruthy() |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + it(`"before" plugin errors should not impact callbacks`, async () => { |
| 62 | + const plugin = new BeforePlugin({ shouldThrow }) |
| 63 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 64 | + |
| 65 | + await analytics.register(plugin) |
| 66 | + |
| 67 | + const context = await new Promise<Context>((resolve) => { |
| 68 | + void analytics.track('test', resolve) |
| 69 | + }) |
| 70 | + |
| 71 | + expect(trackSpy).toHaveBeenCalledTimes(attemptCount) |
| 72 | + expect(context).toBeInstanceOf(Context) |
| 73 | + expect(context.failedDelivery()).toBeTruthy() |
| 74 | + }) |
| 75 | + |
| 76 | + if (retriesEnabled) { |
| 77 | + it(`will not mark delivery failed if retry succeeds`, async () => { |
| 78 | + const plugin: Plugin = { |
| 79 | + name: 'Test Retries Plugin', |
| 80 | + type: 'before', |
| 81 | + version: '1.0.0', |
| 82 | + isLoaded: () => true, |
| 83 | + load: () => Promise.resolve(), |
| 84 | + track: jest.fn((ctx) => { |
| 85 | + if (ctx.attempts < attemptCount) { |
| 86 | + throw new Error(`Plugin failed!`) |
| 87 | + } |
| 88 | + return ctx |
| 89 | + }), |
| 90 | + } |
| 91 | + await analytics.register(plugin) |
| 92 | + |
| 93 | + const context = await analytics.track('test') |
| 94 | + expect(plugin.track).toHaveBeenCalledTimes(attemptCount) |
| 95 | + expect(context).toBeInstanceOf(Context) |
| 96 | + expect(context.failedDelivery()).toBeFalsy() |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + const testPlugins = [ |
| 101 | + new EnrichmentPlugin({ shouldThrow }), |
| 102 | + new DestinationPlugin({ shouldThrow }), |
| 103 | + new AfterPlugin({ shouldThrow }), |
| 104 | + ] |
| 105 | + testPlugins.forEach((plugin) => { |
| 106 | + it(`"${plugin.type}" plugin errors should not throw (single dispatched event)`, async () => { |
| 107 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 108 | + |
| 109 | + await analytics.register(plugin) |
| 110 | + |
| 111 | + const context = await analytics.track('test') |
| 112 | + expect(trackSpy).toHaveBeenCalledTimes(1) |
| 113 | + expect(context).toBeInstanceOf(Context) |
| 114 | + expect(context.failedDelivery()).toBeFalsy() |
| 115 | + }) |
| 116 | + |
| 117 | + it(`"${plugin.type}" plugin errors should not throw (multiple dispatched events)`, async () => { |
| 118 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 119 | + |
| 120 | + await analytics.register(plugin) |
| 121 | + |
| 122 | + const contexts = await Promise.all([ |
| 123 | + analytics.track('test1'), |
| 124 | + analytics.track('test2'), |
| 125 | + analytics.track('test3'), |
| 126 | + ]) |
| 127 | + |
| 128 | + expect(trackSpy).toHaveBeenCalledTimes(3) |
| 129 | + for (const context of contexts) { |
| 130 | + expect(context).toBeInstanceOf(Context) |
| 131 | + expect(context.failedDelivery()).toBeFalsy() |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + it(`"${plugin.type}" plugin errors should not impact callbacks`, async () => { |
| 136 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 137 | + |
| 138 | + await analytics.register(plugin) |
| 139 | + |
| 140 | + const context = await new Promise<Context>((resolve) => { |
| 141 | + void analytics.track('test', resolve) |
| 142 | + }) |
| 143 | + |
| 144 | + expect(trackSpy).toHaveBeenCalledTimes(1) |
| 145 | + expect(context).toBeInstanceOf(Context) |
| 146 | + expect(context.failedDelivery()).toBeFalsy() |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + it('"before" plugin supports cancelation (single dispatched event)', async () => { |
| 151 | + const plugin = new BeforePlugin({ shouldCancel: true }) |
| 152 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 153 | + |
| 154 | + await analytics.register(plugin) |
| 155 | + |
| 156 | + const context = await analytics.track('test') |
| 157 | + expect(trackSpy).toHaveBeenCalledTimes(1) |
| 158 | + expect(context).toBeInstanceOf(Context) |
| 159 | + expect(context.failedDelivery()).toBeTruthy() |
| 160 | + }) |
| 161 | + |
| 162 | + it('"before" plugin supports cancelation (multiple dispatched events)', async () => { |
| 163 | + const plugin = new BeforePlugin({ shouldCancel: true }) |
| 164 | + const trackSpy = jest.spyOn(plugin, 'track') |
| 165 | + |
| 166 | + await analytics.register(plugin) |
| 167 | + const contexts = await Promise.all([ |
| 168 | + analytics.track('test1'), |
| 169 | + analytics.track('test2'), |
| 170 | + analytics.track('test3'), |
| 171 | + ]) |
| 172 | + expect(trackSpy).toHaveBeenCalledTimes(3) |
| 173 | + |
| 174 | + for (const context of contexts) { |
| 175 | + expect(context).toBeInstanceOf(Context) |
| 176 | + expect(context.failedDelivery()).toBeTruthy() |
| 177 | + } |
| 178 | + }) |
| 179 | + |
| 180 | + it(`source middleware should not throw when "next" not called`, async () => { |
| 181 | + const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => {}) |
| 182 | + |
| 183 | + await analytics.addSourceMiddleware(sourceMiddleware) |
| 184 | + |
| 185 | + const context = await analytics.track('test') |
| 186 | + |
| 187 | + expect(sourceMiddleware).toHaveBeenCalledTimes(1) |
| 188 | + expect(context).toBeInstanceOf(Context) |
| 189 | + expect(context.failedDelivery()).toBeTruthy() |
| 190 | + }) |
| 191 | + |
| 192 | + it(`source middleware errors should not throw`, async () => { |
| 193 | + const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => { |
| 194 | + throw new Error('Source middleware error') |
| 195 | + }) |
| 196 | + |
| 197 | + await analytics.addSourceMiddleware(sourceMiddleware) |
| 198 | + |
| 199 | + const context = await analytics.track('test') |
| 200 | + |
| 201 | + expect(sourceMiddleware).toHaveBeenCalledTimes(1 * attemptCount) |
| 202 | + expect(context).toBeInstanceOf(Context) |
| 203 | + expect(context.failedDelivery()).toBeTruthy() |
| 204 | + }) |
| 205 | + |
| 206 | + it(`source middleware errors should not impact callbacks`, async () => { |
| 207 | + const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => { |
| 208 | + throw new Error('Source middleware error') |
| 209 | + }) |
| 210 | + |
| 211 | + await analytics.addSourceMiddleware(sourceMiddleware) |
| 212 | + |
| 213 | + const context = await new Promise<Context>((resolve) => { |
| 214 | + void analytics.track('test', resolve) |
| 215 | + }) |
| 216 | + |
| 217 | + expect(sourceMiddleware).toHaveBeenCalledTimes(1 * attemptCount) |
| 218 | + expect(context).toBeInstanceOf(Context) |
| 219 | + expect(context.failedDelivery()).toBeTruthy() |
| 220 | + }) |
| 221 | + }) |
| 222 | + }) |
| 223 | + }) |
| 224 | +}) |
0 commit comments