Skip to content

Commit

Permalink
added support for get/set state tests in erl test code
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrijalva committed May 21, 2010
1 parent 7620c3b commit 9775f53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions test/ernie_server_test.rb
Expand Up @@ -77,6 +77,12 @@ def teardown
assert_equal nil, svc.cast.ext.set_state(7)
assert Time.now - t0 < 1
assert_equal 7, svc.call.ext.get_state

t0 = Time.now
assert_equal nil, svc.cast.intTest.set_state(7)
assert Time.now - t0 < 1
sleep 0.25
assert_equal 7, svc.call.intTest.get_state
end
end

Expand Down
38 changes: 36 additions & 2 deletions test/sample/intTest.erl
@@ -1,6 +1,6 @@
-module(intTest).

-export([zeronary/0, unary/1, binary/2, ternary/3, big/1]).
-export([zeronary/0, unary/1, binary/2, ternary/3, big/1, set_state/1, get_state/0]).

zeronary() ->
foo.
Expand All @@ -15,4 +15,38 @@ ternary(A,B,C) ->
A + B + C.

big(A) ->
string:copies("a", A).
string:copies("a", A).

get_state() ->
case catch global:send(test_saved_state, {get_state, self()}) of
{'EXIT',{badarg, _}} ->
{error, no_record};
_ ->
receive
{ok, State} ->
State
after 1000 ->
{error, timeout}
end
end.

set_state(State) ->
spawn(fun() -> wrapper(State) end),
ok.

wrapper(State) ->
case global:register_name(test_saved_state, self()) of
no ->
global:send(test_saved_state, {set_state, State});
yes ->
recv(State)
end.

recv(State) ->
receive
{set_state, NewState} ->
recv(NewState);
{get_state, Pid} ->
Pid ! {ok, State},
recv(State)
end.

0 comments on commit 9775f53

Please sign in to comment.