Skip to content

Commit 66823f9

Browse files
committed
Add start_link interface
1 parent 0f69ec9 commit 66823f9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/cets.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
-behaviour(gen_server).
2929

3030
-export([
31+
start_link/2,
3132
start/2,
3233
stop/1,
3334
insert/2,
@@ -70,6 +71,7 @@
7071
]).
7172

7273
-ignore_xref([
74+
start_link/2,
7375
start/2,
7476
stop/1,
7577
insert/2,
@@ -275,6 +277,15 @@
275277
%% We recommend to define that function if keys could have conflicts.
276278
%% This function would be called once for each conflicting key.
277279
%% We recommend to keep that function pure (or at least no blocking calls from it).
280+
-spec start_link(table_name(), start_opts()) -> gen_server:start_ret().
281+
start_link(Tab, Opts) when is_atom(Tab) ->
282+
case check_opts(Opts) of
283+
[] ->
284+
gen_server:start_link({local, Tab}, ?MODULE, {Tab, Opts}, []);
285+
Errors ->
286+
{error, Errors}
287+
end.
288+
278289
-spec start(table_name(), start_opts()) -> gen_server:start_ret().
279290
start(Tab, Opts) when is_atom(Tab) ->
280291
case check_opts(Opts) of

test/cets_SUITE.erl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ groups() ->
3939

4040
cases() ->
4141
[
42+
start_link_inits_and_accepts_records,
4243
inserted_records_could_be_read_back,
4344
insert_many_with_one_record,
4445
insert_many_with_two_records,
@@ -52,6 +53,7 @@ cases() ->
5253
join_works_with_existing_data_with_conflicts_and_defined_conflict_handler_and_more_keys,
5354
join_works_with_existing_data_with_conflicts_and_defined_conflict_handler_and_keypos2,
5455
bag_with_conflict_handler_not_allowed,
56+
bag_with_conflict_handler_not_allowed_for_start_link,
5557
insert_new_works,
5658
insert_new_works_with_table_name,
5759
insert_new_works_when_leader_is_back,
@@ -274,6 +276,12 @@ end_per_testcase(_, _Config) ->
274276
log_modules() ->
275277
[cets, cets_call, cets_long, cets_join, cets_discovery].
276278

279+
start_link_inits_and_accepts_records(Config) ->
280+
Tab = make_name(Config),
281+
start_link_local(Tab),
282+
cets:insert(Tab, {alice, 32}),
283+
[{alice, 32}] = ets:lookup(Tab, alice).
284+
277285
inserted_records_could_be_read_back(Config) ->
278286
Tab = make_name(Config),
279287
start_local(Tab),
@@ -747,6 +755,10 @@ bag_with_conflict_handler_not_allowed(Config) ->
747755
{error, [bag_with_conflict_handler]} =
748756
cets:start(make_name(Config), #{handle_conflict => fun resolve_highest/2, type => bag}).
749757

758+
bag_with_conflict_handler_not_allowed_for_start_link(Config) ->
759+
{error, [bag_with_conflict_handler]} =
760+
cets:start_link(make_name(Config), #{handle_conflict => fun resolve_highest/2, type => bag}).
761+
750762
join_with_the_same_pid(Config) ->
751763
Tab = make_name(Config),
752764
{ok, Pid} = start_local(Tab),
@@ -2917,6 +2929,16 @@ still_works(Pid) ->
29172929
ok = cets:insert(Pid, {1}),
29182930
{ok, [{1}]} = cets:remote_dump(Pid).
29192931

2932+
start_link_local(Name) ->
2933+
start_link_local(Name, #{}).
2934+
2935+
start_link_local(Name, Opts) ->
2936+
catch cets:stop(Name),
2937+
wait_for_name_to_be_free(node(), Name),
2938+
{ok, Pid} = cets:start(Name, Opts),
2939+
schedule_cleanup(Pid),
2940+
{ok, Pid}.
2941+
29202942
start_local(Name) ->
29212943
start_local(Name, #{}).
29222944

0 commit comments

Comments
 (0)