Skip to content

Commit

Permalink
update PinpointProvider retry endpointUpdate logic (aws-amplify#4845)
Browse files Browse the repository at this point in the history
* update retry endpoint update logic

* Add unit test for endpoint update retry
  • Loading branch information
ashika01 authored Feb 11, 2020
1 parent 87f4c08 commit 17452eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,51 @@ describe('AnalyticsProvider test', () => {
expect(reject).toBeCalledWith(mockError);
spyon.mockRestore();
});

test('Exceeded maximum endpoint per user count', async () => {
const analytics = new AnalyticsProvider();
const mockExceededMaxError = {
statusCode: 400,
message: 'Exceeded maximum endpoint per user count 10',
};

analytics.configure(options);

const spyonUpdateEndpoint = jest
.spyOn(Pinpoint.prototype, 'updateEndpoint')
// Reject with error the first time we execute updateEndpoint
.mockImplementationOnce(params => ({
promise: jest.fn().mockRejectedValue(mockExceededMaxError),
}))
// Succeed on the second attempt (i.e. after we go through _retryEndpointUpdate)
.mockImplementationOnce(params => ({
promise: jest.fn(() => Promise.resolve('data')),
}));

jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => Promise.resolve(credentials));

jest
.spyOn(AnalyticsProvider.prototype, '_removeUnusedEndpoints')
.mockImplementationOnce(() => ({
promise: jest.fn().mockResolvedValue(true),
}));

const spyonRetryEndpointUpdate = jest.spyOn(
AnalyticsProvider.prototype,
'_retryEndpointUpdate'
);

const params = { event: { name: '_update_endpoint', immediate: true } };

await analytics.record(params, { resolve, reject });

expect(spyonUpdateEndpoint).toHaveBeenCalledTimes(2); // 1 failed + 1 successful call
expect(spyonRetryEndpointUpdate).toHaveBeenCalled();

spyonUpdateEndpoint.mockRestore();
});
});
});
});
11 changes: 10 additions & 1 deletion packages/analytics/src/Providers/AWSPinpointProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,21 @@ export default class AWSPinpointProvider implements AnalyticsProvider {
const { params } = endpointObject;

// TODO: implement retry with exp back off once exp function is available
const {
config: { resendLimit },
} = params;

params.resendLimit =
typeof params.resendLimit === 'number' ? params.resendLimit : resendLimit;

if (params.resendLimit-- > 0) {
logger.debug(
`resending endpoint update ${params.event.eventId} with ${params.resendLimit} retry attempts remaining`
);
// insert at the front of endpointBuffer
this._endpointBuffer.unshift(endpointObject);
this._endpointBuffer.length
? this._endpointBuffer.unshift(endpointObject)
: this._updateEndpoint(endpointObject);
return;
}

Expand All @@ -512,6 +520,7 @@ export default class AWSPinpointProvider implements AnalyticsProvider {

private async _removeUnusedEndpoints(appId, userId) {
return new Promise((res, rej) => {
// TODO: re-write with Promise (during refactor pt. 2)
this.pinpointClient.getUserEndpoints(
{
ApplicationId: appId,
Expand Down

0 comments on commit 17452eb

Please sign in to comment.