gproc_tests.erl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. P = self(),
  94. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  95. {bar, bar_val}])),
  96. ?assertEqual(P, gproc:where({n,l,foo})),
  97. ?assertEqual(P, gproc:where({n,l,bar})).
  98. t_gproc_crash() ->
  99. P = spawn_helper(),
  100. ?assert(gproc:where({n,l,P}) =:= P),
  101. exit(whereis(gproc), kill),
  102. give_gproc_some_time(100),
  103. ?assert(whereis(gproc) =/= undefined),
  104. %%
  105. %% Check that the registration is still there using an ets:lookup(),
  106. %% Once we've killed the process, gproc will always return undefined
  107. %% if the process is not alive, regardless of whether the registration
  108. %% is still there. So, here, the lookup should find something...
  109. %%
  110. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  111. ?assert(gproc:where({n,l,P}) =:= P),
  112. exit(P, kill),
  113. %% ...and here, it shouldn't.
  114. %% (sleep for a while first to let gproc handle the EXIT
  115. give_gproc_some_time(10),
  116. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  117. t_cancel_wait_and_register() ->
  118. Alias = {n, l, foo},
  119. Me = self(),
  120. P = spawn(fun() ->
  121. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  122. ?assert(element(1,sys:get_status(gproc)) == status),
  123. Me ! {self(), go_ahead},
  124. timer:sleep(infinity)
  125. end),
  126. receive
  127. {P, go_ahead} ->
  128. ?assertEqual(gproc:reg(Alias, undefined), true),
  129. exit(P, kill),
  130. timer:sleep(500),
  131. ?assert(element(1,sys:get_status(gproc)) == status)
  132. end.
  133. t_give_away_to_pid() ->
  134. From = {n, l, foo},
  135. Me = self(),
  136. P = spawn_link(fun t_loop/0),
  137. ?assertEqual(true, gproc:reg(From, undefined)),
  138. ?assertEqual(Me, gproc:where(From)),
  139. ?assertEqual(P, gproc:give_away(From, P)),
  140. ?assertEqual(P, gproc:where(From)),
  141. ?assertEqual(ok, t_call(P, die)).
  142. t_give_away_to_self() ->
  143. From = {n, l, foo},
  144. Me = self(),
  145. ?assertEqual(true, gproc:reg(From, undefined)),
  146. ?assertEqual(Me, gproc:where(From)),
  147. ?assertEqual(Me, gproc:give_away(From, Me)),
  148. ?assertEqual(Me, gproc:where(From)),
  149. ?assertEqual(true, gproc:unreg(From)).
  150. t_give_away_badarg() ->
  151. From = {n, l, foo},
  152. Me = self(),
  153. ?assertEqual(undefined, gproc:where(From)),
  154. ?assertError(badarg, gproc:give_away(From, Me)).
  155. t_give_away_to_unknown() ->
  156. From = {n, l, foo},
  157. Unknown = {n, l, unknown},
  158. Me = self(),
  159. ?assertEqual(true, gproc:reg(From, undefined)),
  160. ?assertEqual(Me, gproc:where(From)),
  161. ?assertEqual(undefined, gproc:where(Unknown)),
  162. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  163. ?assertEqual(undefined, gproc:where(From)).
  164. t_give_away_and_back() ->
  165. From = {n, l, foo},
  166. Me = self(),
  167. P = spawn_link(fun t_loop/0),
  168. ?assertEqual(true, gproc:reg(From, undefined)),
  169. ?assertEqual(Me, gproc:where(From)),
  170. ?assertEqual(P, gproc:give_away(From, P)),
  171. ?assertEqual(P, gproc:where(From)),
  172. ?assertEqual(ok, t_call(P, {give_away, From})),
  173. ?assertEqual(Me, gproc:where(From)),
  174. ?assertEqual(ok, t_call(P, die)).
  175. t_select() ->
  176. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  177. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  178. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  179. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  180. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  181. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  182. %% local names
  183. ?assertEqual(
  184. [{{n,l,{n,1}},self(),x},
  185. {{n,l,{n,2}},self(),y}], gproc:select(
  186. {local,names},
  187. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  188. %% mactch local names on value
  189. ?assertEqual(
  190. [{{n,l,{n,1}},self(),x}], gproc:select(
  191. {local,names},
  192. [{{{n,l,'_'},'_',x},[],['$_']}])),
  193. %% match all on value
  194. ?assertEqual(
  195. [{{n,l,{n,1}},self(),x},
  196. {{p,l,{p,1}},self(),x}], gproc:select(
  197. {all,all},
  198. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  199. %% match all on pid
  200. ?assertEqual(
  201. [{{a,l,{c,1}},self(),1},
  202. {{c,l,{c,1}},self(),1},
  203. {{n,l,{n,1}},self(),x},
  204. {{n,l,{n,2}},self(),y},
  205. {{p,l,{p,1}},self(),x},
  206. {{p,l,{p,2}},self(),y}
  207. ], gproc:select(
  208. {all,all},
  209. [{{'_',self(),'_'},[],['$_']}])).
  210. t_qlc() ->
  211. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  212. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  213. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  214. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  215. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  216. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  217. %% local names
  218. Exp1 = [{{n,l,{n,1}},self(),x},
  219. {{n,l,{n,2}},self(),y}],
  220. ?assertEqual(Exp1,
  221. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  222. ?assertEqual(Exp1,
  223. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  224. %% mactch local names on value
  225. Exp2 = [{{n,l,{n,1}},self(),x}],
  226. ?assertEqual(Exp2,
  227. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  228. %% match all on value
  229. Exp3 = [{{n,l,{n,1}},self(),x},
  230. {{p,l,{p,1}},self(),x}],
  231. ?assertEqual(Exp3,
  232. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  233. %% match all
  234. Exp4 = [{{a,l,{c,1}},self(),1},
  235. {{c,l,{c,1}},self(),1},
  236. {{n,l,{n,1}},self(),x},
  237. {{n,l,{n,2}},self(),y},
  238. {{p,l,{p,1}},self(),x},
  239. {{p,l,{p,2}},self(),y}
  240. ],
  241. ?assertEqual(Exp4,
  242. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  243. %% match on pid
  244. ?assertEqual(Exp4,
  245. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  246. gproc:table(all), P =:= self()]))),
  247. ?assertEqual(Exp4,
  248. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  249. gproc:table(all), P == self()]))).
  250. t_loop() ->
  251. receive
  252. {From, {give_away, Key}} ->
  253. ?assertEqual(From, gproc:give_away(Key, From)),
  254. From ! {self(), ok},
  255. t_loop();
  256. {From, die} ->
  257. From ! {self(), ok}
  258. end.
  259. t_call(P, Msg) ->
  260. P ! {self(), Msg},
  261. receive
  262. {P, Reply} ->
  263. Reply
  264. end.
  265. spawn_helper() ->
  266. Parent = self(),
  267. P = spawn(fun() ->
  268. ?assert(gproc:reg({n,l,self()}) =:= true),
  269. Ref = erlang:monitor(process, Parent),
  270. Parent ! {ok,self()},
  271. receive
  272. {'DOWN', Ref, _, _, _} ->
  273. ok
  274. end
  275. end),
  276. receive
  277. {ok,P} ->
  278. P
  279. end.
  280. give_gproc_some_time(T) ->
  281. timer:sleep(T),
  282. sys:get_status(gproc).
  283. -endif.