-
-
Notifications
You must be signed in to change notification settings - Fork 676
Time out initial fetch, and go to account-picker screen #4166
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ac6d0c
fetchActions: Stop using `tryUntilSuccessful` in `fetchPrivateMessages`.
chrisbobbe 1f0eacb
lolex: Expose some async methods from Lolex.
chrisbobbe 1eb1a5d
async [nfc]: Move `tryUntilSuccessful` to `fetchActions` and rename.
chrisbobbe ba18710
redux: Add `INITIAL_FETCH_ABORT` action.
chrisbobbe d50533c
async: Add `promiseTimeout`.
chrisbobbe 4d1d235
initial fetch: Add a timeout to abort.
chrisbobbe 3939420
fetchActions: Pull `await backoffMachine.wait()` out of `catch` block.
chrisbobbe 785232e
navReducer: Better decide if we have server data in `rehydrate`.
chrisbobbe 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
There are no files selected for viewing
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
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
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
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 |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| import mockStore from 'redux-mock-store'; // eslint-disable-line | ||
|
|
||
| import { fetchMessages, fetchOlder, fetchNewer } from '../fetchActions'; | ||
| import { Lolex } from '../../__tests__/lib/lolex'; | ||
| import { fetchMessages, fetchOlder, fetchNewer, tryFetch } from '../fetchActions'; | ||
| import { streamNarrow, HOME_NARROW, HOME_NARROW_STR } from '../../utils/narrow'; | ||
| import { navStateWithNarrow } from '../../utils/testHelpers'; | ||
| import { ApiError } from '../../api/apiErrors'; | ||
| import { TimeoutError } from '../../utils/async'; | ||
|
|
||
| const narrow = streamNarrow('some stream'); | ||
| const streamNarrowStr = JSON.stringify(narrow); | ||
|
|
@@ -14,6 +17,79 @@ describe('fetchActions', () => { | |
| fetch.reset(); | ||
| }); | ||
|
|
||
| describe('tryFetch', () => { | ||
| const lolex = new Lolex(); | ||
| afterAll(() => { | ||
| lolex.dispose(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // clear any unset timers | ||
| lolex.clearAllTimers(); | ||
| }); | ||
|
|
||
| test('retries a call, if there is an exception', async () => { | ||
| // fail on first call, succeed second time | ||
| let callCount = 0; | ||
| const thrower = () => { | ||
| callCount++; | ||
| if (callCount === 1) { | ||
| throw new Error('First run exception'); | ||
| } | ||
| return 'hello'; | ||
| }; | ||
|
|
||
| const tryFetchPromise = tryFetch(async () => { | ||
| await new Promise(r => setTimeout(r, 10)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearer as (Ditto below.) |
||
| return thrower(); | ||
| }); | ||
| await lolex.runAllTimersAsync(); | ||
| const result = await tryFetchPromise; | ||
|
|
||
| expect(result).toEqual('hello'); | ||
| }); | ||
|
|
||
| test('Rethrows a 4xx error without retrying', async () => { | ||
| const apiError = new ApiError(400, { | ||
| code: 'BAD_REQUEST', | ||
| msg: 'Bad Request', | ||
| result: 'error', | ||
| }); | ||
|
|
||
| const func = jest.fn().mockImplementation(async () => { | ||
| throw apiError; | ||
| }); | ||
| const tryFetchPromise = tryFetch(func); | ||
|
|
||
| await lolex.runAllTimersAsync(); | ||
| expect(func).toHaveBeenCalledTimes(1); | ||
| return expect(tryFetchPromise).rejects.toThrow(apiError); | ||
| }); | ||
|
|
||
| test('times out after many short-duration 5xx errors', async () => { | ||
| const tryFetchPromise = tryFetch(async () => { | ||
| await new Promise(r => setTimeout(r, 50)); | ||
| throw new ApiError(500, { | ||
| code: 'SOME_ERROR_CODE', | ||
| msg: 'Internal Server Error', | ||
| result: 'error', | ||
| }); | ||
| }); | ||
|
|
||
| await lolex.runAllTimersAsync(); | ||
| return expect(tryFetchPromise).rejects.toThrow(TimeoutError); | ||
| }); | ||
|
|
||
| test('times out after hanging on one request', async () => { | ||
| const tryFetchPromise = tryFetch(async () => { | ||
| await new Promise((resolve, reject) => {}); | ||
| }); | ||
|
|
||
| await lolex.runAllTimersAsync(); | ||
| return expect(tryFetchPromise).rejects.toThrow(TimeoutError); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchMessages', () => { | ||
| test('message fetch success action is dispatched after successful fetch', async () => { | ||
| const store = mockStore({ | ||
|
|
||
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
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
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.
It looks like in this version you don't end up using
runOnlyPendingTimersAsync.I suspect the key thing that was needed here wasn't the "async", but the "all". In the tests that use
runAllTimersAsync, there's anonTimeoutcallback which... as its name predicts, gets called only after a timeout, and which then creates a new timeout. SorunOnlyPendingTimerswon't run that new timeout, because it wasn't already pending whenrunOnlyPendingTimerswas invoked.This is probably a sign that we should indeed make a habit of just using the "all" versions all the time 🙂, as I suggested in another comment just now, outside of the specific cases where there's a reason we can't.