Skip to content

Commit

Permalink
Add common_test for esp_cache adding an M:F(A) value datum coop_node
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynel committed Aug 29, 2012
1 parent 9965fd8 commit b43a7da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 12 additions & 8 deletions apps/ctest/coop/esp_cache_SUITE.erl
Expand Up @@ -9,12 +9,12 @@
-export([all/0, init_per_suite/1, end_per_suite/1]).

%% Test Coop Node functionality individually
-export([datum_value/1, worker_value/1]).
-export([datum_value/1, worker_value/1, worker_mfa/1]).

%% Spawned functions must be exported
-export([check_worker/2]).
-export([check_worker/2, compute_value/1]).

all() -> [datum_value, worker_value].
all() -> [datum_value, worker_value, worker_mfa].

init_per_suite(Config) -> Config.
end_per_suite(_Config) -> ok.
Expand Down Expand Up @@ -56,7 +56,7 @@ check_datum(Ref) ->
after 1000 -> timeout
end.

worker_value(_Config) ->
worker_test_age(Value_Expr, Answer) ->

%% Create a worker node...
Self = self(),
Expand All @@ -70,15 +70,15 @@ worker_value(_Config) ->
%% Create a new cached datum node from a simple value...
R1 = make_ref(),
Rcvr = proc_lib:spawn_link(?MODULE, check_worker, [R1, []]),
coop:relay_data(Coop_Node, {add, {age, {?VALUE, 15}, {R1, Rcvr}}}),
coop:relay_data(Coop_Node, {add, {age, Value_Expr, {R1, Rcvr}}}),
Results = check_worker([]),
2 = length(Results),
[?CTL_MSG({link, _Pids2})] = [I || I <- Results, element(1,element(3, I)) =:= link],
[?DATA_MSG({new, age, {coop_node, _, _}})]
= [I || I <- Results, element(1, element(3, I)) =:= new],
true = is_process_alive(Rcvr),
Rcvr ! {results, Self},
[15] = receive B -> B after 1000 -> no_value end,
[Answer] = receive B -> B after 1000 -> no_value end,
meck:unload(coop).

check_worker(Acc) ->
Expand All @@ -91,5 +91,9 @@ check_worker(Ref, Acc) ->
{results, From} -> From ! lists:reverse(Acc)
after 5000 -> no_msg
end.



worker_value(_Config) -> worker_test_age({?VALUE, 15}, 15).
worker_mfa(_Config) -> worker_test_age({?MFA, {?MODULE, compute_value, 7}}, 21).

compute_value(X) -> 3*X.

8 changes: 5 additions & 3 deletions apps/examples/esp_cache/src/esp_cache.erl
Expand Up @@ -173,17 +173,19 @@ init_mfa_worker({{coop_head, _Node_Ctl_Pid, _Node_Task_Pid}, _Kill_Switch} = Sta

%% Compute the replacement value and forward to the existing Coop_Node...
make_new_datum(State, {replace, {_Key, {?MFA, {Mod, Fun, Args}}, {_Ref, _Rqstr} = Requester}, Coop_Node}) ->
%% Directory already knows about this datum, using worker for potentially long running M:F(A)
coop:relay_data(Coop_Node, {replace, Mod:Fun(Args), Requester}),
{State, noop};

%% Create a new Coop_Node initialized with the value to cache, notifying the Coop_Head directory.
make_new_datum({Coop_Head, Kill_Switch} = State, {add, {Key, {?VALUE, V}, {_Ref, _Rqstr} = Requester}}) ->
New_Coop_Node = new_datum_node(Kill_Switch, V),
coop:relay_high_priority_data(Coop_Head, {new, Key, New_Coop_Node}),
coop:relay_data(New_Coop_Node, {get_value, Requester}),
{State, noop};
relay_new_datum(Coop_Head, Key, New_Coop_Node, Requester, State);
make_new_datum({Coop_Head, Kill_Switch} = State, {add, {Key, {?MFA, {Mod, Fun, Args}}, {_Ref, _Rqstr} = Requester}}) ->
New_Coop_Node = new_datum_node(Kill_Switch, Mod:Fun(Args)),
relay_new_datum(Coop_Head, Key, New_Coop_Node, Requester, State).

relay_new_datum(Coop_Head, Key, New_Coop_Node, Requester, State) ->
coop:relay_high_priority_data(Coop_Head, {new, Key, New_Coop_Node}),
coop:relay_data(New_Coop_Node, {get_value, Requester}),
{State, noop}.
Expand Down

0 comments on commit b43a7da

Please sign in to comment.