Skip to content
61 changes: 60 additions & 1 deletion lib/optimizely/optimizely_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,56 @@
#

require 'optimizely'
require 'optimizely/event_dispatcher'
require 'optimizely/event/batch_event_processor'
module Optimizely
class OptimizelyFactory
attr_reader :max_event_batch_size, :max_event_flush_interval

# Convenience method for setting the maximum number of events contained within a batch.
# @param batch_size Integer - Sets size of EventQueue.
# @param logger - Optional LoggerInterface Provides a log method to log messages.
def self.max_event_batch_size(batch_size, logger)
unless batch_size.is_a? Integer
logger.log(
Logger::ERROR,
"Batch size is invalid, setting to default batch size #{BatchEventProcessor::DEFAULT_BATCH_SIZE}."
)
return
end

unless batch_size.positive?
logger.log(
Logger::ERROR,
"Batch size is negative, setting to default batch size #{BatchEventProcessor::DEFAULT_BATCH_SIZE}."
)
return
end
@max_event_batch_size = batch_size
end

# Convenience method for setting the maximum time interval in milliseconds between event dispatches.
# @param flush_interval Numeric - Time interval between event dispatches.
# @param logger - Optional LoggerInterface Provides a log method to log messages.
def self.max_event_flush_interval(flush_interval, logger)
unless flush_interval.is_a? Numeric
logger.log(
Logger::ERROR,
"Flush interval is invalid, setting to default flush interval #{BatchEventProcessor::DEFAULT_BATCH_INTERVAL}."
)
return
end

unless flush_interval.positive?
logger.log(
Logger::ERROR,
"Flush interval is negative, setting to default flush interval #{BatchEventProcessor::DEFAULT_BATCH_INTERVAL}."
)
return
end
@max_event_flush_interval = flush_interval
end

# Returns a new optimizely instance.
#
# @params sdk_key - Required String uniquely identifying the fallback datafile corresponding to project.
Expand Down Expand Up @@ -46,6 +94,9 @@ def self.default_instance_with_config_manager(config_manager)
# @param user_profile_service - Optional UserProfileServiceInterface Provides methods to store and retreive user profiles.
# @param config_manager - Optional ConfigManagerInterface Responds to get_config.
# @param notification_center - Optional Instance of NotificationCenter.
#
# if @max_event_batch_size and @max_event_flush_interval are nil then default batchsize and flush_interval
# will be used to setup batchEventProcessor.
def self.custom_instance(
sdk_key,
datafile = nil,
Expand All @@ -57,6 +108,13 @@ def self.custom_instance(
config_manager = nil,
notification_center = nil
)
event_processor = BatchEventProcessor.new(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add in a doc, default batchsize and flush_interval will be used to setup batchEventProcessor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

event_dispatcher: event_dispatcher || EventDispatcher.new,
batch_size: @max_event_batch_size,
flush_interval: @max_event_flush_interval,
notification_center: notification_center
)

Optimizely::Project.new(
datafile,
event_dispatcher,
Expand All @@ -66,7 +124,8 @@ def self.custom_instance(
user_profile_service,
sdk_key,
config_manager,
notification_center
notification_center,
event_processor
)
end
end
Expand Down
65 changes: 65 additions & 0 deletions spec/optimizely_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,70 @@ def get_config; end
)
expect(optimizely_instance.config_manager). to eq(http_project_config_manager)
end

it 'should take event processor when flush interval and batch size are set' do
Optimizely::OptimizelyFactory.max_event_flush_interval(5, spy_logger)
Optimizely::OptimizelyFactory.max_event_batch_size(100, spy_logger)

event_processor = Optimizely::BatchEventProcessor.new(
event_dispatcher: event_dispatcher,
batch_size: 100,
flush_interval: 5,
notification_center: notification_center
)

optimizely_instance = Optimizely::OptimizelyFactory.custom_instance(
'sdk_key',
datafile,
event_dispatcher,
spy_logger,
error_handler,
false,
user_profile_service,
nil,
notification_center
)

expect(optimizely_instance.event_processor).equal? event_processor
optimizely_instance.close
end
end

describe '.max_event_batch_size' do
it 'should log error message and return nil when invalid batch size provided' do
expect(Optimizely::OptimizelyFactory.max_event_batch_size([], spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_batch_size(true, spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_batch_size('test', spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_batch_size(5.2, spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_batch_size(nil, spy_logger)). to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Batch size is invalid, setting to default batch size 10.').exactly(5).times
expect(Optimizely::OptimizelyFactory.max_event_batch_size(0, spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_batch_size(-2, spy_logger)). to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Batch size is negative, setting to default batch size 10.').twice
end

it 'should not log error and return batch size and when valid batch size provided' do
expect(Optimizely::OptimizelyFactory.max_event_batch_size(5, spy_logger)). to eq(5)
expect(spy_logger).not_to have_received(:log)
end
end

describe '.max_event_flush_interval' do
it 'should log error message and return nil when invalid flush interval provided' do
expect(Optimizely::OptimizelyFactory.max_event_flush_interval([], spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(true, spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_flush_interval('test', spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(nil, spy_logger)). to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Flush interval is invalid, setting to default flush interval 30000.').exactly(4).times
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(0, spy_logger)). to eq(nil)
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(-2, spy_logger)). to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Flush interval is negative, setting to default flush interval 30000.').twice
end

it 'should not log error and return batch size and when valid flush interval provided' do
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(5, spy_logger)). to eq(5)
expect(Optimizely::OptimizelyFactory.max_event_flush_interval(5.5, spy_logger)). to eq(5.5)
expect(spy_logger).not_to have_received(:log)
end
end
end