Skip to content

Commit c479d40

Browse files
authored
fix(event processor): If batchSize = 0 or flushInterval = 0, log that values are invalid and use default values instead (#342)
Summary; Add/change validation for maxQueueSize and flushInterval inputs to AbstractEventProcessor. When invalid, ignore and use default values. Test plan: New unit tests Issues: https://optimizely.atlassian.net/browse/OASIS-5167
1 parent 3398f89 commit c479d40

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

packages/event-processor/CHANGELOG.MD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### New Features
11+
- In `AbstractEventProcessor`, validate maxQueueSize and flushInterval; ignore & use default values when invalid
12+
- `AbstractEventProcessor` can be constructed with a `notificationCenter`. When `notificationCenter` is provided, it triggers a log event notification after the event is sent to the event dispatcher
13+
14+
### Changed
15+
- Removed transformers, interceptors, and callbacks from `AbstractEventProcessor`
16+
1017
## [0.2.1] - June 6, 2019
1118

1219
- Wrap the `callback` in `try/catch` when implementing a custom `eventDispatcher`. This ensures invoking the `callback` will always cleanup any pending retry tasks.
@@ -18,4 +25,4 @@ events that did not send successfully due to page navigation
1825

1926
## [0.1.0] - March 1, 2019
2027

21-
Initial release
28+
Initial release

packages/event-processor/__tests__/v1EventProcessor.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,51 @@ describe('LogTierV1EventProcessor', () => {
369369
expect(notificationCenter.sendNotifications).toBeCalledWith(NOTIFICATION_TYPES.LOG_EVENT, event)
370370
})
371371
})
372+
373+
describe('invalid flushInterval or maxQueueSize', () => {
374+
it('should ignore a flushInterval of 0 and use the default', () => {
375+
const processor = new LogTierV1EventProcessor({
376+
dispatcher: stubDispatcher,
377+
flushInterval: 0,
378+
maxQueueSize: 10,
379+
})
380+
processor.start()
381+
382+
const impressionEvent1 = createImpressionEvent()
383+
processor.process(impressionEvent1)
384+
expect(dispatchStub).toHaveBeenCalledTimes(0)
385+
jest.advanceTimersByTime(30000)
386+
expect(dispatchStub).toHaveBeenCalledTimes(1)
387+
expect(dispatchStub).toHaveBeenCalledWith({
388+
url: 'https://logx.optimizely.com/v1/events',
389+
httpVerb: 'POST',
390+
params: makeBatchedEventV1([impressionEvent1]),
391+
})
392+
})
393+
394+
it('should ignore a maxQueueSize of 0 and use the default', () => {
395+
const processor = new LogTierV1EventProcessor({
396+
dispatcher: stubDispatcher,
397+
flushInterval: 30000,
398+
maxQueueSize: 0,
399+
})
400+
processor.start()
401+
402+
const impressionEvent1 = createImpressionEvent()
403+
processor.process(impressionEvent1)
404+
expect(dispatchStub).toHaveBeenCalledTimes(0)
405+
const impressionEvents = [impressionEvent1]
406+
for (let i = 0; i < 9; i++) {
407+
const evt = createImpressionEvent()
408+
processor.process(evt)
409+
impressionEvents.push(evt)
410+
}
411+
expect(dispatchStub).toHaveBeenCalledTimes(1)
412+
expect(dispatchStub).toHaveBeenCalledWith({
413+
url: 'https://logx.optimizely.com/v1/events',
414+
httpVerb: 'POST',
415+
params: makeBatchedEventV1(impressionEvents),
416+
})
417+
})
418+
})
372419
})

packages/event-processor/src/eventProcessor.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
// TODO change this to use Managed from js-sdk-models when available
1717
import { Managed } from './managed'
1818
import { ConversionEvent, ImpressionEvent } from './events'
19-
import {
20-
EventDispatcher,
21-
EventV1Request,
22-
} from './eventDispatcher'
19+
import { EventDispatcher, EventV1Request } from './eventDispatcher'
2320
import { EventQueue, DefaultEventQueue, SingleEventQueue } from './eventQueue'
2421
import { getLogger } from '@optimizely/js-sdk-logging'
2522
import { NOTIFICATION_TYPES, NotificationCenter } from '@optimizely/js-sdk-utils'
@@ -34,7 +31,9 @@ export interface EventProcessor extends Managed {
3431
process(event: ProcessableEvents): void
3532
}
3633

37-
const MIN_FLUSH_INTERVAL = 100
34+
const DEFAULT_FLUSH_INTERVAL = 30000 // Unit is ms - default flush interval is 30s
35+
const DEFAULT_MAX_QUEUE_SIZE = 10
36+
3837
export abstract class AbstractEventProcessor implements EventProcessor {
3938
protected dispatcher: EventDispatcher
4039
protected queue: EventQueue<ProcessableEvents>
@@ -53,10 +52,25 @@ export abstract class AbstractEventProcessor implements EventProcessor {
5352
}) {
5453
this.dispatcher = dispatcher
5554

55+
if (flushInterval <= 0) {
56+
logger.warn(
57+
`Invalid flushInterval ${flushInterval}, defaulting to ${DEFAULT_FLUSH_INTERVAL}`,
58+
)
59+
flushInterval = DEFAULT_FLUSH_INTERVAL
60+
}
61+
62+
maxQueueSize = Math.floor(maxQueueSize)
63+
if (maxQueueSize < 1) {
64+
logger.warn(
65+
`Invalid maxQueueSize ${maxQueueSize}, defaulting to ${DEFAULT_MAX_QUEUE_SIZE}`,
66+
)
67+
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE
68+
}
69+
5670
maxQueueSize = Math.max(1, maxQueueSize)
5771
if (maxQueueSize > 1) {
5872
this.queue = new DefaultEventQueue({
59-
flushInterval: Math.max(flushInterval, MIN_FLUSH_INTERVAL),
73+
flushInterval,
6074
maxQueueSize,
6175
sink: buffer => this.drainQueue(buffer),
6276
})
@@ -74,15 +88,15 @@ export abstract class AbstractEventProcessor implements EventProcessor {
7488
const promises = this.groupEvents(buffer).map(eventGroup => {
7589
const formattedEvent = this.formatEvents(eventGroup)
7690

77-
return new Promise((resolve) => {
91+
return new Promise(resolve => {
7892
this.dispatcher.dispatchEvent(formattedEvent, () => {
7993
resolve()
8094
})
8195

8296
if (this.notificationCenter) {
8397
this.notificationCenter.sendNotifications(
8498
NOTIFICATION_TYPES.LOG_EVENT,
85-
formattedEvent
99+
formattedEvent,
86100
)
87101
}
88102
})

0 commit comments

Comments
 (0)