File size: 5,439 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import deepFreeze from 'deep-freeze';
import { merge } from 'lodash';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { retryOnFailure as rof } from '../';
import { noRetry, exponentialBackoff } from '../policies';
jest.useFakeTimers();
const retryOnFailure = rof();
const retryWithDelay = ( delay ) => rof( () => delay );
const nextError = { fail: 'failed big time' };
const getSites = deepFreeze(
http( {
method: 'GET',
path: '/sites',
apiVersion: 'v1',
} )
);
const withRetries = ( retryCount ) => ( actionOrInbound ) =>
undefined !== actionOrInbound.originalRequest
? merge( actionOrInbound, {
originalRequest: withRetries( retryCount )( actionOrInbound.originalRequest ),
} )
: merge( actionOrInbound, { meta: { dataLayer: { retryCount } } } );
const createMockStore = () => ( { dispatch: jest.fn() } );
describe( '#retryOnFailure', () => {
test( 'should pass through initially successful requests', () => {
const inbound = { nextData: 1, originalRequest: getSites, store: createMockStore() };
expect( retryOnFailure( inbound ) ).toEqual( inbound );
jest.advanceTimersByTime( 20000 );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
} );
test( 'should pass through no-retry failed requests', () => {
const originalRequest = { ...getSites, options: { retryPolicy: noRetry() } };
const inbound = { nextError, originalRequest, store: createMockStore() };
expect( retryOnFailure( inbound ) ).toEqual( inbound );
jest.advanceTimersByTime( 20000 );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
} );
test( 'should pass through POST requests', () => {
const originalRequest = { ...getSites, method: 'POST' };
const inbound = { nextError, originalRequest, store: createMockStore() };
expect( retryOnFailure( inbound ) ).toEqual( inbound );
jest.advanceTimersByTime( 20000 );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
} );
test( 'should requeue a plain failed request', () => {
const inbound = { nextError, originalRequest: getSites, store: createMockStore() };
expect( retryWithDelay( 1337 )( inbound ) ).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
jest.advanceTimersByTime( 1337 );
expect( inbound.store.dispatch ).toHaveBeenCalledWith( withRetries( 1 )( getSites ) );
} );
test( 'should requeue only up to `maxAttempts`', () => {
const originalRequest = { ...getSites, options: { retryPolicy: { maxAttempts: 3 } } };
const inbound = { nextError, originalRequest, store: createMockStore() };
const retryIt = retryWithDelay( 1337 );
// retry 1
expect( retryIt( inbound ) ).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
jest.advanceTimersByTime( 1337 );
expect( inbound.store.dispatch ).toHaveBeenCalledWith( withRetries( 1 )( originalRequest ) );
// retry 2
expect(
retryIt( { ...inbound, originalRequest: inbound.store.dispatch.mock.lastCall[ 0 ] } )
).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 1 );
jest.advanceTimersByTime( 1337 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
expect( inbound.store.dispatch ).toHaveBeenCalledWith( withRetries( 2 )( originalRequest ) );
// retry 3
expect(
retryIt( { ...inbound, originalRequest: inbound.store.dispatch.mock.lastCall[ 0 ] } )
).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
jest.advanceTimersByTime( 1337 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 3 );
expect( inbound.store.dispatch ).toHaveBeenCalledWith( withRetries( 3 )( originalRequest ) );
// retry 4
const finalRequest = { ...inbound, originalRequest: inbound.store.dispatch.mock.lastCall[ 0 ] };
expect( retryIt( finalRequest ) ).toEqual( finalRequest );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 3 );
jest.advanceTimersByTime( 1337 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 3 );
} );
test( 'should handle `exponentialBackoff`', () => {
const originalRequest = {
...getSites,
options: { retryPolicy: exponentialBackoff( { delay: 1000, maxAttempts: 5 } ) },
};
const inbound = { nextError, originalRequest, store: createMockStore() };
// retry 1
expect( retryOnFailure( inbound ) ).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).not.toHaveBeenCalled();
jest.advanceTimersByTime( 1000 + 3 * 1000 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 1 );
jest.advanceTimersByTime( 200000 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 1 );
// retry 4 (should have much longer delay)
expect( retryOnFailure( withRetries( 4 )( inbound ) ) ).toHaveProperty( 'shouldAbort', true );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 1 );
jest.advanceTimersByTime( 1000 + 3 * 16000 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
jest.advanceTimersByTime( 200000 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
// retry 5 (should not retry)
expect( retryOnFailure( withRetries( 5 )( inbound ) ) ).toEqual( withRetries( 5 )( inbound ) );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
jest.advanceTimersByTime( 200000 );
expect( inbound.store.dispatch ).toHaveBeenCalledTimes( 2 );
} );
} );
|