Skip to content

Commit 346da4e

Browse files
Fix setting timeout; add multiple flush test
1 parent 32a0c10 commit 346da4e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/optimizely-sdk/lib/plugins/odp/odp_event_manager.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { RestApiManager } from './rest_api_manager';
2424
const MAX_RETRIES = 3;
2525
const DEFAULT_BATCH_SIZE = 10;
2626
const DEFAULT_FLUSH_INTERVAL_MSECS = 1000;
27-
const DEFAULT_BROWSER_QUEUE_SIZE = 10;
27+
const DEFAULT_BROWSER_QUEUE_SIZE = 100;
2828
const DEFAULT_SERVER_QUEUE_SIZE = 10000;
2929

3030
/**
@@ -255,19 +255,15 @@ export class OdpEventManager implements IOdpEventManager {
255255
}
256256

257257
// Flush interval occurred & queue has items
258-
if (shouldFlush && this.queueContainsItems()) {
258+
if (shouldFlush) {
259259
// clear the queue completely
260260
this.clearCurrentTimeout();
261261

262262
this.state = STATE.PROCESSING;
263263

264264
while (this.queueContainsItems()) {
265-
this.makeAndSendBatch();
265+
this.makeAndSend1Batch();
266266
}
267-
268-
this.state = STATE.RUNNING;
269-
270-
this.setNewTimeout();
271267
}
272268
// Check if queue has a full batch available
273269
else if (this.queueHasBatches()) {
@@ -276,13 +272,12 @@ export class OdpEventManager implements IOdpEventManager {
276272
this.state = STATE.PROCESSING;
277273

278274
while (this.queueHasBatches()) {
279-
this.makeAndSendBatch();
275+
this.makeAndSend1Batch();
280276
}
281-
282-
this.state = STATE.RUNNING;
283-
284-
this.setNewTimeout();
285277
}
278+
279+
this.state = STATE.RUNNING;
280+
this.setNewTimeout();
286281
}
287282

288283
/**
@@ -309,9 +304,10 @@ export class OdpEventManager implements IOdpEventManager {
309304
* Make a batch and send it to ODP
310305
* @private
311306
*/
312-
private makeAndSendBatch(): void {
307+
private makeAndSend1Batch(): void {
313308
const batch = new Array<OdpEvent>();
314309

310+
// remove a batch from the queue
315311
for (let count = 0; count < this.batchSize; count += 1) {
316312
const event = this.queue.shift();
317313
if (event) {
@@ -322,6 +318,7 @@ export class OdpEventManager implements IOdpEventManager {
322318
}
323319

324320
if (batch.length > 0) {
321+
// put sending the event on another event loop
325322
setTimeout(async () => {
326323
let shouldRetry: boolean;
327324
let attemptNumber = 0;

packages/optimizely-sdk/tests/odpEventManager.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { OdpConfig } from '../lib/plugins/odp/odp_config';
1818
import { OdpEventManager, STATE } from '../lib/plugins/odp/odp_event_manager';
19-
import { anything, capture, instance, mock, resetCalls, verify, when } from 'ts-mockito';
19+
import { anything, capture, instance, mock, resetCalls, spy, verify, when } from 'ts-mockito';
2020
import { RestApiManager } from '../lib/plugins/odp/rest_api_manager';
2121
import { LogHandler, LogLevel } from '../lib/modules/logging';
2222
import { OdpEvent } from '../lib/plugins/odp/odp_event';
@@ -204,6 +204,20 @@ describe('OdpEventManager', () => {
204204
expect(eventData.get('key-4')).toEqual(processedEventData.get('key-4'));
205205
});
206206

207+
it('should attempt to flush an empty queue at flush intervals', async () => {
208+
const eventManager = new OdpEventManager({
209+
odpConfig, apiManager, logger, clientEngine, clientVersion,
210+
flushInterval: 100,
211+
});
212+
const spiedEventManager = spy(eventManager);
213+
214+
eventManager.start();
215+
// do not add events to the queue, but allow for...
216+
await pause(400); // at least 3 flush intervals executions (giving a little longer)
217+
218+
verify(spiedEventManager['processQueue'](anything())).atLeast(3);
219+
});
220+
207221
it('should dispatch events in correct number of batches', async () => {
208222
when(mockApiManager.sendEvents(anything(), anything(), anything())).thenResolve(false);
209223
const apiManager = instance(mockApiManager);

0 commit comments

Comments
 (0)