@@ -20,6 +20,8 @@ var Optimizely = require('./optimizely');
2020var optimizelyFactory = require ( './index.browser' ) ;
2121var packageJSON = require ( '../package.json' ) ;
2222var testData = require ( './tests/test_data' ) ;
23+ var eventProcessor = require ( '@optimizely/js-sdk-event-processor' ) ;
24+ var eventProcessorConfigValidator = require ( './utils/event_processor_config_validator' ) ;
2325
2426var chai = require ( 'chai' ) ;
2527var assert = chai . assert ;
@@ -398,6 +400,133 @@ describe('javascript-sdk', function() {
398400 sinon . assert . calledWithExactly ( logging . setLogHandler , fakeLogger ) ;
399401 } ) ;
400402 } ) ;
403+
404+ describe ( 'event processor configuration' , function ( ) {
405+ var eventProcessorSpy ;
406+ beforeEach ( function ( ) {
407+ eventProcessorSpy = sinon . stub ( eventProcessor , 'LogTierV1EventProcessor' ) . callThrough ( ) ;
408+ } ) ;
409+
410+ afterEach ( function ( ) {
411+ eventProcessor . LogTierV1EventProcessor . restore ( ) ;
412+ } ) ;
413+
414+ it ( 'should use default event flush interval when none is provided' , function ( ) {
415+ optimizelyFactory . createInstance ( {
416+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
417+ errorHandler : fakeErrorHandler ,
418+ eventDispatcher : fakeEventDispatcher ,
419+ logger : silentLogger ,
420+ } ) ;
421+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
422+ flushInterval : 1000 ,
423+ } ) ) ;
424+ } ) ;
425+
426+ describe ( 'with an invalid flush interval' , function ( ) {
427+ beforeEach ( function ( ) {
428+ sinon . stub ( eventProcessorConfigValidator , 'validateEventFlushInterval' ) . returns ( false ) ;
429+ } ) ;
430+
431+ afterEach ( function ( ) {
432+ eventProcessorConfigValidator . validateEventFlushInterval . restore ( ) ;
433+ } ) ;
434+
435+ it ( 'should ignore the event flush interval and use the default instead' , function ( ) {
436+ optimizelyFactory . createInstance ( {
437+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
438+ errorHandler : fakeErrorHandler ,
439+ eventDispatcher : fakeEventDispatcher ,
440+ logger : silentLogger ,
441+ eventFlushInterval : [ 'invalid' , 'flush' , 'interval' ] ,
442+ } ) ;
443+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
444+ flushInterval : 1000 ,
445+ } ) ) ;
446+ } ) ;
447+ } ) ;
448+
449+ describe ( 'with a valid flush interval' , function ( ) {
450+ beforeEach ( function ( ) {
451+ sinon . stub ( eventProcessorConfigValidator , 'validateEventFlushInterval' ) . returns ( true ) ;
452+ } ) ;
453+
454+ afterEach ( function ( ) {
455+ eventProcessorConfigValidator . validateEventFlushInterval . restore ( ) ;
456+ } ) ;
457+
458+ it ( 'should use the provided event flush interval' , function ( ) {
459+ optimizelyFactory . createInstance ( {
460+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
461+ errorHandler : fakeErrorHandler ,
462+ eventDispatcher : fakeEventDispatcher ,
463+ logger : silentLogger ,
464+ eventFlushInterval : 9000 ,
465+ } ) ;
466+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
467+ flushInterval : 9000 ,
468+ } ) ) ;
469+ } ) ;
470+ } ) ;
471+
472+ it ( 'should use default event batch size when none is provided' , function ( ) {
473+ optimizelyFactory . createInstance ( {
474+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
475+ errorHandler : fakeErrorHandler ,
476+ eventDispatcher : fakeEventDispatcher ,
477+ logger : silentLogger ,
478+ } ) ;
479+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
480+ maxQueueSize : 10 ,
481+ } ) ) ;
482+ } ) ;
483+
484+ describe ( 'with an invalid event batch size' , function ( ) {
485+ beforeEach ( function ( ) {
486+ sinon . stub ( eventProcessorConfigValidator , 'validateEventBatchSize' ) . returns ( false ) ;
487+ } ) ;
488+
489+ afterEach ( function ( ) {
490+ eventProcessorConfigValidator . validateEventBatchSize . restore ( ) ;
491+ } ) ;
492+
493+ it ( 'should ignore the event batch size and use the default instead' , function ( ) {
494+ optimizelyFactory . createInstance ( {
495+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
496+ errorHandler : fakeErrorHandler ,
497+ eventDispatcher : fakeEventDispatcher ,
498+ logger : silentLogger ,
499+ eventBatchSize : null ,
500+ } ) ;
501+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
502+ maxQueueSize : 10 ,
503+ } ) ) ;
504+ } ) ;
505+ } ) ;
506+
507+ describe ( 'with a valid event batch size' , function ( ) {
508+ beforeEach ( function ( ) {
509+ sinon . stub ( eventProcessorConfigValidator , 'validateEventBatchSize' ) . returns ( true ) ;
510+ } ) ;
511+
512+ afterEach ( function ( ) {
513+ eventProcessorConfigValidator . validateEventBatchSize . restore ( ) ;
514+ } ) ;
515+
516+ it ( 'should use the provided event batch size' , function ( ) {
517+ optimizelyFactory . createInstance ( {
518+ datafile : testData . getTestProjectConfigWithFeatures ( ) ,
519+ errorHandler : fakeErrorHandler ,
520+ eventDispatcher : fakeEventDispatcher ,
521+ logger : silentLogger ,
522+ eventBatchSize : 300 ,
523+ } ) ;
524+ sinon . assert . calledWithExactly ( eventProcessorSpy , sinon . match ( {
525+ maxQueueSize : 300 ,
526+ } ) ) ;
527+ } ) ;
528+ } ) ;
529+ } ) ;
401530 } ) ;
402531 } ) ;
403532} ) ;
0 commit comments