Skip to content

Commit 286312e

Browse files
authored
Merge pull request #529 from rabbitmq/add-ra-aux_command-3
ra: Add `aux_command/3`
2 parents 6a07bdd + a743ce0 commit 286312e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/ra.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
transfer_leadership/2,
7878
%% auxiliary commands
7979
aux_command/2,
80+
aux_command/3,
8081
cast_aux_command/2,
8182
register_external_log_reader/1,
8283
member_overview/1,
@@ -1172,6 +1173,15 @@ transfer_leadership(ServerId, TargetServerId) ->
11721173
aux_command(ServerRef, Cmd) ->
11731174
gen_statem:call(ServerRef, {aux_command, Cmd}).
11741175

1176+
%% @doc Executes (using a call) an auxiliary command that the state machine can handle.
1177+
%%
1178+
%% @param ServerId the Ra server(s) to send the query to
1179+
%% @param Command an arbitrary term that the state machine can handle
1180+
%% @end
1181+
-spec aux_command(ra_server_id(), term(), timeout()) -> term().
1182+
aux_command(ServerRef, Cmd, Timeout) ->
1183+
gen_statem:call(ServerRef, {aux_command, Cmd}, Timeout).
1184+
11751185
%% @doc Executes (using a cast) an auxiliary command that the state machine can handle.
11761186
%%
11771187
%% @param ServerId the Ra server(s) to send the query to

test/ra_machine_int_SUITE.erl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ all_tests() ->
5151
aux_command,
5252
aux_command_v2,
5353
aux_command_v1_and_v2,
54+
aux_command_timeout,
5455
aux_monitor_effect,
5556
aux_and_machine_monitor_same_process,
5657
aux_and_machine_monitor_same_node,
@@ -810,6 +811,37 @@ aux_command_v1_and_v2(Config) ->
810811
ra:delete_cluster(Cluster),
811812
ok.
812813

814+
aux_command_timeout(Config) ->
815+
ClusterName = ?config(cluster_name, Config),
816+
ServerId1 = ?config(server_id, Config),
817+
Cluster = [ServerId1,
818+
?config(server_id2, Config),
819+
?config(server_id3, Config)],
820+
Mod = ?config(modname, Config),
821+
meck:new(Mod, [non_strict]),
822+
meck:expect(Mod, init, fun (_) -> [] end),
823+
meck:expect(Mod, init_aux, fun (_) -> undefined end),
824+
meck:expect(Mod, apply,
825+
fun
826+
(_, Cmd, State) ->
827+
ct:pal("handling ~p", [Cmd]),
828+
%% handle all
829+
{State, ok}
830+
end),
831+
meck:expect(Mod, handle_aux,
832+
fun
833+
(_RaftState, {call, _From}, {sleep, Sleep}, AuxState, Opaque) ->
834+
timer:sleep(Sleep),
835+
{reply, ok, AuxState, Opaque};
836+
(_RaftState, cast, _Msg, AuxState, Opaque) ->
837+
{no_reply, AuxState, Opaque}
838+
end),
839+
ok = start_cluster(ClusterName, {module, Mod, #{}}, Cluster),
840+
?assertEqual(ok, ra:aux_command(ServerId1, {sleep, 100}, 500)),
841+
?assertExit({timeout, _}, ra:aux_command(ServerId1, {sleep, 1000}, 500)),
842+
ra:delete_cluster(Cluster),
843+
ok.
844+
813845
aux_eval(Config) ->
814846
%% aux handle is automatically passed an eval command after new entries
815847
%% have been applied

0 commit comments

Comments
 (0)