Skip to content

Commit 05c8aee

Browse files
trying to cleanup last few unit tests
1 parent 245d067 commit 05c8aee

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

spec/event/forwarding_event_processor_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878

7979
forwarding_event_processor.process(@conversion_event)
8080

81+
sleep 0.1
82+
8183
expect(notification_center).to have_received(:send_notifications).with(
8284
Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT],
8385
Optimizely::Event.new(:post, log_url, @expected_conversion_params, post_headers)

spec/project_spec.rb

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class InvalidErrorHandler; end
187187
stub_request(:post, impression_log_url).with(query: params)
188188

189189
expect(project_instance.activate('test_experiment', 'test_user')).to eq('control')
190+
sleep 0.1
190191
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
191192
expect(project_instance.decision_service.bucketer).to have_received(:bucket).once
192193
end
@@ -205,6 +206,7 @@ class InvalidErrorHandler; end
205206
stub_request(:post, impression_log_url).with(query: params)
206207

207208
expect(project_instance.activate('test_experiment', 'test_user')).to eq('control')
209+
sleep 0.1
208210
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
209211
end
210212

@@ -296,6 +298,7 @@ class InvalidErrorHandler; end
296298
# Should be included via exact match string audience with id '3468206642'
297299
expect(@project_typed_audience_instance.activate('typed_audience_experiment', 'test_user', 'house' => 'Gryffindor'))
298300
.to eq('A')
301+
sleep 0.1
299302
expect(@project_typed_audience_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
300303
expect(@project_typed_audience_instance.decision_service.bucketer).to have_received(:bucket).once
301304
end
@@ -371,6 +374,7 @@ class InvalidErrorHandler; end
371374

372375
expect(@project_typed_audience_instance.activate('audience_combinations_experiment', 'test_user', user_attributes))
373376
.to eq('A')
377+
sleep 0.1
374378
expect(@project_typed_audience_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
375379
expect(@project_typed_audience_instance.decision_service.bucketer).to have_received(:bucket).once
376380
end
@@ -432,6 +436,7 @@ class InvalidErrorHandler; end
432436

433437
expect(project_instance.activate('test_experiment_with_audience', 'test_user', attributes))
434438
.to eq('control_with_audience')
439+
sleep 0.1
435440
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
436441
expect(project_instance.decision_service.bucketer).to have_received(:bucket).once
437442
end
@@ -492,6 +497,7 @@ class InvalidErrorHandler; end
492497
project_instance.decision_service.set_forced_variation(project_config, 'test_experiment_with_audience', 'test_user', 'variation_with_audience')
493498
variation_to_return = project_instance.decision_service.get_forced_variation(project_config, 'test_experiment', 'test_user')
494499
allow(project_instance.decision_service.bucketer).to receive(:bucket).and_return(variation_to_return)
500+
sleep 0.1
495501
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
496502

497503
expect(project_instance.activate('test_experiment_with_audience', 'test_user', 'browser_type' => 'firefox'))
@@ -595,13 +601,16 @@ def callback(_args); end
595601
method(:callback)
596602
)
597603
variation_to_return = project_config.get_variation_from_id('test_experiment', '111128')
604+
sleep 0.1
598605
allow(project_instance.decision_service.bucketer).to receive(:bucket).and_return(variation_to_return)
599606
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
600607
allow(project_config).to receive(:get_audience_ids_for_experiment)
601608
.with('test_experiment')
602609
.and_return([])
603610
experiment = project_config.get_experiment_from_key('test_experiment')
604611

612+
# sleep because forwarder is wrapped in a thread
613+
sleep 0.1
605614
# Decision listener
606615
expect(project_instance.notification_center).to receive(:send_notifications).with(
607616
Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], any_args
@@ -621,6 +630,8 @@ def callback(_args); end
621630

622631
project_instance.activate('test_experiment', 'test_user')
623632

633+
sleep 0.1
634+
624635
expect(spy_logger).to have_received(:log).once.with(Logger::INFO, "Activating user 'test_user' in experiment 'test_experiment'.")
625636
end
626637

@@ -634,6 +645,7 @@ def callback(_args); end
634645
allow(project_instance.decision_service.bucketer).to receive(:bucket).and_return(variation_to_return)
635646
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(any_args).and_raise(RuntimeError)
636647
project_instance.activate('test_experiment', 'test_user')
648+
sleep 0.1
637649
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, "Error dispatching event: #{log_event} RuntimeError.")
638650
end
639651

@@ -661,6 +673,7 @@ def callback(_args); end
661673

662674
expect(project_instance.activate('test_experiment_with_audience', 'forced_audience_user', 'browser_type' => 'wrong_browser'))
663675
.to eq('variation_with_audience')
676+
sleep 0.1
664677
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once
665678
expect(Optimizely::Audience).to_not have_received(:user_in_experiment?)
666679
end
@@ -712,6 +725,7 @@ def callback(_args); end
712725
expect(project_instance.notification_center).to receive(:send_notifications).ordered
713726

714727
project_instance.activate('test_experiment', 'test_user')
728+
sleep 0.1
715729
end
716730
end
717731

@@ -720,16 +734,17 @@ def callback(_args); end
720734
WebMock.allow_net_connect!
721735
notification_center = Optimizely::NotificationCenter.new(spy_logger, error_handler)
722736

723-
expect(notification_center).to receive(:send_notifications).with(
737+
expect(notification_center).to receive(:send_notifications).once.with(
724738
Optimizely::NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE]
725739
).ordered
726740

727741
expect(notification_center).to receive(:send_notifications).ordered
728742

729743
expect(notification_center).to receive(:send_notifications).ordered
730744
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
745+
logger:spy_logger,
731746
url: 'https://cdn.optimizely.com/datafiles/QBw9gFM8oTn7ogY9ANCC1z.json',
732-
notification_center: notification_center
747+
notification_center: notification_center,
733748
)
734749

735750
project_instance = Optimizely::Project.new(
@@ -741,18 +756,20 @@ def callback(_args); end
741756

742757
expect(http_project_config_manager.config).not_to eq(nil)
743758
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
759+
sleep 0.5
744760
end
745761

746762
it 'should update config, send update notification when sdk key is provided' do
747763
WebMock.allow_net_connect!
748764
notification_center = Optimizely::NotificationCenter.new(spy_logger, error_handler)
765+
allow(notification_center).to receive(:send_notifications)
749766

750767
expect(notification_center).to receive(:send_notifications).with(
751768
Optimizely::NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE]
752-
).ordered
769+
)
753770

754-
expect(notification_center).to receive(:send_notifications).ordered
755-
expect(notification_center).to receive(:send_notifications).ordered
771+
expect(notification_center).to receive(:send_notifications)
772+
expect(notification_center).to receive(:send_notifications)
756773

757774
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
758775
sdk_key: 'QBw9gFM8oTn7ogY9ANCC1z',
@@ -768,6 +785,7 @@ def callback(_args); end
768785

769786
expect(http_project_config_manager.config).not_to eq(nil)
770787
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
788+
sleep 0.5
771789
end
772790
end
773791

@@ -792,6 +810,7 @@ def callback(_args); end
792810

793811
expect(project_instance.is_valid).to be true
794812
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
813+
sleep 0.5
795814
end
796815
end
797816
end
@@ -859,13 +878,15 @@ def callback(_args); end
859878

860879
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
861880
project_instance.track('test_event', 'test_user')
881+
sleep 0.1
862882
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
863883
end
864884

865885
it 'should properly track an event by calling dispatch_event with right params after forced variation' do
866886
project_instance.decision_service.set_forced_variation(project_config, 'test_experiment', 'test_user', 'variation')
867887
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
868888
project_instance.track('test_event', 'test_user')
889+
sleep 0.1
869890
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, @expected_track_event_params, post_headers)).once
870891
end
871892

@@ -878,6 +899,7 @@ def callback(_args); end
878899
project_instance.decision_service.set_forced_variation(project_config, 'test_experiment', 'test_user', 'variation')
879900
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
880901
project_instance.track('test_event', 'test_user', nil, revenue: 42)
902+
sleep 0.1
881903
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
882904
end
883905

@@ -889,6 +911,7 @@ def callback(_args); end
889911
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(any_args).and_raise(RuntimeError)
890912

891913
project_instance.track('test_event', 'test_user')
914+
sleep 0.1
892915
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, "Error dispatching event: #{log_event} RuntimeError.")
893916
end
894917

@@ -917,6 +940,7 @@ def callback(_args); end
917940
).ordered
918941

919942
project_instance.track('test_event', 'test_user', nil, 'revenue' => 42)
943+
sleep 0.1
920944
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
921945
end
922946

@@ -933,6 +957,7 @@ def callback(_args); end
933957

934958
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
935959
project_instance.track('test_event_with_audience', 'test_user', 'browser_type' => 'firefox')
960+
sleep 0.1
936961
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
937962
end
938963

@@ -980,6 +1005,7 @@ def callback(_args); end
9801005
# Should be included via substring match string audience with id '3988293898'
9811006
allow(@project_typed_audience_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
9821007
@project_typed_audience_instance.track('item_bought', 'test_user', 'house' => 'Welcome to Slytherin!')
1008+
sleep 0.1
9831009
expect(@project_typed_audience_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, @expected_event_params, post_headers)).once
9841010
end
9851011

@@ -988,6 +1014,7 @@ def callback(_args); end
9881014
params[:visitors][0][:attributes][0][:value] = 'Welcome to Hufflepuff!'
9891015
allow(@project_typed_audience_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
9901016
@project_typed_audience_instance.track('item_bought', 'test_user', 'house' => 'Welcome to Hufflepuff!')
1017+
sleep 0.1
9911018
expect(@project_typed_audience_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
9921019
end
9931020

@@ -1018,6 +1045,7 @@ def callback(_args); end
10181045
params[:visitors][0][:snapshots][0][:events][0][:key] = 'user_signed_up'
10191046
allow(@project_typed_audience_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
10201047
@project_typed_audience_instance.track('user_signed_up', 'test_user', user_attributes)
1048+
sleep 0.1
10211049
expect(@project_typed_audience_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
10221050
end
10231051
end
@@ -1035,6 +1063,7 @@ def callback(_args); end
10351063

10361064
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
10371065
project_instance.track('test_event_with_audience', 'test_user', 'browser_type' => 'cyberdog')
1066+
sleep 0.1
10381067
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
10391068
end
10401069

@@ -1044,6 +1073,7 @@ def callback(_args); end
10441073
params[:visitors][0][:snapshots][0][:events][0][:key] = 'test_event_not_running'
10451074
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
10461075
project_instance.track('test_event_not_running', 'test_user')
1076+
sleep 0.1
10471077
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once
10481078
end
10491079

@@ -1055,6 +1085,7 @@ def callback(_args); end
10551085
)
10561086
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
10571087
project_instance.track('test_event', 'test_user', nil, 'revenue' => 42)
1088+
sleep 0.1
10581089
expect(spy_logger).to have_received(:log).with(Logger::INFO, "Tracking event 'test_event' for user 'test_user'.")
10591090
end
10601091

@@ -1066,6 +1097,7 @@ def callback(_args); end
10661097
it 'should return false when called with attributes in an invalid format' do
10671098
expect(project_instance.error_handler).to receive(:handle_error).with(any_args).once.and_return(nil)
10681099
project_instance.track('test_event', 'test_user', 'invalid')
1100+
sleep 0.1
10691101
end
10701102

10711103
it 'should raise an exception when called with event tags in an invalid format' do
@@ -1538,6 +1570,7 @@ def callback(_args); end
15381570
allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(decision_to_return)
15391571

15401572
expect(project_instance.is_feature_enabled('multi_variate_feature', 'test_user')).to be false
1573+
sleep 0.2
15411574
expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(instance_of(Optimizely::Event)).once
15421575
expect(spy_logger).to have_received(:log).once.with(Logger::INFO, "Feature 'multi_variate_feature' is not enabled for user 'test_user'.")
15431576
end
@@ -1560,7 +1593,7 @@ def callback(_args); end
15601593

15611594
# Decision listener called when the user is in experiment with variation feature on.
15621595
expect(variation_to_return['featureEnabled']).to be true
1563-
expect(project_instance.notification_center).to receive(:send_notifications).once.with(
1596+
expect(project_instance.notification_center).to receive(:send_notifications).with(
15641597
Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION],
15651598
'feature', 'test_user', {},
15661599
feature_enabled: true,
@@ -1573,6 +1606,7 @@ def callback(_args); end
15731606
).ordered
15741607

15751608
project_instance.is_feature_enabled('multi_variate_feature', 'test_user')
1609+
sleep 0.5
15761610
end
15771611

15781612
it 'should call decision listener when user is bucketed into a feature experiment with featureEnabled property is false' do
@@ -1604,6 +1638,7 @@ def callback(_args); end
16041638
)
16051639

16061640
project_instance.is_feature_enabled('multi_variate_feature', 'test_user', 'browser_type' => 'chrome')
1641+
sleep 0.5
16071642
end
16081643

16091644
it 'should call decision listener when user is bucketed into rollout with featureEnabled property is true' do
@@ -1628,6 +1663,7 @@ def callback(_args); end
16281663
)
16291664

16301665
project_instance.is_feature_enabled('boolean_single_variable_feature', 'test_user', 'browser_type' => 'firefox')
1666+
sleep 0.1
16311667
end
16321668

16331669
it 'should call decision listener when user is bucketed into rollout with featureEnabled property is false' do
@@ -1644,6 +1680,7 @@ def callback(_args); end
16441680
)
16451681

16461682
project_instance.is_feature_enabled('boolean_single_variable_feature', 'test_user')
1683+
sleep 0.1
16471684
end
16481685

16491686
it 'call decision listener when the user is not bucketed into any experiment or rollout' do
@@ -2814,7 +2851,7 @@ def callback(_args); end
28142851
project_instance = Optimizely::Project.new(nil, nil, nil, nil, true, nil, nil, config_manager, nil, event_processor)
28152852

28162853
expect(config_manager.stopped).to be false
2817-
expect(event_processor.started).to be true
2854+
expect(event_processor.started).to be false
28182855

28192856
project_instance.close
28202857

0 commit comments

Comments
 (0)