gproc_tests.erl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. -include_lib("stdlib/include/qlc.hrl").
  22. reg_test_() ->
  23. {setup,
  24. fun() ->
  25. application:start(gproc)
  26. end,
  27. fun(_) ->
  28. application:stop(gproc)
  29. end,
  30. [
  31. {spawn, ?_test(t_simple_reg())}
  32. , ?_test(t_is_clean())
  33. , {spawn, ?_test(t_simple_prop())}
  34. , ?_test(t_is_clean())
  35. , {spawn, ?_test(t_await())}
  36. , ?_test(t_is_clean())
  37. , {spawn, ?_test(t_simple_mreg())}
  38. , ?_test(t_is_clean())
  39. , {spawn, ?_test(t_gproc_crash())}
  40. , ?_test(t_is_clean())
  41. , {spawn, ?_test(t_cancel_wait_and_register())}
  42. , ?_test(t_is_clean())
  43. , {spawn, ?_test(t_give_away_to_pid())}
  44. , ?_test(t_is_clean())
  45. , {spawn, ?_test(t_give_away_to_self())}
  46. , ?_test(t_is_clean())
  47. , {spawn, ?_test(t_give_away_badarg())}
  48. , ?_test(t_is_clean())
  49. , {spawn, ?_test(t_give_away_to_unknown())}
  50. , ?_test(t_is_clean())
  51. , {spawn, ?_test(t_give_away_and_back())}
  52. , ?_test(t_is_clean())
  53. , {spawn, ?_test(t_select())}
  54. , ?_test(t_is_clean())
  55. , {spawn, ?_test(t_qlc())}
  56. , ?_test(t_is_clean())
  57. ]}.
  58. t_simple_reg() ->
  59. ?assert(gproc:reg({n,l,name}) =:= true),
  60. ?assert(gproc:where({n,l,name}) =:= self()),
  61. ?assert(gproc:unreg({n,l,name}) =:= true),
  62. ?assert(gproc:where({n,l,name}) =:= undefined).
  63. t_simple_prop() ->
  64. ?assert(gproc:reg({p,l,prop}) =:= true),
  65. ?assert(t_other_proc(fun() ->
  66. ?assert(gproc:reg({p,l,prop}) =:= true)
  67. end) =:= ok),
  68. ?assert(gproc:unreg({p,l,prop}) =:= true).
  69. t_other_proc(F) ->
  70. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  71. receive
  72. {'DOWN',Ref,_,_,R} ->
  73. R
  74. after 10000 ->
  75. erlang:error(timeout)
  76. end.
  77. t_await() ->
  78. Me = self(),
  79. {_Pid,Ref} = spawn_monitor(
  80. fun() -> exit(?assert(gproc:await({n,l,t_await}) =:= {Me,val})) end),
  81. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  82. receive
  83. {'DOWN', Ref, _, _, R} ->
  84. ?assertEqual(R, ok)
  85. after 10000 ->
  86. erlang:error(timeout)
  87. end.
  88. t_is_clean() ->
  89. sys:get_status(gproc), % in order to synch
  90. T = ets:tab2list(gproc),
  91. ?assert(T =:= []).
  92. t_simple_mreg() ->
  93. ok.
  94. t_gproc_crash() ->
  95. P = spawn_helper(),
  96. ?assert(gproc:where({n,l,P}) =:= P),
  97. exit(whereis(gproc), kill),
  98. give_gproc_some_time(100),
  99. ?assert(whereis(gproc) =/= undefined),
  100. %%
  101. %% Check that the registration is still there using an ets:lookup(),
  102. %% Once we've killed the process, gproc will always return undefined
  103. %% if the process is not alive, regardless of whether the registration
  104. %% is still there. So, here, the lookup should find something...
  105. %%
  106. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  107. ?assert(gproc:where({n,l,P}) =:= P),
  108. exit(P, kill),
  109. %% ...and here, it shouldn't.
  110. %% (sleep for a while first to let gproc handle the EXIT
  111. give_gproc_some_time(10),
  112. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  113. t_cancel_wait_and_register() ->
  114. Alias = {n, l, foo},
  115. Me = self(),
  116. P = spawn(fun() ->
  117. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  118. ?assert(element(1,sys:get_status(gproc)) == status),
  119. Me ! {self(), go_ahead},
  120. timer:sleep(infinity)
  121. end),
  122. receive
  123. {P, go_ahead} ->
  124. ?assertEqual(gproc:reg(Alias, undefined), true),
  125. exit(P, kill),
  126. timer:sleep(500),
  127. ?assert(element(1,sys:get_status(gproc)) == status)
  128. end.
  129. t_give_away_to_pid() ->
  130. From = {n, l, foo},
  131. Me = self(),
  132. P = spawn_link(fun t_loop/0),
  133. ?assertEqual(true, gproc:reg(From, undefined)),
  134. ?assertEqual(Me, gproc:where(From)),
  135. ?assertEqual(P, gproc:give_away(From, P)),
  136. ?assertEqual(P, gproc:where(From)),
  137. ?assertEqual(ok, t_call(P, die)).
  138. t_give_away_to_self() ->
  139. From = {n, l, foo},
  140. Me = self(),
  141. ?assertEqual(true, gproc:reg(From, undefined)),
  142. ?assertEqual(Me, gproc:where(From)),
  143. ?assertEqual(Me, gproc:give_away(From, Me)),
  144. ?assertEqual(Me, gproc:where(From)),
  145. ?assertEqual(true, gproc:unreg(From)).
  146. t_give_away_badarg() ->
  147. From = {n, l, foo},
  148. Me = self(),
  149. ?assertEqual(undefined, gproc:where(From)),
  150. ?assertError(badarg, gproc:give_away(From, Me)).
  151. t_give_away_to_unknown() ->
  152. From = {n, l, foo},
  153. Unknown = {n, l, unknown},
  154. Me = self(),
  155. ?assertEqual(true, gproc:reg(From, undefined)),
  156. ?assertEqual(Me, gproc:where(From)),
  157. ?assertEqual(undefined, gproc:where(Unknown)),
  158. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  159. ?assertEqual(undefined, gproc:where(From)).
  160. t_give_away_and_back() ->
  161. From = {n, l, foo},
  162. Me = self(),
  163. P = spawn_link(fun t_loop/0),
  164. ?assertEqual(true, gproc:reg(From, undefined)),
  165. ?assertEqual(Me, gproc:where(From)),
  166. ?assertEqual(P, gproc:give_away(From, P)),
  167. ?assertEqual(P, gproc:where(From)),
  168. ?assertEqual(ok, t_call(P, {give_away, From})),
  169. ?assertEqual(Me, gproc:where(From)),
  170. ?assertEqual(ok, t_call(P, die)).
  171. t_select() ->
  172. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  173. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  174. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  175. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  176. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  177. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  178. %% local names
  179. ?assertEqual(
  180. [{{n,l,{n,1}},self(),x},
  181. {{n,l,{n,2}},self(),y}], gproc:select(
  182. {local,names},
  183. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  184. %% mactch local names on value
  185. ?assertEqual(
  186. [{{n,l,{n,1}},self(),x}], gproc:select(
  187. {local,names},
  188. [{{{n,l,'_'},'_',x},[],['$_']}])),
  189. %% match all on value
  190. ?assertEqual(
  191. [{{n,l,{n,1}},self(),x},
  192. {{p,l,{p,1}},self(),x}], gproc:select(
  193. {all,all},
  194. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  195. %% match all on pid
  196. ?assertEqual(
  197. [{{a,l,{c,1}},self(),1},
  198. {{c,l,{c,1}},self(),1},
  199. {{n,l,{n,1}},self(),x},
  200. {{n,l,{n,2}},self(),y},
  201. {{p,l,{p,1}},self(),x},
  202. {{p,l,{p,2}},self(),y}
  203. ], gproc:select(
  204. {all,all},
  205. [{{'_',self(),'_'},[],['$_']}])).
  206. t_qlc() ->
  207. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  208. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  209. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  210. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  211. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  212. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  213. %% local names
  214. Exp1 = [{{n,l,{n,1}},self(),x},
  215. {{n,l,{n,2}},self(),y}],
  216. ?assertEqual(Exp1,
  217. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  218. ?assertEqual(Exp1,
  219. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  220. %% mactch local names on value
  221. Exp2 = [{{n,l,{n,1}},self(),x}],
  222. ?assertEqual(Exp2,
  223. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  224. %% match all on value
  225. Exp3 = [{{n,l,{n,1}},self(),x},
  226. {{p,l,{p,1}},self(),x}],
  227. ?assertEqual(Exp3,
  228. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  229. %% match all on pid
  230. Exp4 = [{{a,l,{c,1}},self(),1},
  231. {{c,l,{c,1}},self(),1},
  232. {{n,l,{n,1}},self(),x},
  233. {{n,l,{n,2}},self(),y},
  234. {{p,l,{p,1}},self(),x},
  235. {{p,l,{p,2}},self(),y}
  236. ],
  237. ?assertEqual(Exp4,
  238. qlc:e(qlc:q([X || X <- gproc:table(all)]))).
  239. t_loop() ->
  240. receive
  241. {From, {give_away, Key}} ->
  242. ?assertEqual(From, gproc:give_away(Key, From)),
  243. From ! {self(), ok},
  244. t_loop();
  245. {From, die} ->
  246. From ! {self(), ok}
  247. end.
  248. t_call(P, Msg) ->
  249. P ! {self(), Msg},
  250. receive
  251. {P, Reply} ->
  252. Reply
  253. end.
  254. spawn_helper() ->
  255. Parent = self(),
  256. P = spawn(fun() ->
  257. ?assert(gproc:reg({n,l,self()}) =:= true),
  258. Ref = erlang:monitor(process, Parent),
  259. Parent ! {ok,self()},
  260. receive
  261. {'DOWN', Ref, _, _, _} ->
  262. ok
  263. end
  264. end),
  265. receive
  266. {ok,P} ->
  267. P
  268. end.
  269. give_gproc_some_time(T) ->
  270. timer:sleep(T),
  271. sys:get_status(gproc).
  272. -endif.