Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit 8809296

Browse files
Increase sample count to pass Chi squared test in more environments, reorganise tests
We still depend on the PRNG to provide a reasonably uniform distribution of inputs (e.g. routing keys) but things pass in at least 3 different environments reliably with 150K iterations. Pair: @dcorbacho. References #37, #38. (cherry picked from commit e081baa)
1 parent 72b3665 commit 8809296

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

test/rabbit_exchange_type_consistent_hash_SUITE.erl

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ all() ->
3030
groups() ->
3131
[
3232
{non_parallel_tests, [], [
33-
routing_test,
33+
routing_key_hashing_test,
34+
custom_header_hashing_test,
35+
message_id_hashing_test,
36+
correlation_id_hashing_test,
37+
timestamp_hashing_test,
38+
other_routing_test,
3439
test_binding_queue_cleanup,
3540
test_binding_exchange_cleanup,
3641
test_bucket_sizes
@@ -72,22 +77,34 @@ end_per_testcase(Testcase, Config) ->
7277
%% Test cases
7378
%% -------------------------------------------------------------------
7479

75-
routing_test(Config) ->
76-
routing_test0(Config, [<<"q0">>, <<"q1">>, <<"q2">>, <<"q3">>]),
77-
ok.
80+
-define(Qs, [<<"q0">>, <<"q1">>, <<"q2">>, <<"q3">>]).
81+
%% N.B. lowering this value below 100K increases the probability
82+
%% of failing the Chi squared test in some environments
83+
-define(DEFAULT_SAMPLE_COUNT, 150000).
84+
85+
routing_key_hashing_test(Config) ->
86+
ok = test_with_rk(Config, ?Qs).
87+
88+
custom_header_hashing_test(Config) ->
89+
ok = test_with_header(Config, ?Qs).
90+
91+
message_id_hashing_test(Config) ->
92+
ok = test_with_message_id(Config, ?Qs).
93+
94+
correlation_id_hashing_test(Config) ->
95+
ok = test_with_correlation_id(Config, ?Qs).
7896

79-
routing_test0(Config, Qs) ->
80-
ok = test_with_rk(Config, Qs),
81-
ok = test_with_header(Config, Qs),
97+
timestamp_hashing_test(Config) ->
98+
ok = test_with_timestamp(Config, ?Qs).
99+
100+
other_routing_test(Config) ->
82101
ok = test_binding_with_negative_routing_key(Config),
83102
ok = test_binding_with_non_numeric_routing_key(Config),
84-
ok = test_with_correlation_id(Config, Qs),
85-
ok = test_with_message_id(Config, Qs),
86-
ok = test_with_timestamp(Config, Qs),
87103
ok = test_non_supported_property(Config),
88104
ok = test_mutually_exclusive_arguments(Config),
89105
ok.
90106

107+
91108
%% -------------------------------------------------------------------
92109
%% Implementation
93110
%% -------------------------------------------------------------------
@@ -109,7 +126,6 @@ test_with_header(Config, Qs) ->
109126
#amqp_msg{props = #'P_basic'{headers = H}, payload = <<>>}
110127
end, [{<<"hash-header">>, longstr, <<"hashme">>}], Qs).
111128

112-
113129
test_with_correlation_id(Config, Qs) ->
114130
test0(Config, fun(E) ->
115131
#'basic.publish'{exchange = E}
@@ -131,7 +147,7 @@ test_with_timestamp(Config, Qs) ->
131147
#'basic.publish'{exchange = E}
132148
end,
133149
fun() ->
134-
#amqp_msg{props = #'P_basic'{timestamp = rndint()}, payload = <<>>}
150+
#amqp_msg{props = #'P_basic'{timestamp = rnd_int()}, payload = <<>>}
135151
end, [{<<"hash-property">>, longstr, <<"timestamp">>}], Qs).
136152

137153
test_mutually_exclusive_arguments(Config) ->
@@ -164,13 +180,15 @@ test_non_supported_property(Config) ->
164180
ok.
165181

166182
rnd() ->
167-
list_to_binary(integer_to_list(rndint())).
183+
list_to_binary(integer_to_list(rnd_int())).
168184

169-
rndint() ->
170-
rand:uniform(1000000).
185+
rnd_int() ->
186+
rand:uniform(10000000).
171187

172188
test0(Config, MakeMethod, MakeMsg, DeclareArgs, [Q1, Q2, Q3, Q4] = Queues) ->
173-
Count = 100000,
189+
test0(Config, MakeMethod, MakeMsg, DeclareArgs, [Q1, Q2, Q3, Q4] = Queues, ?DEFAULT_SAMPLE_COUNT).
190+
191+
test0(Config, MakeMethod, MakeMsg, DeclareArgs, [Q1, Q2, Q3, Q4] = Queues, IterationCount) ->
174192
Chan = rabbit_ct_client_helpers:open_channel(Config, 0),
175193
#'confirm.select_ok'{} = amqp_channel:call(Chan, #'confirm.select'{}),
176194

@@ -202,7 +220,7 @@ test0(Config, MakeMethod, MakeMsg, DeclareArgs, [Q1, Q2, Q3, Q4] = Queues) ->
202220

203221
[amqp_channel:call(Chan,
204222
MakeMethod(CHX),
205-
MakeMsg()) || _ <- lists:duplicate(Count, const)],
223+
MakeMsg()) || _ <- lists:duplicate(IterationCount, const)],
206224
amqp_channel:wait_for_confirms(Chan, 5000),
207225
timer:sleep(500),
208226

@@ -213,15 +231,17 @@ test0(Config, MakeMethod, MakeMsg, DeclareArgs, [Q1, Q2, Q3, Q4] = Queues) ->
213231
exclusive = true}),
214232
M
215233
end || Q <- Queues],
216-
Count = lists:sum(Counts), %% All messages got routed
234+
IterationCount = lists:sum(Counts), %% All messages got routed
217235

218236
%% Chi-square test
219237
%% H0: routing keys are not evenly distributed according to weight
220-
Expected = [Count div 6, Count div 6, (Count div 6) * 2, (Count div 6) * 2],
238+
Expected = [IterationCount div 6, IterationCount div 6, (IterationCount div 6) * 2, (IterationCount div 6) * 2],
221239
Obs = lists:zip(Counts, Expected),
222240
Chi = lists:sum([((O - E) * (O - E)) / E || {O, E} <- Obs]),
223-
ct:pal("Chi-square test for 3 degrees of freedom is ~p, p = 0.01 is 11.35",
224-
[Chi]),
241+
ct:pal("Chi-square test for 3 degrees of freedom is ~p, p = 0.01 is 11.35, observations (counts, expected): ~p",
242+
[Chi, Obs]),
243+
%% N.B. we need at least 100K iterations to reliably pass this test in multiple different
244+
%% environments
225245
?assert(Chi < 11.35),
226246

227247
clean_up_test_topology(Config, CHX, Queues),
@@ -433,8 +453,7 @@ count_all_queue_buckets(Config) ->
433453
[rabbit_exchange_type_consistent_hash_bucket_queue, size]).
434454

435455
clean_up_test_topology(Config) ->
436-
Qs = [<<"q0">>, <<"q1">>, <<"q2">>, <<"q3">>],
437-
clean_up_test_topology(Config, <<"e">>, Qs).
456+
clean_up_test_topology(Config, <<"e">>, ?Qs).
438457

439458
clean_up_test_topology(Config, X, Qs) ->
440459
Ch = rabbit_ct_client_helpers:open_channel(Config, 0),

0 commit comments

Comments
 (0)