Skip to content

Commit

Permalink
Merge pull request rkallos#20 from rkallos/feature/add-reuse-sandbox
Browse files Browse the repository at this point in the history
Add wrek_vert:reuse_sandbox/2 and test
  • Loading branch information
rkallos committed Jan 14, 2019
2 parents f1898b1 + 2367bd5 commit 6eca7cf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/wrek.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, wrek,
[{description, "An OTP application"},
{vsn, "2.0.2"},
{vsn, "3.1.0"},
{registered, [wrek_sup]},
{mod, {wrek_app, []}},
{applications,
Expand Down
4 changes: 2 additions & 2 deletions src/wrek.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
-type state() :: #state{}.


-spec put_sandbox(pid(), file:filename_all()) -> ok.
-spec put_sandbox(pid(), file:filename_all()) -> {ok, wrek_vert_t:t()}.

put_sandbox(Pid, Dir) ->
gen_server:call(Pid, {put_sandbox, Dir}).
Expand Down Expand Up @@ -87,7 +87,7 @@ handle_call({put_sandbox, Dir}, {From, _}, State = #state{wrek = Wrek}) ->
{ok, {Name, Vert}} = wrek_t:child(Wrek, From),
Vert2 = wrek_vert_t:set_dir(Vert, Dir),
Wrek2 = wrek_t:add_vertex(Wrek, Name, Vert2),
{reply, ok, State#state{wrek = Wrek2}};
{reply, {ok, Vert2}, State#state{wrek = Wrek2}};

handle_call(sandbox, _From, State) ->
{reply, State#state.sandbox, State};
Expand Down
35 changes: 30 additions & 5 deletions src/wrek_vert.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_all/1,
get_sandbox/2,
make_sandbox/1,
reuse_sandbox/2,
notify/2
]).

Expand Down Expand Up @@ -85,6 +86,12 @@ make_sandbox(Pid) ->
gen_server:call(Pid, make_sandbox).


-spec reuse_sandbox(pid(), any()) -> {ok, file:filename_all()} | {error, term()}.

reuse_sandbox(Pid, Who) ->
gen_server:call(Pid, {reuse_sandbox, Who}).


-spec notify(pid(), any()) -> ok.

notify(Pid, Msg) ->
Expand Down Expand Up @@ -131,12 +138,12 @@ handle_call({get, Who0, Key, Default}, _From, State) ->
{reply, Default, State}
end;

handle_call(get_all, _From, #state{data = Data0}) ->
handle_call(get_all, _From, State = #state{data = Data0}) ->
Data = maps:fold(fun(Name, Vert, Acc) ->
D = wrek_vert_t:kv(Vert),
Acc#{Name => D}
end, #{}, Data0),
{reply, ok, Data};
{reply, Data, State};

handle_call({get_sandbox, Who0}, _From, State) ->
#state{data = Data} = State,
Expand All @@ -152,13 +159,31 @@ handle_call({get_sandbox, Who0}, _From, State) ->
{reply, undefined, State}
end;

handle_call(make_sandbox, _From, State = #state{parent = Parent}) ->
handle_call(make_sandbox, _From, State = #state{data = Data, parent = Parent}) ->
{_DagId, VertId} = id(State),
DagDir = dag_dir(Parent),
VertStr = integer_to_list(VertId),
Dir = wrek_utils:sandbox(DagDir, VertStr),
wrek:put_sandbox(Parent, Dir),
{reply, Dir, State};
{ok, Vert2} = wrek:put_sandbox(Parent, Dir),
Data2 = Data#{name(State) => Vert2},
{reply, Dir, State#state{data = Data2, vert = Vert2}};

handle_call({reuse_sandbox, Who}, _From, State = #state{parent = Parent}) ->
#state{data = Data} = State,
{Reply, State2} = case Data of
#{Who := V} ->
case wrek_vert_t:dir(V) of
undefined ->
{{error, {no_sandbox, Who}}, State};
Dir ->
{ok, Vert2} = wrek:put_sandbox(Parent, Dir),
Data2 = Data#{name(State) => Vert2},
{{ok, Dir}, State#state{data = Data2, vert = Vert2}}
end;
_ ->
{{error, {no_vert, Who, Data}}, State}
end,
{reply, Reply, State2};

handle_call(_Req, _From, State) ->
{reply, ok, State}.
Expand Down
9 changes: 9 additions & 0 deletions src/wrek_vert_t.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
has_succeeded/1,
is_finished/1,
succeed/2,
to_list/1,
% getters
args/1,
deps/1,
Expand Down Expand Up @@ -149,6 +150,14 @@ succeed(Vert = #?T{kv = Kv}, Result) ->
set_status(Vert2, done).


-spec to_list(t()) -> [{atom(), any()}].

to_list(T = #?T{}) ->
Fields = record_info(fields, ?T),
[_Tag | Values] = tuple_to_list(T),
lists:zip(Fields, Values).


-spec args(t()) -> args_t().

args(#?T{args = Args}) ->
Expand Down
10 changes: 10 additions & 0 deletions test/verts/wrek_reuse_sandbox_vert.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-module(wrek_reuse_sandbox_vert).

-behaviour(wrek_vert).

-export([run/2]).

run([Who], Pid) ->
{ok, Dir} = wrek_vert:reuse_sandbox(Pid, Who),
wrek_vert:notify(Pid, {dir, Dir}),
{ok, #{dir => Dir}}.
8 changes: 4 additions & 4 deletions test/wrek_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ sandbox_test() ->
VertMap = #{
1 => #{module => wrek_make_sandbox_vert, args => [], deps => []},
2 => #{module => wrek_get_sandbox_vert, args => [1], deps => [1]},
3 => #{module => wrek_get_sandbox_vert, args => [1], deps => [2]}
3 => #{module => wrek_reuse_sandbox_vert, args => [1], deps => [1]},
4 => #{module => wrek_get_sandbox_vert, args => [3], deps => [3]}
},

{ok, EvMgr} = gen_event:start_link({local, wrek_test_manager}),
Expand All @@ -330,9 +331,8 @@ sandbox_test() ->
Acc
end, [], Msgs),

?assertEqual(3, length(Notifs)),
?assertEqual(lists:nth(1, Notifs), lists:nth(2, Notifs)),
?assertEqual(lists:nth(2, Notifs), lists:nth(3, Notifs)).
?assertEqual(4, length(Notifs)),
?assertEqual(lists:usort(Notifs), [lists:nth(1, Notifs)]).

custom_exec_callback_test() ->
Self = self(),
Expand Down

0 comments on commit 6eca7cf

Please sign in to comment.