|
| 1 | +import { Destination, Event, LogLevel } from '@amplitude/analytics-core'; |
| 2 | +import * as http from 'http'; |
| 3 | +import { AddressInfo } from 'net'; |
| 4 | +import { NodeConfig } from '../src/config'; |
| 5 | + |
| 6 | +/** |
| 7 | + * SDK-188: an upload that never settles used to pin Destination.flushId, which |
| 8 | + * made every later flush a no-op while execute() kept appending to an uncapped |
| 9 | + * queue. These assert the queue drains no matter how the upload fails. |
| 10 | + */ |
| 11 | +describe('destination integration: upload timeout', () => { |
| 12 | + let server: http.Server; |
| 13 | + let serverUrl: string; |
| 14 | + let requestCount: number; |
| 15 | + |
| 16 | + let uploadsPerEvent: Map<string, number>; |
| 17 | + |
| 18 | + const listen = async (handler: http.RequestListener) => { |
| 19 | + requestCount = 0; |
| 20 | + uploadsPerEvent = new Map(); |
| 21 | + server = http.createServer((req, res) => { |
| 22 | + const chunks: Buffer[] = []; |
| 23 | + req.on('data', (chunk: Buffer) => chunks.push(chunk)); |
| 24 | + req.on('end', () => { |
| 25 | + requestCount += 1; |
| 26 | + const body = JSON.parse(Buffer.concat(chunks).toString()) as { events: { insert_id: string }[] }; |
| 27 | + body.events.forEach(({ insert_id }) => |
| 28 | + uploadsPerEvent.set(insert_id, (uploadsPerEvent.get(insert_id) ?? 0) + 1), |
| 29 | + ); |
| 30 | + handler(req, res); |
| 31 | + }); |
| 32 | + }); |
| 33 | + await new Promise<void>((resolve) => server.listen(0, resolve)); |
| 34 | + serverUrl = `http://localhost:${(server.address() as AddressInfo).port}/2/httpapi`; |
| 35 | + }; |
| 36 | + |
| 37 | + const setupDestination = async (overrides = {}) => { |
| 38 | + const config = new NodeConfig('a'.repeat(32), { |
| 39 | + serverUrl, |
| 40 | + requestTimeoutMillis: 50, |
| 41 | + flushIntervalMillis: 10, |
| 42 | + flushQueueSize: 5, |
| 43 | + logLevel: LogLevel.None, |
| 44 | + ...overrides, |
| 45 | + }); |
| 46 | + const destination = new Destination(); |
| 47 | + destination.retryTimeout = 10; |
| 48 | + await destination.setup(config); |
| 49 | + return destination; |
| 50 | + }; |
| 51 | + |
| 52 | + const events = (count: number): Event[] => |
| 53 | + Array.from({ length: count }, (_, i) => ({ event_type: 'exposure', insert_id: `id-${i}` })); |
| 54 | + |
| 55 | + // Callbacks fire inside send(), one tick before flush() clears flushId. Poll |
| 56 | + // instead of asserting immediately; a flushId that never clears is the bug. |
| 57 | + const waitForFlushToRelease = async (destination: Destination) => { |
| 58 | + for (let i = 0; i < 200 && destination.flushId !== null; i++) { |
| 59 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + afterEach(async () => { |
| 64 | + server.closeAllConnections(); |
| 65 | + await new Promise((resolve) => server.close(resolve)); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should recover once a stalled upload times out', async () => { |
| 69 | + await listen((_, res) => { |
| 70 | + // Hold the first upload open forever, then behave normally. |
| 71 | + if (requestCount === 1) { |
| 72 | + return; |
| 73 | + } |
| 74 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 75 | + res.end(JSON.stringify({ code: 200 })); |
| 76 | + }); |
| 77 | + const destination = await setupDestination(); |
| 78 | + |
| 79 | + const results = await Promise.all(events(20).map((event) => destination.execute(event))); |
| 80 | + |
| 81 | + expect(requestCount).toBeGreaterThan(1); |
| 82 | + expect(results.every((result) => result.code === 200)).toBe(true); |
| 83 | + await waitForFlushToRelease(destination); |
| 84 | + |
| 85 | + expect(destination.queue).toHaveLength(0); |
| 86 | + expect(destination.flushId).toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should drain the queue when every upload stalls', async () => { |
| 90 | + await listen(() => { |
| 91 | + // Never respond to anything. |
| 92 | + }); |
| 93 | + const destination = await setupDestination({ flushMaxRetries: 2 }); |
| 94 | + |
| 95 | + const results = await Promise.all(events(20).map((event) => destination.execute(event))); |
| 96 | + |
| 97 | + expect(results.every((result) => result.code === 500)).toBe(true); |
| 98 | + await waitForFlushToRelease(destination); |
| 99 | + |
| 100 | + expect(destination.queue).toHaveLength(0); |
| 101 | + expect(destination.flushId).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should retry rather than drop when uploads time out', async () => { |
| 105 | + await listen(() => { |
| 106 | + // Never respond to anything. |
| 107 | + }); |
| 108 | + const destination = await setupDestination({ flushMaxRetries: 4 }); |
| 109 | + |
| 110 | + const results = await Promise.all(events(10).map((event) => destination.execute(event))); |
| 111 | + |
| 112 | + // Held in memory and re-uploaded until flushMaxRetries is exhausted, rather |
| 113 | + // than discarded after the first failed attempt. |
| 114 | + expect([...uploadsPerEvent.values()]).toEqual(Array(10).fill(4)); |
| 115 | + expect(results.every((result) => result.message === 'Event rejected due to exceeded retry count')).toBe(true); |
| 116 | + }); |
| 117 | + |
| 118 | + test('should retry rather than drop when the response is truncated', async () => { |
| 119 | + await listen((_, res) => { |
| 120 | + res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': '200' }); |
| 121 | + res.write('{"code":200,"events_ing'); |
| 122 | + setTimeout(() => res.socket?.destroy(), 5); |
| 123 | + }); |
| 124 | + const destination = await setupDestination({ flushMaxRetries: 3 }); |
| 125 | + |
| 126 | + const results = await Promise.all(events(10).map((event) => destination.execute(event))); |
| 127 | + |
| 128 | + expect([...uploadsPerEvent.values()]).toEqual(Array(10).fill(3)); |
| 129 | + expect(results.every((result) => result.message === 'Event rejected due to exceeded retry count')).toBe(true); |
| 130 | + }); |
| 131 | + |
| 132 | + test('should drain the queue when uploads return an empty body', async () => { |
| 133 | + await listen((_, res) => { |
| 134 | + res.writeHead(200, { 'Content-Length': '0' }); |
| 135 | + res.end(); |
| 136 | + }); |
| 137 | + const destination = await setupDestination(); |
| 138 | + |
| 139 | + const results = await Promise.all(events(20).map((event) => destination.execute(event))); |
| 140 | + |
| 141 | + expect(results.every((result) => result.code === 200)).toBe(true); |
| 142 | + await waitForFlushToRelease(destination); |
| 143 | + |
| 144 | + expect(destination.queue).toHaveLength(0); |
| 145 | + expect(destination.flushId).toBeNull(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments