Add gm_ctflow_state
This commit is contained in:
@@ -1,5 +1,30 @@
|
|||||||
-module(gm_ctflow).
|
-module(gm_ctflow).
|
||||||
|
|
||||||
|
-export([ put/2
|
||||||
|
, get/1
|
||||||
|
, get/2
|
||||||
|
, erase/1
|
||||||
|
]).
|
||||||
|
|
||||||
-type flow() :: any().
|
-type flow() :: any().
|
||||||
|
-type key() :: any().
|
||||||
|
-type value() :: any().
|
||||||
|
|
||||||
-export_type([ flow/0 ]).
|
-export_type([ flow/0 ]).
|
||||||
|
|
||||||
|
-spec put(key(), value()) -> 'ok'.
|
||||||
|
put(Key, Value) ->
|
||||||
|
gm_ctflow_state:put(Key, Value).
|
||||||
|
|
||||||
|
-spec get(key()) -> value() | no_return().
|
||||||
|
get(Key) ->
|
||||||
|
gm_ctflow_state:get(Key).
|
||||||
|
|
||||||
|
-spec get(key(), Default) -> value()
|
||||||
|
when Default :: value().
|
||||||
|
get(Key, Default) ->
|
||||||
|
gm_ctflow_state:get(Key, Default).
|
||||||
|
|
||||||
|
-spec erase(key()) -> 'ok'.
|
||||||
|
erase(Key) ->
|
||||||
|
gm_ctflow_state:erase(Key).
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
-export([start/2, stop/1]).
|
-export([start/2, stop/1]).
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
|
gm_ctflow_state:init(),
|
||||||
gm_ctflow_sup:start_link().
|
gm_ctflow_sup:start_link().
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
-module(gm_ctflow_state).
|
||||||
|
|
||||||
|
-export([ init/0
|
||||||
|
, put/2
|
||||||
|
, get/1
|
||||||
|
, get/2
|
||||||
|
, erase/1 ]).
|
||||||
|
|
||||||
|
-type key() :: any().
|
||||||
|
-type value() :: any().
|
||||||
|
|
||||||
|
|
||||||
|
init() ->
|
||||||
|
ets:new(?MODULE, [ordered_set, public, named_table]).
|
||||||
|
|
||||||
|
-spec put(key(), value()) -> 'ok'.
|
||||||
|
put(Key, Value) ->
|
||||||
|
ets:insert(?MODULE, {Key, Value}),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
-spec get(key()) -> value() | no_return().
|
||||||
|
get(Key) ->
|
||||||
|
case ets:lookup(?MODULE, Key) of
|
||||||
|
[] ->
|
||||||
|
error({no_such_key, Key});
|
||||||
|
[{_, Value}] ->
|
||||||
|
Value
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec get(key(), Default) -> value()
|
||||||
|
when Default :: value().
|
||||||
|
get(Key, Default) ->
|
||||||
|
case ets:lookup(?MODULE, Key) of
|
||||||
|
[{_, Value}] ->
|
||||||
|
Value;
|
||||||
|
[] ->
|
||||||
|
Default
|
||||||
|
end.
|
||||||
|
|
||||||
|
erase(Key) ->
|
||||||
|
ets:delete(?MODULE, Key),
|
||||||
|
ok.
|
||||||
@@ -11,7 +11,10 @@
|
|||||||
]).
|
]).
|
||||||
|
|
||||||
%% test cases
|
%% test cases
|
||||||
-export([ init_counter/1
|
-export([ put_a/1
|
||||||
|
, get_a/1
|
||||||
|
, erase_a/1
|
||||||
|
, init_counter/1
|
||||||
, incr/1
|
, incr/1
|
||||||
, check1/1
|
, check1/1
|
||||||
, check2/1
|
, check2/1
|
||||||
@@ -27,12 +30,17 @@
|
|||||||
|
|
||||||
all() ->
|
all() ->
|
||||||
[
|
[
|
||||||
{group, statefuns}
|
{group, state}
|
||||||
|
, {group, statefuns}
|
||||||
, {group, workers}
|
, {group, workers}
|
||||||
].
|
].
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
[ {statefuns, [sequence], [ init_counter
|
[ {state, [sequence], [ put_a
|
||||||
|
, get_a
|
||||||
|
, erase_a
|
||||||
|
]}
|
||||||
|
, {statefuns, [sequence], [ init_counter
|
||||||
, incr
|
, incr
|
||||||
, check1 %% state = 1
|
, check1 %% state = 1
|
||||||
, incr
|
, incr
|
||||||
@@ -71,6 +79,21 @@ init_per_testcase(_Case, Config) ->
|
|||||||
end_per_testcase(_Case, _Config) ->
|
end_per_testcase(_Case, _Config) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
%% ======================================================================
|
||||||
|
%% State update commands
|
||||||
|
|
||||||
|
put_a(_Cfg) ->
|
||||||
|
ok = gm_ctflow:put(a, 1),
|
||||||
|
?assertMatch(1, gm_ctflow:get(a)).
|
||||||
|
|
||||||
|
get_a(_Cfg) ->
|
||||||
|
?assertMatch(1, gm_ctflow:get(a)).
|
||||||
|
|
||||||
|
erase_a(_Cfg) ->
|
||||||
|
ok = gm_ctflow:erase(a),
|
||||||
|
?assertException(error, {no_such_key,a}, gm_ctflow:get(a)),
|
||||||
|
?assertMatch(undefined, gm_ctflow:get(a, undefined)).
|
||||||
|
|
||||||
%% ======================================================================
|
%% ======================================================================
|
||||||
%% This is our 'ctflow_fun' instance, which floats above some simple
|
%% This is our 'ctflow_fun' instance, which floats above some simple
|
||||||
%% test cases. In this test suite, we only exercise the flow funs, but
|
%% test cases. In this test suite, we only exercise the flow funs, but
|
||||||
|
|||||||
Reference in New Issue
Block a user