-
Notifications
You must be signed in to change notification settings - Fork 2
feat(graphql): refactor service #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9feec43
feat(graphql): refactor service
2f04e54
clean up
4e3714d
format
c8c6a1d
polish: remove useRealTimers and restoreAllMocks
f8dca7e
polish: test names
3f686a5
polish: dispatchEvent
2e8fe7e
polish: setTimeoutSpy
6592115
polish: toHaveBeenCalledTimes
699cd9e
polish: handleBrowserOfflineWithThisBound
9168f4e
use warn
2d3706e
rename
96cc749
add guard
8c5d8ca
refactor tests
b8b31d5
roll back close
026bdaf
update tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
...es/javascript-api/src/lib/services/graphql/__tests__/graphql-reconnect-resilience.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { WebSocket } from 'mock-socket'; | ||
|
|
||
| import { GraphQLSubscriptionsFixture } from '../__fixtures__/graphql-subscriptions-fixture'; | ||
| import * as backoff from '../../../util/randomized-exponential-backoff/randomized-exponential-backoff'; | ||
|
|
||
| jest.mock('isomorphic-ws', () => WebSocket); | ||
| jest.mock('../../../util/sleep-ms/sleep-ms', () => ({ | ||
| sleepMs: () => new Promise((resolve) => setTimeout(resolve, 4)), | ||
| })); | ||
|
|
||
| describe('GraphQL reconnect resilience', () => { | ||
| let fixture: GraphQLSubscriptionsFixture; | ||
| let backoffSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| backoffSpy = jest.spyOn( | ||
| backoff, | ||
| 'calculateRandomizedExponentialBackoffTime', | ||
| ); | ||
| fixture = new GraphQLSubscriptionsFixture(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fixture.cleanup(); | ||
| }); | ||
|
|
||
| it('should escalate reconnect backoff across repeated unstable connections', async () => { | ||
| const sub = fixture.triggerSubscription(); | ||
|
|
||
| for (let cycle = 0; cycle < 3; cycle++) { | ||
| await fixture.handleConnectionInit(); | ||
| await fixture.consumeSubscribeMessage(); | ||
| await fixture.closeWithCode(1001); | ||
| fixture.openServer(); | ||
| } | ||
|
|
||
| const counters = backoffSpy.mock.calls.map((call) => call[0]); | ||
| expect(counters).toEqual([0, 1, 2]); | ||
|
|
||
| await fixture.handleConnectionInit(); | ||
| sub.unsubscribe(); | ||
| }); | ||
|
|
||
| it('should not leak ping intervals when the connection monitor restarts', async () => { | ||
| const setIntervalSpy = jest.spyOn(global, 'setInterval'); | ||
| const clearIntervalSpy = jest.spyOn(global, 'clearInterval'); | ||
|
|
||
| const sub = fixture.triggerSubscription(); | ||
| await fixture.handleConnectionInit(); | ||
| await fixture.consumeSubscribeMessage(); | ||
|
|
||
| fixture.sendMessageToClient({ type: 'connection_ack' }); | ||
| await fixture.consumeSubscribeMessage(); | ||
|
|
||
| const armedIds = setIntervalSpy.mock.results.map((result) => result.value); | ||
| const clearedIds = clearIntervalSpy.mock.calls.map((call) => call[0]); | ||
| const liveIntervals = armedIds.filter((id) => !clearedIds.includes(id)); | ||
| expect(liveIntervals).toHaveLength(1); | ||
|
|
||
| sub.unsubscribe(); | ||
| }); | ||
|
|
||
| it('should reconnect through backoff after a pong timeout, not immediately', async () => { | ||
| jest.useFakeTimers(); | ||
| const sub = fixture.triggerSubscription(); | ||
| await jest.runAllTimersAsync(); | ||
|
|
||
| await fixture.handleConnectionInit(); | ||
| await jest.runOnlyPendingTimersAsync(); | ||
| await fixture.consumeSubscribeMessage(); | ||
|
|
||
| await jest.advanceTimersToNextTimerAsync(); | ||
| await fixture.consumePingMessage(); | ||
|
|
||
| await jest.runOnlyPendingTimersAsync(); | ||
|
|
||
| expect(backoffSpy).toHaveBeenCalled(); | ||
|
|
||
| jest.useRealTimers(); | ||
| sub.unsubscribe(); | ||
| }); | ||
|
|
||
| it('should log how long the browser was offline when it comes back online', () => { | ||
| const service = fixture.graphqlService as any; | ||
| const warnSpy = jest.spyOn(service.logger, 'warn'); | ||
| let now = 1000; | ||
| const nowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now); | ||
|
|
||
| service.handleBrowserOffline(); | ||
| now = 6000; | ||
| service.handleBrowserOnline(); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith('Browser came back online', { | ||
| offlineDurationMs: 5000, | ||
| }); | ||
|
|
||
| nowSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('should clear the previous pong timeout before arming a new one', () => { | ||
| const service = fixture.graphqlService as any; | ||
| service.socket = { send: jest.fn() }; | ||
|
|
||
| const setTimeoutSpy = jest.spyOn(global, 'setTimeout'); | ||
| service.sendPing(); | ||
|
|
||
| const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout'); | ||
| service.sendPing(); | ||
|
|
||
| expect(clearTimeoutSpy).toHaveBeenCalledWith( | ||
| setTimeoutSpy.mock.results[0].value, | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion(non-blocking): maybe
toHaveBeenCalledTimes(1)?