|
| 1 | +import * as chai from 'chai'; |
| 2 | +import sinonChai from 'sinon-chai'; |
| 3 | +import { |
| 4 | + fakeFetchGetKeepalive, |
| 5 | + fakeFetchInstall, |
| 6 | + fakeFetchRespondWith, |
| 7 | + fakeFetchRestore, |
| 8 | +} from '../../../tests/utils/index.ts'; |
| 9 | +import { _resetKeepaliveTracking, FetchTransport } from './FetchTransport.ts'; |
| 10 | + |
| 11 | +chai.use(sinonChai); |
| 12 | +const { expect } = chai; |
| 13 | + |
| 14 | +const makeTransport = ( |
| 15 | + overrides?: Partial<ConstructorParameters<typeof FetchTransport>[0]>, |
| 16 | +) => |
| 17 | + new FetchTransport({ |
| 18 | + url: 'http://example.com', |
| 19 | + headers: {}, |
| 20 | + compression: 'none', |
| 21 | + ...overrides, |
| 22 | + }); |
| 23 | + |
| 24 | +const smallPayload = new TextEncoder().encode('{"small": true}'); |
| 25 | +const largePayload = new Uint8Array(49153); // 1 byte over the 48KiB budget |
| 26 | + |
| 27 | +interface Deferred { |
| 28 | + promise: Promise<Response>; |
| 29 | + resolve: (value: Response) => void; |
| 30 | + reject: (reason: Error) => void; |
| 31 | +} |
| 32 | + |
| 33 | +function createDeferred(): Deferred { |
| 34 | + let resolve!: (value: Response) => void; |
| 35 | + let reject!: (reason: Error) => void; |
| 36 | + const promise = new Promise<Response>((res, rej) => { |
| 37 | + resolve = res; |
| 38 | + reject = rej; |
| 39 | + }); |
| 40 | + return { promise, resolve, reject }; |
| 41 | +} |
| 42 | + |
| 43 | +describe('FetchTransport', () => { |
| 44 | + beforeEach(() => { |
| 45 | + _resetKeepaliveTracking(); |
| 46 | + fakeFetchInstall(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + fakeFetchRestore(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('keepalive budget', () => { |
| 54 | + it('should set keepalive to true for payloads within the budget', async () => { |
| 55 | + fakeFetchRespondWith('ok', { status: 200 }); |
| 56 | + const transport = makeTransport(); |
| 57 | + |
| 58 | + await transport.send(smallPayload, 1000); |
| 59 | + |
| 60 | + expect(fakeFetchGetKeepalive()).to.equal(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should set keepalive to false for payloads exceeding the budget', async () => { |
| 64 | + fakeFetchRespondWith('ok', { status: 200 }); |
| 65 | + const transport = makeTransport(); |
| 66 | + |
| 67 | + await transport.send(largePayload, 1000); |
| 68 | + |
| 69 | + expect(fakeFetchGetKeepalive()).to.equal(false); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('keepalive cumulative tracking', () => { |
| 74 | + it('should disable keepalive when cumulative bytes exceed the budget', async () => { |
| 75 | + const deferred = createDeferred(); |
| 76 | + fakeFetchRestore(); |
| 77 | + const stub = fakeFetchInstall(); |
| 78 | + stub.onCall(0).returns(deferred.promise); |
| 79 | + stub.onCall(1).resolves(new Response('ok', { status: 200 })); |
| 80 | + |
| 81 | + const transport = makeTransport(); |
| 82 | + |
| 83 | + // 30KiB payload — fits individually but two exceed the 48KiB budget |
| 84 | + const payload30k = new Uint8Array(30 * 1024); |
| 85 | + |
| 86 | + // Start first request (stays inflight) |
| 87 | + const first = transport.send(payload30k, 5000); |
| 88 | + // Allow microtask to run so fetch is called |
| 89 | + await Promise.resolve(); |
| 90 | + |
| 91 | + expect(fakeFetchGetKeepalive(0)).to.equal(true); |
| 92 | + |
| 93 | + // Second request should exceed cumulative budget |
| 94 | + await transport.send(payload30k, 5000); |
| 95 | + |
| 96 | + expect(fakeFetchGetKeepalive(1)).to.equal(false); |
| 97 | + |
| 98 | + // Clean up |
| 99 | + deferred.resolve(new Response('ok', { status: 200 })); |
| 100 | + await first; |
| 101 | + }); |
| 102 | + |
| 103 | + it('should disable keepalive when concurrent count reaches 9', async () => { |
| 104 | + const deferreds: Deferred[] = []; |
| 105 | + fakeFetchRestore(); |
| 106 | + const stub = fakeFetchInstall(); |
| 107 | + |
| 108 | + for (let i = 0; i < 9; i++) { |
| 109 | + const d = createDeferred(); |
| 110 | + deferreds.push(d); |
| 111 | + stub.onCall(i).returns(d.promise); |
| 112 | + } |
| 113 | + stub.onCall(9).resolves(new Response('ok', { status: 200 })); |
| 114 | + |
| 115 | + const transport = makeTransport(); |
| 116 | + const tinyPayload = new Uint8Array(1); |
| 117 | + |
| 118 | + // Start 9 concurrent requests |
| 119 | + const pending = []; |
| 120 | + for (let i = 0; i < 9; i++) { |
| 121 | + pending.push(transport.send(tinyPayload, 5000)); |
| 122 | + await Promise.resolve(); |
| 123 | + } |
| 124 | + |
| 125 | + for (let i = 0; i < 9; i++) { |
| 126 | + expect(fakeFetchGetKeepalive(i)).to.equal(true); |
| 127 | + } |
| 128 | + |
| 129 | + // 10th request should exceed concurrent limit |
| 130 | + await transport.send(tinyPayload, 5000); |
| 131 | + |
| 132 | + expect(fakeFetchGetKeepalive(9)).to.equal(false); |
| 133 | + |
| 134 | + // Clean up |
| 135 | + for (const d of deferreds) { |
| 136 | + d.resolve(new Response('ok', { status: 200 })); |
| 137 | + } |
| 138 | + await Promise.all(pending); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should decrement counters after request completes', async () => { |
| 142 | + const deferred = createDeferred(); |
| 143 | + fakeFetchRestore(); |
| 144 | + const stub = fakeFetchInstall(); |
| 145 | + stub.onCall(0).returns(deferred.promise); |
| 146 | + stub.onCall(1).resolves(new Response('ok', { status: 200 })); |
| 147 | + stub.onCall(2).resolves(new Response('ok', { status: 200 })); |
| 148 | + |
| 149 | + const transport = makeTransport(); |
| 150 | + const payload30k = new Uint8Array(30 * 1024); |
| 151 | + |
| 152 | + // First request stays inflight |
| 153 | + const first = transport.send(payload30k, 5000); |
| 154 | + await Promise.resolve(); |
| 155 | + |
| 156 | + // Second request exceeds cumulative budget |
| 157 | + await transport.send(payload30k, 5000); |
| 158 | + |
| 159 | + expect(fakeFetchGetKeepalive(1)).to.equal(false); |
| 160 | + |
| 161 | + // Resolve first request to free up budget |
| 162 | + deferred.resolve(new Response('ok', { status: 200 })); |
| 163 | + await first; |
| 164 | + |
| 165 | + // Third request should fit now |
| 166 | + await transport.send(payload30k, 5000); |
| 167 | + |
| 168 | + expect(fakeFetchGetKeepalive(2)).to.equal(true); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should decrement counters after request fails', async () => { |
| 172 | + const deferred = createDeferred(); |
| 173 | + fakeFetchRestore(); |
| 174 | + const stub = fakeFetchInstall(); |
| 175 | + stub.onCall(0).returns(deferred.promise); |
| 176 | + stub.onCall(1).resolves(new Response('ok', { status: 200 })); |
| 177 | + stub.onCall(2).resolves(new Response('ok', { status: 200 })); |
| 178 | + |
| 179 | + const transport = makeTransport(); |
| 180 | + const payload30k = new Uint8Array(30 * 1024); |
| 181 | + |
| 182 | + // First request stays inflight |
| 183 | + const first = transport.send(payload30k, 5000); |
| 184 | + await Promise.resolve(); |
| 185 | + |
| 186 | + // Second request exceeds cumulative budget |
| 187 | + await transport.send(payload30k, 5000); |
| 188 | + |
| 189 | + expect(fakeFetchGetKeepalive(1)).to.equal(false); |
| 190 | + |
| 191 | + // Reject first request to free up budget |
| 192 | + deferred.reject(new TypeError('Failed to fetch')); |
| 193 | + await first; |
| 194 | + |
| 195 | + // Third request should fit now |
| 196 | + await transport.send(payload30k, 5000); |
| 197 | + |
| 198 | + expect(fakeFetchGetKeepalive(2)).to.equal(true); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('network errors', () => { |
| 203 | + it('should return failure when fetch throws', async () => { |
| 204 | + // Restore then re-install to reconfigure the stub to reject |
| 205 | + fakeFetchRestore(); |
| 206 | + fakeFetchInstall().rejects(new TypeError('Failed to fetch')); |
| 207 | + |
| 208 | + const transport = makeTransport(); |
| 209 | + const result = await transport.send(smallPayload, 1000); |
| 210 | + |
| 211 | + expect(result).to.deep.equal({ |
| 212 | + status: 'failure', |
| 213 | + error: new TypeError('Failed to fetch'), |
| 214 | + }); |
| 215 | + }); |
| 216 | + }); |
| 217 | +}); |
0 commit comments