Skip to content

Commit b65930d

Browse files
committed
Test epocxy_ets_fsm:change_owner/2
1 parent 99df24b commit b65930d

2 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/epocxy_ets_fsm.erl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
-type ets_concurrency() :: none | read_only | write_only | read_and_write.
5454
-export_type([ets_concurrency/0]).
5555

56-
-spec start_link() -> {ok, pid()}.
57-
-spec create_ets_table(ets_concurrency()) -> ets:tid().
58-
-spec create_ets_table(atom(), ets_concurrency()) -> ets:tid().
59-
-spec delete_ets_table(atom() | ets:tid()) -> ok.
60-
-spec change_owner(atom() | ets:tid(), pid()) -> ok.
56+
-spec start_link () -> {ok, pid()}.
57+
-spec create_ets_table (ets_concurrency()) -> ets:tid().
58+
-spec create_ets_table (atom(), ets_concurrency()) -> ets:tid().
59+
-spec delete_ets_table (atom() | ets:tid()) -> ok.
60+
-spec change_owner (atom() | ets:tid(), pid()) -> ok.
6161

6262
start_link() ->
6363
gen_fsm:start_link({local, ?SERVER}, ?MODULE, {}, []).
@@ -76,7 +76,8 @@ delete_ets_table(Table_Id_Or_Name) ->
7676
ok.
7777

7878
change_owner(Table_Id_Or_Name, New_Owner) ->
79-
gen_fsm:sync_send_event(?SERVER, {change_owner, Table_Id_Or_Name, New_Owner}).
79+
true = gen_fsm:sync_send_event(?SERVER, {change_owner, Table_Id_Or_Name, New_Owner}),
80+
ok.
8081

8182
make_options(none) -> make_base_options([]);
8283
make_options(read_only) -> make_base_options([{read_concurrency, true}]);
@@ -92,13 +93,14 @@ make_base_options(Concurrency_Options) ->
9293
%%% gen_fsm callbacks
9394
%%%===================================================================
9495

95-
-type internal_state() :: #eef_state{}.
96-
-type state_name() :: 'READY'.
97-
-type create_ets_cmd() :: {create_ets_table, proplists:proplist()}
98-
| {create_ets_table, Name::atom(), proplists:proplist()}.
99-
-type delete_ets_cmd() :: {delete_ets_table, ets:tid() | atom()}.
100-
-type stop_cmd() :: stop.
101-
%% -type ready_cmds() :: create_ets_cmd() | delete_ets_cmd() | stop_cmd().
96+
-type internal_state() :: #eef_state{}.
97+
-type state_name() :: 'READY'.
98+
-type create_ets_cmd() :: {create_ets_table, proplists:proplist()}
99+
| {create_ets_table, Name::atom(), proplists:proplist()}.
100+
-type delete_ets_cmd() :: {delete_ets_table, ets:tid() | atom()}.
101+
-type change_owner_cmd() :: {change_owner, ets:tid() | atom(), pid()}.
102+
-type stop_cmd() :: stop.
103+
%% -type ready_cmds() :: create_ets_cmd() | delete_ets_cmd() | change_owner_cmd() | stop_cmd().
102104

103105
-spec init({}) -> {ok, 'READY', internal_state()}.
104106
-spec terminate (any(), state_name(), internal_state()) -> ok.
@@ -112,9 +114,10 @@ code_change (_OldVsn, State_Name, #eef_state{} = State, _Extra) -> {ok, State_N
112114

113115
%% The FSM has only the 'READY' state.
114116
-type from() :: {pid(), reference()}.
115-
-spec 'READY'(create_ets_cmd(), from(), internal_state()) -> ets:tid();
116-
(delete_ets_cmd(), from(), internal_state()) -> true;
117-
(stop_cmd(), from(), internal_state()) -> {stop, normal}.
117+
-spec 'READY'(create_ets_cmd(), from(), internal_state()) -> ets:tid();
118+
(delete_ets_cmd(), from(), internal_state()) -> true;
119+
(change_owner_cmd(), from(), internal_state()) -> true;
120+
(stop_cmd(), from(), internal_state()) -> {stop, normal}.
118121

119122
-define(READY(__Cmd), 'READY'(__Cmd, _From, #eef_state{} = State)).
120123
-define(REPLY(__Reply), {reply, __Reply, 'READY', State}).

test/epocxy/epocxy_ets_fsm_SUITE.erl

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
-auth('jay@duomark.com').
1515
-vsn('').
1616

17-
-export([all/0, init_per_suite/1, end_per_suite/1]).
18-
-export([check_create/1]).
17+
-export([
18+
all/0,
19+
init_per_suite/1, end_per_suite/1
20+
]).
21+
22+
-export([check_create/1, check_owner/1]).
1923

2024
-include_lib("common_test/include/ct.hrl").
2125

2226
-spec all() -> [atom()].
2327

24-
all() -> [check_create].
28+
all() -> [check_create, check_owner].
2529

2630
-type config() :: proplists:proplist().
2731
-spec init_per_suite (config()) -> config().
@@ -33,15 +37,15 @@ end_per_suite (Config) -> Config.
3337
%% Test Module is ?TM
3438
-define(TM, epocxy_ets_fsm).
3539

40+
3641
%%%------------------------------------------------------------------------------
3742
%%% Unit tests for cxy_cache core
3843
%%%------------------------------------------------------------------------------
3944

40-
41-
%% Validate any atom can be used as a cache_name and info/1 will report properly.
45+
%% Validate ets tables are created with the proper attributes, and can be deleted.
4246
-spec check_create(config()) -> ok.
4347
check_create(_Config) ->
44-
epocxy_sup:start_link(),
48+
{ok, Sup_Pid} = epocxy_sup:start_link(),
4549

4650
ct:log("Create and delete unnamed ets tables"),
4751
Fsm_Pid = whereis(?TM),
@@ -55,14 +59,9 @@ check_create(_Config) ->
5559
{t3, write_only}, {t4, read_and_write}]],
5660

5761
ct:comment("Successfully tested creating and deleting ets tables"),
58-
cleanup(whereis(epocxy_sup), Fsm_Pid),
62+
cleanup(Sup_Pid, Fsm_Pid),
5963
ok.
6064

61-
cleanup(Pid, Fsm_Pid) ->
62-
supervisor:terminate_child(Pid, Fsm_Pid),
63-
unlink(Pid),
64-
exit(Pid, kill).
65-
6665
validate_create_table(Fsm_Pid, Cxy_Type, Named) ->
6766
Tid = case Named of
6867
no_name -> ?TM:create_ets_table(Cxy_Type);
@@ -76,3 +75,22 @@ validate_create_table(Fsm_Pid, Cxy_Type, Named) ->
7675
end,
7776
ok = ?TM:delete_ets_table(Tid),
7877
ok.
78+
79+
%% Validate the owner of an ets table can be changed.
80+
check_owner(_Config) ->
81+
{ok, Sup_Pid} = epocxy_sup:start_link(),
82+
Fsm_Pid = whereis(?TM),
83+
84+
T1 = ?TM:create_ets_table(write_only),
85+
Fsm_Pid = ets:info(T1, owner),
86+
Self = self(),
87+
ok = ?TM:change_owner(T1, Self),
88+
Self = ets:info(T1, owner),
89+
90+
cleanup(Sup_Pid, Fsm_Pid),
91+
ok.
92+
93+
cleanup(Pid, Fsm_Pid) ->
94+
supervisor:terminate_child(Pid, Fsm_Pid),
95+
unlink(Pid),
96+
exit(Pid, kill).

0 commit comments

Comments
 (0)