Skip to content

Commit 3435a0d

Browse files
committed
Fix a flake in amqp_client_SUITE
`event_recorder` listens for events globally rather than per-connection so it's possible for `connection_closed` and other events from prior test cases to be captured and mixed with the events the test is interested in, causing a flake. Instead we can assert that the events we gather from `event_recorder` contain the ones emitted by the connection.
1 parent 1c4af0c commit 3435a0d

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

deps/rabbit/test/amqp_client_SUITE.erl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,27 +1226,29 @@ events(Config) ->
12261226
end,
12271227
ok = close_connection_sync(Connection),
12281228

1229-
[E0, E1, E2] = event_recorder:get_events(Config),
1229+
Events = event_recorder:get_events(Config),
12301230
ok = event_recorder:stop(Config),
1231+
ct:pal("Recorded events: ~p", [Events]),
12311232

1232-
assert_event_type(user_authentication_success, E0),
12331233
Protocol = {protocol, {1, 0}},
1234-
assert_event_prop([{name, <<"guest">>},
1234+
AuthProps = [{name, <<"guest">>},
12351235
{auth_mechanism, <<"PLAIN">>},
12361236
{ssl, false},
12371237
Protocol],
1238-
E0),
1238+
?assertMatch(
1239+
{value, _},
1240+
find_event(user_authentication_success, AuthProps, Events)),
12391241

1240-
assert_event_type(connection_created, E1),
12411242
Node = get_node_config(Config, 0, nodename),
1242-
assert_event_prop(
1243-
[Protocol,
1244-
{node, Node},
1245-
{vhost, <<"/">>},
1246-
{user, <<"guest">>},
1247-
{type, network}],
1248-
E1),
1249-
Props = E1#event.props,
1243+
ConnectionCreatedProps = [Protocol,
1244+
{node, Node},
1245+
{vhost, <<"/">>},
1246+
{user, <<"guest">>},
1247+
{type, network}],
1248+
{value, ConnectionCreatedEvent} = find_event(
1249+
connection_created,
1250+
ConnectionCreatedProps, Events),
1251+
Props = ConnectionCreatedEvent#event.props,
12501252
Name = proplists:lookup(name, Props),
12511253
Pid = proplists:lookup(pid, Props),
12521254
ClientProperties = {client_properties, List} = proplists:lookup(client_properties, Props),
@@ -1257,13 +1259,14 @@ events(Config) ->
12571259
{<<"ignore-maintenance">>, bool, true},
12581260
List)),
12591261

1260-
assert_event_type(connection_closed, E2),
1261-
assert_event_prop(
1262-
[{node, Node},
1263-
Name,
1264-
Pid,
1265-
ClientProperties],
1266-
E2).
1262+
ConnectionClosedProps = [{node, Node},
1263+
Name,
1264+
Pid,
1265+
ClientProperties],
1266+
?assertMatch(
1267+
{value, _},
1268+
find_event(connection_closed, ConnectionClosedProps, Events)),
1269+
ok.
12671270

12681271
sync_get_unsettled_classic_queue(Config) ->
12691272
sync_get_unsettled(<<"classic">>, Config).
@@ -4193,3 +4196,19 @@ has_local_member(QName) ->
41934196
{error, _} ->
41944197
false
41954198
end.
4199+
4200+
-spec find_event(Type, Props, Events) -> Ret when
4201+
Type :: atom(),
4202+
Props :: proplists:proplist(),
4203+
Events :: [#event{}],
4204+
Ret :: {value, #event{}} | false.
4205+
4206+
find_event(Type, Props, Events) when is_list(Props), is_list(Events) ->
4207+
lists:search(
4208+
fun(#event{type = EventType, props = EventProps}) ->
4209+
Type =:= EventType andalso
4210+
lists:all(
4211+
fun({Key, _Value}) ->
4212+
lists:keymember(Key, 1, EventProps)
4213+
end, Props)
4214+
end, Events).

0 commit comments

Comments
 (0)