Skip to content

Commit

Permalink
fix(analytics): Buffer limit should be adhered to (aws-amplify#10015)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Fang <chrfang@amazon.com>
  • Loading branch information
cshfang and Chris Fang committed Jun 29, 2022
1 parent 11b537c commit 3dd9035
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 1 deletion.
67 changes: 67 additions & 0 deletions packages/analytics/__tests__/Providers/EventBuffer.test.ts
@@ -0,0 +1,67 @@
/*
* Copyright 2017-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import EventBuffer from '../../src/Providers/EventBuffer';

const DEFAULT_CONFIG = {
bufferSize: 1000,
flushSize: 100,
flushInterval: 5 * 1000, // 5s
resendLimit: 5,
};

const EVENT_OBJECT = {
params: {
event: {
eventId: 'event-id',
name: 'name',
attributes: 'attributes',
metrics: 'metrics',
session: {},
immediate: false,
},
timestamp: '2022-06-22T17:24:58Z',
config: {
appId: 'app-id',
endpointId: 'endpoint-id',
region: 'region',
resendLimit: 5,
},
credentials: {},
resendLimit: 5,
},
handlers: {
resolve: jest.fn(),
reject: jest.fn(),
},
};

describe('EventBuffer', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('can be constructed', () => {
const buffer = new EventBuffer({}, DEFAULT_CONFIG);
expect(buffer).toBeDefined();
});

test('does not allow buffer size to be exceeded', () => {
const config = { ...DEFAULT_CONFIG, bufferSize: 1 };
const buffer = new EventBuffer({}, config);
buffer.push(EVENT_OBJECT);
buffer.push(EVENT_OBJECT);
expect(EVENT_OBJECT.handlers.reject).toBeCalledWith(
Error('Exceeded the size of analytics events buffer')
);
});
});
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/analytics/src/Providers/EventBuffer.ts
Expand Up @@ -43,7 +43,8 @@ export default class EventsBuffer {
}

public push(event: EventObject) {
if (this._buffer > this._config.bufferSize) {
// if the buffer is currently at the configured limit, pushing would exceed it
if (this._buffer.length >= this._config.bufferSize) {
logger.debug('Exceeded analytics events buffer size');
return event.handlers.reject(
new Error('Exceeded the size of analytics events buffer')
Expand Down

0 comments on commit 3dd9035

Please sign in to comment.