gproc_tests.erl 12 KB

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