|
| 1 | +import { WebSocket } from 'mock-socket'; |
| 2 | + |
| 3 | +import { GraphQLSubscriptionsFixture } from '../__fixtures__/graphql-subscriptions-fixture'; |
| 4 | +import * as backoff from '../../../util/randomized-exponential-backoff/randomized-exponential-backoff'; |
| 5 | + |
| 6 | +jest.mock('isomorphic-ws', () => WebSocket); |
| 7 | +jest.mock('../../../util/sleep-ms/sleep-ms', () => ({ |
| 8 | + sleepMs: () => new Promise((resolve) => setTimeout(resolve, 4)), |
| 9 | +})); |
| 10 | + |
| 11 | +// Mirrors the constants in graphql.service.ts (not exported). |
| 12 | +const MAX_FAILED_HANDSHAKES = 5; |
| 13 | +const BLOCKED_RETRY_INTERVAL_MS = 5 * 60 * 1000; |
| 14 | + |
| 15 | +describe('GraphQL blocked-WebSocket back-off', () => { |
| 16 | + let fixture: GraphQLSubscriptionsFixture; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + fixture = new GraphQLSubscriptionsFixture(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + await fixture.cleanup(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should back off to a slow interval after repeated handshakes fail before connecting', async () => { |
| 27 | + const service = fixture.graphqlService as any; |
| 28 | + const backoffSpy = jest.spyOn( |
| 29 | + backoff, |
| 30 | + 'calculateRandomizedExponentialBackoffTime', |
| 31 | + ); |
| 32 | + const infoSpy = jest.spyOn(service.logger, 'info'); |
| 33 | + |
| 34 | + const sub = fixture.triggerSubscription(); |
| 35 | + |
| 36 | + // Each cycle: connect, never ACK, close before the connection is established. |
| 37 | + for (let i = 0; i < MAX_FAILED_HANDSHAKES; i++) { |
| 38 | + await fixture.waitForConnection(); |
| 39 | + await fixture.getNextMessage(); // consume connection_init |
| 40 | + await fixture.closeWithCode(1001); |
| 41 | + fixture.openServer(); |
| 42 | + } |
| 43 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 44 | + |
| 45 | + expect(service.consecutiveFailedHandshakes).toBeGreaterThanOrEqual( |
| 46 | + MAX_FAILED_HANDSHAKES, |
| 47 | + ); |
| 48 | + // Fast exponential backoff is used only for the first (MAX - 1) reconnects; |
| 49 | + // once the cap is hit, the flat slow interval is used instead. |
| 50 | + expect(backoffSpy).toHaveBeenCalledTimes(MAX_FAILED_HANDSHAKES - 1); |
| 51 | + expect(infoSpy).toHaveBeenCalledWith( |
| 52 | + `Reconnect socket in ${BLOCKED_RETRY_INTERVAL_MS}ms`, |
| 53 | + ); |
| 54 | + |
| 55 | + sub.unsubscribe(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not count a close that happens after a successful connection', async () => { |
| 59 | + const service = fixture.graphqlService as any; |
| 60 | + |
| 61 | + const sub = fixture.triggerSubscription(); |
| 62 | + await fixture.handleConnectionInit(); // connection_ack -> CONNECTED |
| 63 | + await fixture.consumeSubscribeMessage(); |
| 64 | + |
| 65 | + expect(service.consecutiveFailedHandshakes).toBe(0); |
| 66 | + |
| 67 | + // A drop after the connection was established is a normal reconnect. |
| 68 | + await fixture.closeWithCode(1001); |
| 69 | + expect(service.consecutiveFailedHandshakes).toBe(0); |
| 70 | + |
| 71 | + fixture.openServer(); |
| 72 | + await fixture.handleConnectionInit(); |
| 73 | + sub.unsubscribe(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should reset the counter when the connection is established', async () => { |
| 77 | + const service = fixture.graphqlService as any; |
| 78 | + |
| 79 | + const sub = fixture.triggerSubscription(); |
| 80 | + |
| 81 | + for (let i = 0; i < 2; i++) { |
| 82 | + await fixture.waitForConnection(); |
| 83 | + await fixture.getNextMessage(); |
| 84 | + await fixture.closeWithCode(1001); |
| 85 | + fixture.openServer(); |
| 86 | + } |
| 87 | + expect(service.consecutiveFailedHandshakes).toBe(2); |
| 88 | + |
| 89 | + // A successful connection_ack resets the counter back to fast reconnects. |
| 90 | + await fixture.handleConnectionInit(); |
| 91 | + await fixture.consumeSubscribeMessage(); |
| 92 | + expect(service.consecutiveFailedHandshakes).toBe(0); |
| 93 | + |
| 94 | + sub.unsubscribe(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments