@@ -12,14 +12,151 @@ module LaunchDarkly
1212 let ( :starting_timestamp ) { 1000 }
1313 let ( :default_config_opts ) { { diagnostic_opt_out : true , logger : $null_log } }
1414 let ( :default_config ) { Config . new ( default_config_opts ) }
15+ let ( :omit_anonymous_contexts_config ) { Config . new ( default_config_opts . merge ( omit_anonymous_contexts : true ) ) }
1516 let ( :context ) { LDContext . create ( { kind : "user" , key : "userkey" , name : "Red" } ) }
17+ let ( :anon_context ) { LDContext . create ( { kind : "org" , key : "orgkey" , name : "Organization" , anonymous : true } ) }
1618
17- it "queues identify event" do
18- with_processor_and_sender ( default_config , starting_timestamp ) do |ep , sender |
19- ep . record_identify_event ( context )
19+ describe "identify events" do
20+ it "can be queued" do
21+ with_processor_and_sender ( default_config , starting_timestamp ) do |ep , sender |
22+ ep . record_identify_event ( context )
2023
21- output = flush_and_get_events ( ep , sender )
22- expect ( output ) . to contain_exactly ( eq ( identify_event ( default_config , context ) ) )
24+ output = flush_and_get_events ( ep , sender )
25+ expect ( output ) . to contain_exactly ( eq ( identify_event ( default_config , context ) ) )
26+ end
27+ end
28+
29+ it "does queue if anonymous" do
30+ with_processor_and_sender ( default_config , starting_timestamp ) do |ep , sender |
31+ ep . record_identify_event ( anon_context )
32+
33+ output = flush_and_get_events ( ep , sender )
34+ expect ( output ) . to contain_exactly ( eq ( identify_event ( default_config , anon_context ) ) )
35+ end
36+ end
37+
38+ it "does not queue if anonymous and omit_anonymous_contexts" do
39+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
40+ ep . record_identify_event ( anon_context )
41+
42+ output = flush_and_get_events ( ep , sender )
43+ expect ( output ) . to be_nil
44+ end
45+ end
46+
47+ it "strips anonymous contexts from multi kind contexts if omit_anonymous_contexts" do
48+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
49+ user = LDContext . create ( { kind : "user" , key : "userkey" , name : "Example User" , anonymous : true } )
50+ org = LDContext . create ( { kind : "org" , key : "orgkey" , name : "Big Organization" } )
51+ device = LDContext . create ( { kind : "device" , key : "devicekey" , name : "IoT Device" , anonymous : true } )
52+
53+ ep . record_identify_event ( LDContext . create_multi ( [ user , org , device ] ) )
54+
55+ output = flush_and_get_events ( ep , sender )
56+ expect ( output ) . to contain_exactly ( eq ( identify_event ( omit_anonymous_contexts_config , org ) ) )
57+ end
58+ end
59+
60+ it "does not queue if all are anonymous and omit_anonymous_contexts" do
61+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
62+ user = LDContext . create ( { kind : "user" , key : "userkey" , name : "Example User" , anonymous : true } )
63+ org = LDContext . create ( { kind : "org" , key : "orgkey" , name : "Big Organization" , anonymous : true } )
64+ device = LDContext . create ( { kind : "device" , key : "devicekey" , name : "IoT Device" , anonymous : true } )
65+
66+ ep . record_identify_event ( LDContext . create_multi ( [ user , org , device ] ) )
67+
68+ output = flush_and_get_events ( ep , sender )
69+ expect ( output ) . to be_nil
70+ end
71+ end
72+ end
73+
74+ describe "index events" do
75+ it "does not ignore single-kind anonymous context" do
76+ with_processor_and_sender ( default_config , starting_timestamp ) do |ep , sender |
77+ flag = { key : "flagkey" , version : 11 }
78+ ep . record_eval_event ( anon_context , 'flagkey' , 11 , 1 , 'value' , nil , nil , true )
79+
80+ output = flush_and_get_events ( ep , sender )
81+ expect ( output ) . to contain_exactly (
82+ eq ( index_event ( default_config , anon_context ) ) ,
83+ eq ( feature_event ( default_config , flag , anon_context , 1 , 'value' ) ) ,
84+ include ( :kind => "summary" )
85+ )
86+
87+ summary = output . detect { |e | e [ :kind ] == "summary" }
88+ expect ( summary [ :features ] [ :flagkey ] [ :contextKinds ] ) . to contain_exactly ( "org" )
89+ end
90+ end
91+
92+ it "ignore single-kind anonymous context if omit_anonymous_contexts" do
93+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
94+ flag = { key : "flagkey" , version : 11 }
95+ ep . record_eval_event ( anon_context , 'flagkey' , 11 , 1 , 'value' , nil , nil , true )
96+
97+ output = flush_and_get_events ( ep , sender )
98+ expect ( output ) . to contain_exactly (
99+ eq ( feature_event ( omit_anonymous_contexts_config , flag , anon_context , 1 , 'value' ) ) ,
100+ include ( :kind => "summary" )
101+ )
102+
103+ summary = output . detect { |e | e [ :kind ] == "summary" }
104+ expect ( summary [ :features ] [ :flagkey ] [ :contextKinds ] ) . to contain_exactly ( "org" )
105+ end
106+ end
107+
108+ it "ignore anonymous contexts from multi-kind if omit_anonymous_contexts" do
109+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
110+ flag = { key : "flagkey" , version : 11 }
111+ multi = LDContext . create_multi ( [ context , anon_context ] )
112+ ep . record_eval_event ( multi , 'flagkey' , 11 , 1 , 'value' , nil , nil , true )
113+
114+ output = flush_and_get_events ( ep , sender )
115+ expect ( output ) . to contain_exactly (
116+ eq ( index_event ( omit_anonymous_contexts_config , context ) ) ,
117+ eq ( feature_event ( omit_anonymous_contexts_config , flag , multi , 1 , 'value' ) ) ,
118+ include ( :kind => "summary" )
119+ )
120+
121+ summary = output . detect { |e | e [ :kind ] == "summary" }
122+ expect ( summary [ :features ] [ :flagkey ] [ :contextKinds ] ) . to contain_exactly ( "user" , "org" )
123+ end
124+ end
125+
126+ it "handles mult-kind context being completely anonymous if omit_anonymous_contexts" do
127+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
128+ flag = { key : "flagkey" , version : 11 }
129+ anon_user = LDContext . create ( { kind : "user" , key : "userkey" , name : "User name" , anonymous : true } )
130+ multi = LDContext . create_multi ( [ anon_user , anon_context ] )
131+ ep . record_eval_event ( multi , 'flagkey' , 11 , 1 , 'value' , nil , nil , true )
132+
133+ output = flush_and_get_events ( ep , sender )
134+ expect ( output ) . to contain_exactly (
135+ eq ( feature_event ( omit_anonymous_contexts_config , flag , multi , 1 , 'value' ) ) ,
136+ include ( :kind => "summary" )
137+ )
138+
139+ summary = output . detect { |e | e [ :kind ] == "summary" }
140+ expect ( summary [ :features ] [ :flagkey ] [ :contextKinds ] ) . to contain_exactly ( "user" , "org" )
141+ end
142+ end
143+
144+ it "anonymous context does not prevent subsequent index events if omit_anonymous_contexts" do
145+ with_processor_and_sender ( omit_anonymous_contexts_config , starting_timestamp ) do |ep , sender |
146+ flag = { key : "flagkey" , version : 11 }
147+ ep . record_eval_event ( anon_context , 'flagkey' , 11 , 1 , 'value' , nil , nil , false )
148+ non_anon_context = LDContext . create ( { kind : "org" , key : "orgkey" , name : "Organization" , anonymous : false } )
149+ ep . record_eval_event ( non_anon_context , 'flagkey' , 11 , 1 , 'value' , nil , nil , false )
150+
151+ output = flush_and_get_events ( ep , sender )
152+ expect ( output ) . to contain_exactly (
153+ eq ( index_event ( omit_anonymous_contexts_config , non_anon_context , starting_timestamp + 1 ) ) ,
154+ include ( :kind => "summary" )
155+ )
156+
157+ summary = output . detect { |e | e [ :kind ] == "summary" }
158+ expect ( summary [ :features ] [ :flagkey ] [ :contextKinds ] ) . to contain_exactly ( "org" )
159+ end
23160 end
24161 end
25162
@@ -274,7 +411,7 @@ module LaunchDarkly
274411
275412 it "treats nil value for custom the same as an empty hash" do
276413 with_processor_and_sender ( default_config , starting_timestamp ) do |ep , sender |
277- user_with_nil_custom = LDContext . create ( { key : "userkey" , custom : nil } )
414+ user_with_nil_custom = LDContext . create ( { key : "userkey" , kind : "user" , custom : nil } )
278415 ep . record_identify_event ( user_with_nil_custom )
279416
280417 output = flush_and_get_events ( ep , sender )
@@ -721,7 +858,7 @@ def custom_event(context, key, data, metric_value)
721858 def flush_and_get_events ( ep , sender )
722859 ep . flush
723860 ep . wait_until_inactive
724- sender . analytics_payloads . pop
861+ sender . analytics_payloads . pop unless sender . analytics_payloads . empty?
725862 end
726863 end
727864end
0 commit comments