gproc_tests.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. application:start(mnesia)
  27. end,
  28. fun(_) ->
  29. application:stop(gproc),
  30. application:stop(mnesia)
  31. end,
  32. [
  33. {spawn, ?_test(t_simple_reg())}
  34. , ?_test(t_is_clean())
  35. , {spawn, ?_test(t_simple_prop())}
  36. , ?_test(t_is_clean())
  37. , {spawn, ?_test(t_await())}
  38. , ?_test(t_is_clean())
  39. , {spawn, ?_test(t_simple_mreg())}
  40. , ?_test(t_is_clean())
  41. , {spawn, ?_test(t_gproc_crash())}
  42. , ?_test(t_is_clean())
  43. , {spawn, ?_test(t_cancel_wait_and_register())}
  44. , ?_test(t_is_clean())
  45. , {spawn, ?_test(t_give_away_to_pid())}
  46. , ?_test(t_is_clean())
  47. , {spawn, ?_test(t_give_away_to_self())}
  48. , ?_test(t_is_clean())
  49. , {spawn, ?_test(t_give_away_badarg())}
  50. , ?_test(t_is_clean())
  51. , {spawn, ?_test(t_give_away_to_unknown())}
  52. , ?_test(t_is_clean())
  53. , {spawn, ?_test(t_give_away_and_back())}
  54. , ?_test(t_is_clean())
  55. , {spawn, ?_test(t_select())}
  56. , ?_test(t_is_clean())
  57. , {spawn, ?_test(t_qlc())}
  58. , ?_test(t_is_clean())
  59. , {spawn, ?_test(t_get_env())}
  60. , ?_test(t_is_clean())
  61. , {spawn, ?_test(t_get_set_env())}
  62. , ?_test(t_is_clean())
  63. , {spawn, ?_test(t_set_env())}
  64. , ?_test(t_is_clean())
  65. , {spawn, ?_test(t_get_env_inherit())}
  66. , ?_test(t_is_clean())
  67. ]}.
  68. t_simple_reg() ->
  69. ?assert(gproc:reg({n,l,name}) =:= true),
  70. ?assert(gproc:where({n,l,name}) =:= self()),
  71. ?assert(gproc:unreg({n,l,name}) =:= true),
  72. ?assert(gproc:where({n,l,name}) =:= undefined).
  73. t_simple_prop() ->
  74. ?assert(gproc:reg({p,l,prop}) =:= true),
  75. ?assert(t_other_proc(fun() ->
  76. ?assert(gproc:reg({p,l,prop}) =:= true)
  77. end) =:= ok),
  78. ?assert(gproc:unreg({p,l,prop}) =:= true).
  79. t_other_proc(F) ->
  80. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  81. receive
  82. {'DOWN',Ref,_,_,R} ->
  83. R
  84. after 10000 ->
  85. erlang:error(timeout)
  86. end.
  87. t_await() ->
  88. Me = self(),
  89. {_Pid,Ref} = spawn_monitor(
  90. fun() -> exit(?assert(gproc:await({n,l,t_await}) =:= {Me,val})) end),
  91. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  92. receive
  93. {'DOWN', Ref, _, _, R} ->
  94. ?assertEqual(R, ok)
  95. after 10000 ->
  96. erlang:error(timeout)
  97. end.
  98. t_is_clean() ->
  99. sys:get_status(gproc), % in order to synch
  100. T = ets:tab2list(gproc),
  101. ?assert(T =:= []).
  102. t_simple_mreg() ->
  103. P = self(),
  104. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  105. {bar, bar_val}])),
  106. ?assertEqual(P, gproc:where({n,l,foo})),
  107. ?assertEqual(P, gproc:where({n,l,bar})),
  108. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  109. t_gproc_crash() ->
  110. P = spawn_helper(),
  111. ?assert(gproc:where({n,l,P}) =:= P),
  112. exit(whereis(gproc), kill),
  113. give_gproc_some_time(100),
  114. ?assert(whereis(gproc) =/= undefined),
  115. %%
  116. %% Check that the registration is still there using an ets:lookup(),
  117. %% Once we've killed the process, gproc will always return undefined
  118. %% if the process is not alive, regardless of whether the registration
  119. %% is still there. So, here, the lookup should find something...
  120. %%
  121. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  122. ?assert(gproc:where({n,l,P}) =:= P),
  123. exit(P, kill),
  124. %% ...and here, it shouldn't.
  125. %% (sleep for a while first to let gproc handle the EXIT
  126. give_gproc_some_time(10),
  127. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  128. t_cancel_wait_and_register() ->
  129. Alias = {n, l, foo},
  130. Me = self(),
  131. P = spawn(fun() ->
  132. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  133. ?assert(element(1,sys:get_status(gproc)) == status),
  134. Me ! {self(), go_ahead},
  135. timer:sleep(infinity)
  136. end),
  137. receive
  138. {P, go_ahead} ->
  139. ?assertEqual(gproc:reg(Alias, undefined), true),
  140. exit(P, kill),
  141. timer:sleep(500),
  142. ?assert(element(1,sys:get_status(gproc)) == status)
  143. end.
  144. t_give_away_to_pid() ->
  145. From = {n, l, foo},
  146. Me = self(),
  147. P = spawn_link(fun t_loop/0),
  148. ?assertEqual(true, gproc:reg(From, undefined)),
  149. ?assertEqual(Me, gproc:where(From)),
  150. ?assertEqual(P, gproc:give_away(From, P)),
  151. ?assertEqual(P, gproc:where(From)),
  152. ?assertEqual(ok, t_call(P, die)).
  153. t_give_away_to_self() ->
  154. From = {n, l, foo},
  155. Me = self(),
  156. ?assertEqual(true, gproc:reg(From, undefined)),
  157. ?assertEqual(Me, gproc:where(From)),
  158. ?assertEqual(Me, gproc:give_away(From, Me)),
  159. ?assertEqual(Me, gproc:where(From)),
  160. ?assertEqual(true, gproc:unreg(From)).
  161. t_give_away_badarg() ->
  162. From = {n, l, foo},
  163. Me = self(),
  164. ?assertEqual(undefined, gproc:where(From)),
  165. ?assertError(badarg, gproc:give_away(From, Me)).
  166. t_give_away_to_unknown() ->
  167. From = {n, l, foo},
  168. Unknown = {n, l, unknown},
  169. Me = self(),
  170. ?assertEqual(true, gproc:reg(From, undefined)),
  171. ?assertEqual(Me, gproc:where(From)),
  172. ?assertEqual(undefined, gproc:where(Unknown)),
  173. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  174. ?assertEqual(undefined, gproc:where(From)).
  175. t_give_away_and_back() ->
  176. From = {n, l, foo},
  177. Me = self(),
  178. P = spawn_link(fun t_loop/0),
  179. ?assertEqual(true, gproc:reg(From, undefined)),
  180. ?assertEqual(Me, gproc:where(From)),
  181. ?assertEqual(P, gproc:give_away(From, P)),
  182. ?assertEqual(P, gproc:where(From)),
  183. ?assertEqual(ok, t_call(P, {give_away, From})),
  184. ?assertEqual(Me, gproc:where(From)),
  185. ?assertEqual(ok, t_call(P, die)).
  186. t_select() ->
  187. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  188. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  189. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  190. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  191. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  192. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  193. %% local names
  194. ?assertEqual(
  195. [{{n,l,{n,1}},self(),x},
  196. {{n,l,{n,2}},self(),y}], gproc:select(
  197. {local,names},
  198. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  199. %% mactch local names on value
  200. ?assertEqual(
  201. [{{n,l,{n,1}},self(),x}], gproc:select(
  202. {local,names},
  203. [{{{n,l,'_'},'_',x},[],['$_']}])),
  204. %% match all on value
  205. ?assertEqual(
  206. [{{n,l,{n,1}},self(),x},
  207. {{p,l,{p,1}},self(),x}], gproc:select(
  208. {all,all},
  209. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  210. %% match all on pid
  211. ?assertEqual(
  212. [{{a,l,{c,1}},self(),1},
  213. {{c,l,{c,1}},self(),1},
  214. {{n,l,{n,1}},self(),x},
  215. {{n,l,{n,2}},self(),y},
  216. {{p,l,{p,1}},self(),x},
  217. {{p,l,{p,2}},self(),y}
  218. ], gproc:select(
  219. {all,all},
  220. [{{'_',self(),'_'},[],['$_']}])).
  221. t_qlc() ->
  222. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  223. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  224. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  225. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  226. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  227. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  228. %% local names
  229. Exp1 = [{{n,l,{n,1}},self(),x},
  230. {{n,l,{n,2}},self(),y}],
  231. ?assertEqual(Exp1,
  232. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  233. ?assertEqual(Exp1,
  234. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  235. %% mactch local names on value
  236. Exp2 = [{{n,l,{n,1}},self(),x}],
  237. ?assertEqual(Exp2,
  238. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  239. %% match all on value
  240. Exp3 = [{{n,l,{n,1}},self(),x},
  241. {{p,l,{p,1}},self(),x}],
  242. ?assertEqual(Exp3,
  243. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  244. %% match all
  245. Exp4 = [{{a,l,{c,1}},self(),1},
  246. {{c,l,{c,1}},self(),1},
  247. {{n,l,{n,1}},self(),x},
  248. {{n,l,{n,2}},self(),y},
  249. {{p,l,{p,1}},self(),x},
  250. {{p,l,{p,2}},self(),y}
  251. ],
  252. ?assertEqual(Exp4,
  253. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  254. %% match on pid
  255. ?assertEqual(Exp4,
  256. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  257. gproc:table(all), P =:= self()]))),
  258. ?assertEqual(Exp4,
  259. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  260. gproc:table(all), P == self()]))).
  261. t_get_env() ->
  262. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  263. ?assertEqual(true, os:putenv("SSSS", "s2")),
  264. ?assertEqual(true, os:putenv("TTTT", "s3")),
  265. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  266. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  267. %%
  268. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  269. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  270. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  271. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  272. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  273. %%
  274. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  275. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  276. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  277. {t, foo}, 3}])),
  278. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  279. t_get_set_env() ->
  280. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  281. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  282. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  283. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  284. t_set_env() ->
  285. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  286. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  287. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  288. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  289. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  290. %%
  291. ?assertEqual(true, os:putenv("SSSS", "s0")),
  292. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  293. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  294. ?assertEqual("s1", os:getenv("SSSS")),
  295. ?assertEqual(true, os:putenv("SSSS", "s0")),
  296. ?assertEqual([{self(),"s1"}],
  297. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  298. %%
  299. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  300. [{ram_copies,[node()]}])),
  301. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  302. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  303. {t_set_env,a},3}])),
  304. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  305. %% non-existing mnesia obj
  306. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  307. {t_set_env,b},3}])),
  308. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  309. t_get_env_inherit() ->
  310. P = spawn_link(fun() ->
  311. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  312. gproc:reg({n,l,get_env_p}),
  313. t_loop()
  314. end),
  315. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  316. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  317. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, {n,l,get_env_p}}])),
  318. ?assertEqual(ok, t_call(P, die)).
  319. t_loop() ->
  320. receive
  321. {From, {give_away, Key}} ->
  322. ?assertEqual(From, gproc:give_away(Key, From)),
  323. From ! {self(), ok},
  324. t_loop();
  325. {From, die} ->
  326. From ! {self(), ok}
  327. end.
  328. t_call(P, Msg) ->
  329. P ! {self(), Msg},
  330. receive
  331. {P, Reply} ->
  332. Reply
  333. end.
  334. spawn_helper() ->
  335. Parent = self(),
  336. P = spawn(fun() ->
  337. ?assert(gproc:reg({n,l,self()}) =:= true),
  338. Ref = erlang:monitor(process, Parent),
  339. Parent ! {ok,self()},
  340. receive
  341. {'DOWN', Ref, _, _, _} ->
  342. ok
  343. end
  344. end),
  345. receive
  346. {ok,P} ->
  347. P
  348. end.
  349. give_gproc_some_time(T) ->
  350. timer:sleep(T),
  351. sys:get_status(gproc).
  352. -endif.