Skip to content

Commit

Permalink
Merge pull request #183 from esl/coordinator/arbitrary_names
Browse files Browse the repository at this point in the history
Allow any term to be a coordinator name
  • Loading branch information
DenysGonchar authored May 15, 2024
2 parents 0d47028 + a89e5c1 commit 887e312
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/coordinator/amoc_coordinator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-define(IS_N_OF_USERS(N), (?IS_POS_INT(N) orelse N =:= all)).
-define(IS_TIMEOUT(Timeout), (?IS_POS_INT(Timeout) orelse Timeout =:= infinity)).

-type name() :: atom().
-type name() :: term().

-type data() :: {pid(), Data :: any()}.

Expand Down Expand Up @@ -102,11 +102,11 @@ reset(Name) ->
notify(Name, reset_coordinator).

-spec notify(name(), coordinator_timeout | reset_coordinator | {coordinate, {pid(), term()}}) -> ok.
notify(Name, coordinator_timeout) when is_atom(Name) ->
notify(Name, coordinator_timeout) ->
do_notify(Name, coordinator_timeout);
notify(Name, reset_coordinator) when is_atom(Name) ->
notify(Name, reset_coordinator) ->
do_notify(Name, reset_coordinator);
notify(Name, {coordinate, _} = Event) when is_atom(Name) ->
notify(Name, {coordinate, _} = Event) ->
do_notify(Name, Event).

do_notify(Name, Event) ->
Expand Down
1 change: 0 additions & 1 deletion src/coordinator/amoc_coordinator_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ start_link() ->

-spec init(term()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init([]) ->
ets:new(?MODULE, [named_table, ordered_set, public, {read_concurrency, true}]),
AChild = #{id => amoc_coordinator_worker_sup,
start => {amoc_coordinator_worker_sup, start_link, []},
restart => transient,
Expand Down
12 changes: 12 additions & 0 deletions test/amoc_coordinator_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all() ->
plan_normalises_successfully,
ordering_plan_sets_all_at_the_end,
failing_action_does_not_kill_the_worker,
coordinator_name_can_be_dynamic_terms,
execute_with_range_without_timeout,
execute_plan_without_timeout,
reset_plan_without_timeout,
Expand Down Expand Up @@ -220,6 +221,17 @@ execute_plan_with_timeout(_Config) ->
assert_telemetry_events(Name, [start, {N1, add}, timeout,
{N2, add}, timeout, stop]).

coordinator_name_can_be_dynamic_terms(_) ->
BinName = <<(atom_to_binary(?FUNCTION_NAME))/binary, (crypto:strong_rand_bytes(8))/binary>>,
SomePlan = [{1000, fun(_Event) -> ok end}],
Names = [{some_composed_tuple},
[list, made_of, atoms],
base64:encode(BinName)],
[ ?assertEqual(ok, amoc_coordinator:start(Name, SomePlan)) || Name <- Names ],
[ {ok, _, _Workers} = amoc_coordinator_sup:get_workers(Name) || Name <- Names ],
[ [amoc_coordinator:add(Name, User) || User <- lists:seq(1, 10)] || Name <- Names ],
[ amoc_coordinator:stop(Name) || Name <- Names ].

failing_action_does_not_kill_the_worker(_) ->
Name = ?FUNCTION_NAME,
Plan = {2, [mock_failing()]},
Expand Down

0 comments on commit 887e312

Please sign in to comment.