gproc_tests.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
  17. %%
  18. -module(gproc_tests).
  19. -ifdef(TEST).
  20. -include_lib("eunit/include/eunit.hrl").
  21. reg_test_() ->
  22. {setup,
  23. fun() ->
  24. application:start(gproc)
  25. end,
  26. fun(_) ->
  27. application:stop(gproc)
  28. end,
  29. [
  30. {spawn, ?_test(t_simple_reg())}
  31. , ?_test(t_is_clean())
  32. , {spawn, ?_test(t_simple_prop())}
  33. , ?_test(t_is_clean())
  34. , {spawn, ?_test(t_await())}
  35. , ?_test(t_is_clean())
  36. , {spawn, ?_test(t_simple_mreg())}
  37. , ?_test(t_is_clean())
  38. , {spawn, ?_test(t_gproc_crash())}
  39. , ?_test(t_is_clean())
  40. , {spawn, ?_test(t_cancel_wait_and_register())}
  41. , ?_test(t_is_clean())
  42. , {spawn, ?_test(t_give_away_to_pid())}
  43. , ?_test(t_is_clean())
  44. , {spawn, ?_test(t_give_away_to_self())}
  45. , ?_test(t_is_clean())
  46. , {spawn, ?_test(t_give_away_badarg())}
  47. , ?_test(t_is_clean())
  48. , {spawn, ?_test(t_give_away_to_unknown())}
  49. , ?_test(t_is_clean())
  50. , {spawn, ?_test(t_give_away_and_back())}
  51. , ?_test(t_is_clean())
  52. ]}.
  53. t_simple_reg() ->
  54. ?assert(gproc:reg({n,l,name}) =:= true),
  55. ?assert(gproc:where({n,l,name}) =:= self()),
  56. ?assert(gproc:unreg({n,l,name}) =:= true),
  57. ?assert(gproc:where({n,l,name}) =:= undefined).
  58. t_simple_prop() ->
  59. ?assert(gproc:reg({p,l,prop}) =:= true),
  60. ?assert(t_other_proc(fun() ->
  61. ?assert(gproc:reg({p,l,prop}) =:= true)
  62. end) =:= ok),
  63. ?assert(gproc:unreg({p,l,prop}) =:= true).
  64. t_other_proc(F) ->
  65. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  66. receive
  67. {'DOWN',Ref,_,_,R} ->
  68. R
  69. after 10000 ->
  70. erlang:error(timeout)
  71. end.
  72. t_await() ->
  73. Me = self(),
  74. {_Pid,Ref} = spawn_monitor(
  75. fun() -> exit(?assert(gproc:await({n,l,t_await}) =:= {Me,val})) end),
  76. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  77. receive
  78. {'DOWN', Ref, _, _, R} ->
  79. ?assertEqual(R, ok)
  80. after 10000 ->
  81. erlang:error(timeout)
  82. end.
  83. t_is_clean() ->
  84. sys:get_status(gproc), % in order to synch
  85. T = ets:tab2list(gproc),
  86. ?assert(T =:= []).
  87. t_simple_mreg() ->
  88. ok.
  89. t_gproc_crash() ->
  90. P = spawn_helper(),
  91. ?assert(gproc:where({n,l,P}) =:= P),
  92. exit(whereis(gproc), kill),
  93. give_gproc_some_time(100),
  94. ?assert(whereis(gproc) =/= undefined),
  95. %%
  96. %% Check that the registration is still there using an ets:lookup(),
  97. %% Once we've killed the process, gproc will always return undefined
  98. %% if the process is not alive, regardless of whether the registration
  99. %% is still there. So, here, the lookup should find something...
  100. %%
  101. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  102. ?assert(gproc:where({n,l,P}) =:= P),
  103. exit(P, kill),
  104. %% ...and here, it shouldn't.
  105. %% (sleep for a while first to let gproc handle the EXIT
  106. give_gproc_some_time(10),
  107. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  108. t_cancel_wait_and_register() ->
  109. Alias = {n, l, foo},
  110. Me = self(),
  111. P = spawn(fun() ->
  112. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  113. ?assert(element(1,sys:get_status(gproc)) == status),
  114. Me ! {self(), go_ahead},
  115. timer:sleep(infinity)
  116. end),
  117. receive
  118. {P, go_ahead} ->
  119. ?assertEqual(gproc:reg(Alias, undefined), true),
  120. exit(P, kill),
  121. timer:sleep(500),
  122. ?assert(element(1,sys:get_status(gproc)) == status)
  123. end.
  124. t_give_away_to_pid() ->
  125. From = {n, l, foo},
  126. Me = self(),
  127. P = spawn_link(fun t_loop/0),
  128. ?assertEqual(true, gproc:reg(From, undefined)),
  129. ?assertEqual(Me, gproc:where(From)),
  130. ?assertEqual(P, gproc:give_away(From, P)),
  131. ?assertEqual(P, gproc:where(From)),
  132. ?assertEqual(ok, t_call(P, die)).
  133. t_give_away_to_self() ->
  134. From = {n, l, foo},
  135. Me = self(),
  136. ?assertEqual(true, gproc:reg(From, undefined)),
  137. ?assertEqual(Me, gproc:where(From)),
  138. ?assertEqual(Me, gproc:give_away(From, Me)),
  139. ?assertEqual(Me, gproc:where(From)),
  140. ?assertEqual(true, gproc:unreg(From)).
  141. t_give_away_badarg() ->
  142. From = {n, l, foo},
  143. Me = self(),
  144. ?assertEqual(undefined, gproc:where(From)),
  145. ?assertError(badarg, gproc:give_away(From, Me)).
  146. t_give_away_to_unknown() ->
  147. From = {n, l, foo},
  148. Unknown = {n, l, unknown},
  149. Me = self(),
  150. ?assertEqual(true, gproc:reg(From, undefined)),
  151. ?assertEqual(Me, gproc:where(From)),
  152. ?assertEqual(undefined, gproc:where(Unknown)),
  153. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  154. ?assertEqual(undefined, gproc:where(From)).
  155. t_give_away_and_back() ->
  156. From = {n, l, foo},
  157. Me = self(),
  158. P = spawn_link(fun t_loop/0),
  159. ?assertEqual(true, gproc:reg(From, undefined)),
  160. ?assertEqual(Me, gproc:where(From)),
  161. ?assertEqual(P, gproc:give_away(From, P)),
  162. ?assertEqual(P, gproc:where(From)),
  163. ?assertEqual(ok, t_call(P, {give_away, From})),
  164. ?assertEqual(Me, gproc:where(From)),
  165. ?assertEqual(ok, t_call(P, die)).
  166. t_loop() ->
  167. receive
  168. {From, {give_away, Key}} ->
  169. ?assertEqual(From, gproc:give_away(Key, From)),
  170. From ! {self(), ok},
  171. t_loop();
  172. {From, die} ->
  173. From ! {self(), ok}
  174. end.
  175. t_call(P, Msg) ->
  176. P ! {self(), Msg},
  177. receive
  178. {P, Reply} ->
  179. Reply
  180. end.
  181. spawn_helper() ->
  182. Parent = self(),
  183. P = spawn(fun() ->
  184. ?assert(gproc:reg({n,l,self()}) =:= true),
  185. Ref = erlang:monitor(process, Parent),
  186. Parent ! {ok,self()},
  187. receive
  188. {'DOWN', Ref, _, _, _} ->
  189. ok
  190. end
  191. end),
  192. receive
  193. {ok,P} ->
  194. P
  195. end.
  196. give_gproc_some_time(T) ->
  197. timer:sleep(T),
  198. sys:get_status(gproc).
  199. -endif.