gproc_tests.erl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. , {spawn, ?_test(t_select())}
  53. , ?_test(t_is_clean())
  54. ]}.
  55. t_simple_reg() ->
  56. ?assert(gproc:reg({n,l,name}) =:= true),
  57. ?assert(gproc:where({n,l,name}) =:= self()),
  58. ?assert(gproc:unreg({n,l,name}) =:= true),
  59. ?assert(gproc:where({n,l,name}) =:= undefined).
  60. t_simple_prop() ->
  61. ?assert(gproc:reg({p,l,prop}) =:= true),
  62. ?assert(t_other_proc(fun() ->
  63. ?assert(gproc:reg({p,l,prop}) =:= true)
  64. end) =:= ok),
  65. ?assert(gproc:unreg({p,l,prop}) =:= true).
  66. t_other_proc(F) ->
  67. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  68. receive
  69. {'DOWN',Ref,_,_,R} ->
  70. R
  71. after 10000 ->
  72. erlang:error(timeout)
  73. end.
  74. t_await() ->
  75. Me = self(),
  76. {_Pid,Ref} = spawn_monitor(
  77. fun() -> exit(?assert(gproc:await({n,l,t_await}) =:= {Me,val})) end),
  78. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  79. receive
  80. {'DOWN', Ref, _, _, R} ->
  81. ?assertEqual(R, ok)
  82. after 10000 ->
  83. erlang:error(timeout)
  84. end.
  85. t_is_clean() ->
  86. sys:get_status(gproc), % in order to synch
  87. T = ets:tab2list(gproc),
  88. ?assert(T =:= []).
  89. t_simple_mreg() ->
  90. ok.
  91. t_gproc_crash() ->
  92. P = spawn_helper(),
  93. ?assert(gproc:where({n,l,P}) =:= P),
  94. exit(whereis(gproc), kill),
  95. give_gproc_some_time(100),
  96. ?assert(whereis(gproc) =/= undefined),
  97. %%
  98. %% Check that the registration is still there using an ets:lookup(),
  99. %% Once we've killed the process, gproc will always return undefined
  100. %% if the process is not alive, regardless of whether the registration
  101. %% is still there. So, here, the lookup should find something...
  102. %%
  103. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  104. ?assert(gproc:where({n,l,P}) =:= P),
  105. exit(P, kill),
  106. %% ...and here, it shouldn't.
  107. %% (sleep for a while first to let gproc handle the EXIT
  108. give_gproc_some_time(10),
  109. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  110. t_cancel_wait_and_register() ->
  111. Alias = {n, l, foo},
  112. Me = self(),
  113. P = spawn(fun() ->
  114. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  115. ?assert(element(1,sys:get_status(gproc)) == status),
  116. Me ! {self(), go_ahead},
  117. timer:sleep(infinity)
  118. end),
  119. receive
  120. {P, go_ahead} ->
  121. ?assertEqual(gproc:reg(Alias, undefined), true),
  122. exit(P, kill),
  123. timer:sleep(500),
  124. ?assert(element(1,sys:get_status(gproc)) == status)
  125. end.
  126. t_give_away_to_pid() ->
  127. From = {n, l, foo},
  128. Me = self(),
  129. P = spawn_link(fun t_loop/0),
  130. ?assertEqual(true, gproc:reg(From, undefined)),
  131. ?assertEqual(Me, gproc:where(From)),
  132. ?assertEqual(P, gproc:give_away(From, P)),
  133. ?assertEqual(P, gproc:where(From)),
  134. ?assertEqual(ok, t_call(P, die)).
  135. t_give_away_to_self() ->
  136. From = {n, l, foo},
  137. Me = self(),
  138. ?assertEqual(true, gproc:reg(From, undefined)),
  139. ?assertEqual(Me, gproc:where(From)),
  140. ?assertEqual(Me, gproc:give_away(From, Me)),
  141. ?assertEqual(Me, gproc:where(From)),
  142. ?assertEqual(true, gproc:unreg(From)).
  143. t_give_away_badarg() ->
  144. From = {n, l, foo},
  145. Me = self(),
  146. ?assertEqual(undefined, gproc:where(From)),
  147. ?assertError(badarg, gproc:give_away(From, Me)).
  148. t_give_away_to_unknown() ->
  149. From = {n, l, foo},
  150. Unknown = {n, l, unknown},
  151. Me = self(),
  152. ?assertEqual(true, gproc:reg(From, undefined)),
  153. ?assertEqual(Me, gproc:where(From)),
  154. ?assertEqual(undefined, gproc:where(Unknown)),
  155. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  156. ?assertEqual(undefined, gproc:where(From)).
  157. t_give_away_and_back() ->
  158. From = {n, l, foo},
  159. Me = self(),
  160. P = spawn_link(fun t_loop/0),
  161. ?assertEqual(true, gproc:reg(From, undefined)),
  162. ?assertEqual(Me, gproc:where(From)),
  163. ?assertEqual(P, gproc:give_away(From, P)),
  164. ?assertEqual(P, gproc:where(From)),
  165. ?assertEqual(ok, t_call(P, {give_away, From})),
  166. ?assertEqual(Me, gproc:where(From)),
  167. ?assertEqual(ok, t_call(P, die)).
  168. t_select() ->
  169. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  170. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  171. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  172. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  173. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  174. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  175. %% local names
  176. ?assertEqual(
  177. [{{n,l,{n,1}},self(),x},
  178. {{n,l,{n,2}},self(),y}], gproc:select(
  179. {local,names},
  180. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  181. %% mactch local names on value
  182. ?assertEqual(
  183. [{{n,l,{n,1}},self(),x}], gproc:select(
  184. {local,names},
  185. [{{{n,l,'_'},'_',x},[],['$_']}])),
  186. %% match all on value
  187. ?assertEqual(
  188. [{{n,l,{n,1}},self(),x},
  189. {{p,l,{p,1}},self(),x}], gproc:select(
  190. {all,all},
  191. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  192. %% match all on pid
  193. ?assertEqual(
  194. [{{a,l,{c,1}},self(),1},
  195. {{c,l,{c,1}},self(),1},
  196. {{n,l,{n,1}},self(),x},
  197. {{n,l,{n,2}},self(),y},
  198. {{p,l,{p,1}},self(),x},
  199. {{p,l,{p,2}},self(),y}
  200. ], gproc:select(
  201. {all,all},
  202. [{{'_',self(),'_'},[],['$_']}])).
  203. t_loop() ->
  204. receive
  205. {From, {give_away, Key}} ->
  206. ?assertEqual(From, gproc:give_away(Key, From)),
  207. From ! {self(), ok},
  208. t_loop();
  209. {From, die} ->
  210. From ! {self(), ok}
  211. end.
  212. t_call(P, Msg) ->
  213. P ! {self(), Msg},
  214. receive
  215. {P, Reply} ->
  216. Reply
  217. end.
  218. spawn_helper() ->
  219. Parent = self(),
  220. P = spawn(fun() ->
  221. ?assert(gproc:reg({n,l,self()}) =:= true),
  222. Ref = erlang:monitor(process, Parent),
  223. Parent ! {ok,self()},
  224. receive
  225. {'DOWN', Ref, _, _, _} ->
  226. ok
  227. end
  228. end),
  229. receive
  230. {ok,P} ->
  231. P
  232. end.
  233. give_gproc_some_time(T) ->
  234. timer:sleep(T),
  235. sys:get_status(gproc).
  236. -endif.