gproc_tests.erl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. conf_test_() ->
  23. {foreach,
  24. fun() ->
  25. application:unload(gproc)
  26. end,
  27. fun(_) ->
  28. application:stop(gproc)
  29. end,
  30. [?_test(t_server_opts()),
  31. ?_test(t_ets_opts())]}.
  32. t_server_opts() ->
  33. H = 10000,
  34. application:set_env(gproc, server_options, [{min_heap_size, H}]),
  35. ?assert(ok == application:start(gproc)),
  36. {min_heap_size, H1} = process_info(whereis(gproc), min_heap_size),
  37. ?assert(is_integer(H1) andalso H1 > H).
  38. t_ets_opts() ->
  39. %% Cannot inspect the write_concurrency attribute on an ets table in
  40. %% any easy way, so trace on the ets:new/2 call and check the arguments.
  41. application:set_env(gproc, ets_options, [{write_concurrency, false}]),
  42. erlang:trace_pattern({ets,new, 2}, [{[gproc,'_'],[],[]}], [global]),
  43. erlang:trace(new, true, [call]),
  44. ?assert(ok == application:start(gproc)),
  45. erlang:trace(new, false, [call]),
  46. receive
  47. {trace,_,call,{ets,new,[gproc,Opts]}} ->
  48. ?assertMatch({write_concurrency, false},
  49. lists:keyfind(write_concurrency,1,Opts))
  50. after 3000 ->
  51. error(timeout)
  52. end.
  53. reg_test_() ->
  54. {setup,
  55. fun() ->
  56. application:start(gproc),
  57. application:start(mnesia)
  58. end,
  59. fun(_) ->
  60. application:stop(gproc),
  61. application:stop(mnesia)
  62. end,
  63. [
  64. {spawn, ?_test(?debugVal(t_simple_reg()))}
  65. , ?_test(t_is_clean())
  66. , {spawn, ?_test(?debugVal(t_simple_reg_or_locate()))}
  67. , ?_test(t_is_clean())
  68. , {spawn, ?_test(?debugVal(t_reg_or_locate2()))}
  69. , ?_test(t_is_clean())
  70. , {spawn, ?_test(?debugVal(t_reg_or_locate3()))}
  71. , ?_test(t_is_clean())
  72. , {spawn, ?_test(?debugVal(t_simple_counter()))}
  73. , ?_test(t_is_clean())
  74. , {spawn, ?_test(?debugVal(t_simple_aggr_counter()))}
  75. , ?_test(t_is_clean())
  76. , {spawn, ?_test(?debugVal(t_update_counters()))}
  77. , ?_test(t_is_clean())
  78. , {spawn, ?_test(?debugVal(t_simple_prop()))}
  79. , ?_test(t_is_clean())
  80. , {spawn, ?_test(?debugVal(t_await()))}
  81. , ?_test(t_is_clean())
  82. , {spawn, ?_test(?debugVal(t_await_self()))}
  83. , ?_test(t_is_clean())
  84. , {spawn, ?_test(?debugVal(t_await_crash()))}
  85. , ?_test(t_is_clean())
  86. , {spawn, ?_test(?debugVal(t_simple_mreg()))}
  87. , ?_test(t_is_clean())
  88. , {spawn, ?_test(?debugVal(t_mreg_props()))}
  89. , ?_test(t_is_clean())
  90. , {spawn, ?_test(?debugVal(t_gproc_crash()))}
  91. , ?_test(t_is_clean())
  92. , {spawn, ?_test(?debugVal(t_cancel_wait_and_register()))}
  93. , ?_test(t_is_clean())
  94. , {spawn, ?_test(?debugVal(t_give_away_to_pid()))}
  95. , ?_test(t_is_clean())
  96. , {spawn, ?_test(?debugVal(t_give_away_to_self()))}
  97. , ?_test(t_is_clean())
  98. , {spawn, ?_test(?debugVal(t_give_away_badarg()))}
  99. , ?_test(t_is_clean())
  100. , {spawn, ?_test(?debugVal(t_give_away_to_unknown()))}
  101. , ?_test(t_is_clean())
  102. , {spawn, ?_test(?debugVal(t_give_away_and_back()))}
  103. , ?_test(t_is_clean())
  104. , {spawn, ?_test(?debugVal(t_select()))}
  105. , ?_test(t_is_clean())
  106. , {spawn, ?_test(?debugVal(t_select_count()))}
  107. , ?_test(t_is_clean())
  108. , {spawn, ?_test(?debugVal(t_qlc()))}
  109. , ?_test(t_is_clean())
  110. , {spawn, ?_test(?debugVal(t_qlc_dead()))}
  111. , ?_test(t_is_clean())
  112. , {spawn, ?_test(?debugVal(t_get_env()))}
  113. , ?_test(t_is_clean())
  114. , {spawn, ?_test(?debugVal(t_get_set_env()))}
  115. , ?_test(t_is_clean())
  116. , {spawn, ?_test(?debugVal(t_set_env()))}
  117. , ?_test(t_is_clean())
  118. , {spawn, ?_test(?debugVal(t_get_env_inherit()))}
  119. , ?_test(t_is_clean())
  120. , {spawn, ?_test(?debugVal(t_monitor()))}
  121. , ?_test(t_is_clean())
  122. , {spawn, ?_test(?debugVal(t_monitor_give_away()))}
  123. , ?_test(t_is_clean())
  124. , {spawn, ?_test(?debugVal(t_subscribe()))}
  125. , ?_test(t_is_clean())
  126. , {spawn, ?_test(?debugVal(t_gproc_info()))}
  127. , ?_test(t_is_clean())
  128. ]}.
  129. t_simple_reg() ->
  130. ?assert(gproc:reg({n,l,name}) =:= true),
  131. ?assert(gproc:where({n,l,name}) =:= self()),
  132. ?assert(gproc:unreg({n,l,name}) =:= true),
  133. ?assert(gproc:where({n,l,name}) =:= undefined).
  134. t_simple_reg_or_locate() ->
  135. P = self(),
  136. ?assertMatch({P, undefined}, gproc:reg_or_locate({n,l,name})),
  137. ?assertMatch(P, gproc:where({n,l,name})),
  138. ?assertMatch({P, my_val}, gproc:reg_or_locate({n,l,name2}, my_val)),
  139. ?assertMatch(my_val, gproc:get_value({n,l,name2})).
  140. t_reg_or_locate2() ->
  141. P = self(),
  142. {P1,R1} = spawn_monitor(fun() ->
  143. Ref = erlang:monitor(process, P),
  144. gproc:reg({n,l,foo}, the_value),
  145. P ! {self(), ok},
  146. receive
  147. {'DOWN',Ref,_,_,_} -> ok
  148. end
  149. end),
  150. receive {P1, ok} -> ok end,
  151. ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
  152. exit(P1, kill),
  153. receive
  154. {'DOWN',R1,_,_,_} ->
  155. ok
  156. end.
  157. t_reg_or_locate3() ->
  158. P = self(),
  159. {P1, Value} = gproc:reg_or_locate(
  160. {n,l,foo}, the_value,
  161. fun() ->
  162. P ! {self(), ok},
  163. receive
  164. {'DOWN',_Ref,_,_,_} -> ok
  165. end
  166. end),
  167. ?assert(P =/= P1),
  168. ?assert(Value =:= the_value),
  169. _Ref = erlang:monitor(process, P1),
  170. receive
  171. {P1, ok} -> ok;
  172. {'DOWN', _Ref, _, _, _Reason} ->
  173. ?assert(process_died_unexpectedly)
  174. end,
  175. ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
  176. exit(P1, kill),
  177. receive
  178. {'DOWN',_R1,_,_,_} ->
  179. ok
  180. end.
  181. t_simple_counter() ->
  182. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  183. ?assert(gproc:get_value({c,l,c1}) =:= 3),
  184. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  185. ?assert(gproc:reset_counter({c,l,c1}) =:= {7, 3}).
  186. t_simple_aggr_counter() ->
  187. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  188. ?assert(gproc:reg({a,l,c1}) =:= true),
  189. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  190. P = self(),
  191. P1 = spawn_link(fun() ->
  192. gproc:reg({c,l,c1}, 5),
  193. P ! {self(), ok},
  194. receive
  195. {P, goodbye} -> ok
  196. end
  197. end),
  198. receive {P1, ok} -> ok end,
  199. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  200. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  201. ?assert(gproc:get_value({a,l,c1}) =:= 12),
  202. P1 ! {self(), goodbye},
  203. R = erlang:monitor(process, P1),
  204. receive {'DOWN', R, _, _, _} ->
  205. gproc:audit_process(P1)
  206. end,
  207. ?assert(gproc:get_value({a,l,c1}) =:= 7).
  208. t_update_counters() ->
  209. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  210. ?assert(gproc:reg({a,l,c1}) =:= true),
  211. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  212. P = self(),
  213. P1 = spawn_link(fun() ->
  214. gproc:reg({c,l,c1}, 5),
  215. P ! {self(), ok},
  216. receive
  217. {P, goodbye} -> ok
  218. end
  219. end),
  220. receive {P1, ok} -> ok end,
  221. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  222. Me = self(),
  223. ?assertEqual([{{c,l,c1},Me,7},
  224. {{c,l,c1},P1,8}], gproc:update_counters(l, [{{c,l,c1}, Me, 4},
  225. {{c,l,c1}, P1, 3}])),
  226. ?assert(gproc:get_value({a,l,c1}) =:= 15),
  227. P1 ! {self(), goodbye},
  228. R = erlang:monitor(process, P1),
  229. receive {'DOWN', R, _, _, _} ->
  230. gproc:audit_process(P1)
  231. end,
  232. ?assert(gproc:get_value({a,l,c1}) =:= 7).
  233. t_simple_prop() ->
  234. ?assert(gproc:reg({p,l,prop}) =:= true),
  235. ?assert(t_other_proc(fun() ->
  236. ?assert(gproc:reg({p,l,prop}) =:= true)
  237. end) =:= ok),
  238. ?assert(gproc:unreg({p,l,prop}) =:= true).
  239. t_other_proc(F) ->
  240. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  241. receive
  242. {'DOWN',Ref,_,_,R} ->
  243. R
  244. after 10000 ->
  245. erlang:error(timeout)
  246. end.
  247. t_await() ->
  248. Me = self(),
  249. {_Pid,Ref} = spawn_monitor(
  250. fun() ->
  251. exit(?assert(
  252. gproc:await({n,l,t_await}) =:= {Me,val}))
  253. end),
  254. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  255. receive
  256. {'DOWN', Ref, _, _, R} ->
  257. ?assertEqual(R, ok)
  258. after 10000 ->
  259. erlang:error(timeout)
  260. end.
  261. t_await_self() ->
  262. Me = self(),
  263. Ref = gproc:nb_wait({n, l, t_await_self}),
  264. ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
  265. ?assertEqual(true, receive
  266. {gproc, Ref, R, Wh} ->
  267. {registered, {{n, l, t_await_self},
  268. Me, some_value}} = {R, Wh},
  269. true
  270. after 10000 ->
  271. timeout
  272. end).
  273. t_await_crash() ->
  274. Name = {n,l,{dummy,?LINE}},
  275. {Pid,_} = spawn_regger(Name),
  276. ?assertEqual({Pid,undefined}, gproc:await(Name, 1000)),
  277. exit(Pid, kill),
  278. {NewPid,MRef} = spawn_regger(Name),
  279. ?assertEqual(false, is_process_alive(Pid)),
  280. ?assertEqual({NewPid,undefined}, gproc:await(Name, 1000)),
  281. exit(NewPid, kill),
  282. receive {'DOWN', MRef, _, _, _} -> ok end.
  283. spawn_regger(Name) ->
  284. spawn_monitor(fun() ->
  285. gproc:reg(Name),
  286. timer:sleep(60000)
  287. end).
  288. t_is_clean() ->
  289. sys:get_status(gproc), % in order to synch
  290. sys:get_status(gproc_monitor),
  291. T = ets:tab2list(gproc),
  292. Tm = ets:tab2list(gproc_monitor),
  293. ?assertMatch([], Tm),
  294. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
  295. t_simple_mreg() ->
  296. P = self(),
  297. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  298. {bar, bar_val}])),
  299. ?assertEqual(P, gproc:where({n,l,foo})),
  300. ?assertEqual(P, gproc:where({n,l,bar})),
  301. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  302. t_mreg_props() ->
  303. P = self(),
  304. ?assertEqual(true, gproc:mreg(p, l, [{p, v}])),
  305. ?assertEqual(v, gproc:get_value({p,l,p})),
  306. %% Force a context switch, since gproc:monitor_me() is asynchronous
  307. _ = sys:get_status(gproc),
  308. {monitors, Mons} = process_info(whereis(gproc), monitors),
  309. ?assertEqual(true, lists:keymember(P, 2, Mons)).
  310. t_gproc_crash() ->
  311. P = spawn_helper(),
  312. ?assert(gproc:where({n,l,P}) =:= P),
  313. exit(whereis(gproc), kill),
  314. give_gproc_some_time(100),
  315. ?assert(whereis(gproc) =/= undefined),
  316. %%
  317. %% Check that the registration is still there using an ets:lookup(),
  318. %% Once we've killed the process, gproc will always return undefined
  319. %% if the process is not alive, regardless of whether the registration
  320. %% is still there. So, here, the lookup should find something...
  321. %%
  322. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  323. ?assert(gproc:where({n,l,P}) =:= P),
  324. exit(P, kill),
  325. %% ...and here, it shouldn't.
  326. %% (sleep for a while first to let gproc handle the EXIT
  327. give_gproc_some_time(10),
  328. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  329. t_cancel_wait_and_register() ->
  330. Alias = {n, l, foo},
  331. Me = self(),
  332. P = spawn(fun() ->
  333. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  334. ?assert(element(1,sys:get_status(gproc)) == status),
  335. Me ! {self(), go_ahead},
  336. timer:sleep(infinity)
  337. end),
  338. receive
  339. {P, go_ahead} ->
  340. ?assertEqual(gproc:reg(Alias, undefined), true),
  341. exit(P, kill),
  342. timer:sleep(500),
  343. ?assert(element(1,sys:get_status(gproc)) == status)
  344. end.
  345. t_give_away_to_pid() ->
  346. From = {n, l, foo},
  347. Me = self(),
  348. P = spawn_link(fun t_loop/0),
  349. ?assertEqual(true, gproc:reg(From, undefined)),
  350. ?assertEqual(Me, gproc:where(From)),
  351. ?assertEqual(P, gproc:give_away(From, P)),
  352. ?assertEqual(P, gproc:where(From)),
  353. ?assertEqual(ok, t_call(P, die)).
  354. t_give_away_to_self() ->
  355. From = {n, l, foo},
  356. Me = self(),
  357. ?assertEqual(true, gproc:reg(From, undefined)),
  358. ?assertEqual(Me, gproc:where(From)),
  359. ?assertEqual(Me, gproc:give_away(From, Me)),
  360. ?assertEqual(Me, gproc:where(From)),
  361. ?assertEqual(true, gproc:unreg(From)).
  362. t_give_away_badarg() ->
  363. From = {n, l, foo},
  364. Me = self(),
  365. ?assertEqual(undefined, gproc:where(From)),
  366. ?assertError(badarg, gproc:give_away(From, Me)).
  367. t_give_away_to_unknown() ->
  368. From = {n, l, foo},
  369. Unknown = {n, l, unknown},
  370. Me = self(),
  371. ?assertEqual(true, gproc:reg(From, undefined)),
  372. ?assertEqual(Me, gproc:where(From)),
  373. ?assertEqual(undefined, gproc:where(Unknown)),
  374. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  375. ?assertEqual(undefined, gproc:where(From)).
  376. t_give_away_and_back() ->
  377. From = {n, l, foo},
  378. Me = self(),
  379. P = spawn_link(fun t_loop/0),
  380. ?assertEqual(true, gproc:reg(From, undefined)),
  381. ?assertEqual(Me, gproc:where(From)),
  382. ?assertEqual(P, gproc:give_away(From, P)),
  383. ?assertEqual(P, gproc:where(From)),
  384. ?assertEqual(ok, t_call(P, {give_away, From})),
  385. ?assertEqual(Me, gproc:where(From)),
  386. ?assertEqual(ok, t_call(P, die)).
  387. t_select() ->
  388. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  389. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  390. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  391. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  392. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  393. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  394. %% local names
  395. ?assertEqual(
  396. [{{n,l,{n,1}},self(),x},
  397. {{n,l,{n,2}},self(),y}], gproc:select(
  398. {local,names},
  399. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  400. %% mactch local names on value
  401. ?assertEqual(
  402. [{{n,l,{n,1}},self(),x}], gproc:select(
  403. {local,names},
  404. [{{{n,l,'_'},'_',x},[],['$_']}])),
  405. %% match all on value
  406. ?assertEqual(
  407. [{{n,l,{n,1}},self(),x},
  408. {{p,l,{p,1}},self(),x}], gproc:select(
  409. {all,all},
  410. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  411. %% match all on pid
  412. ?assertEqual(
  413. [{{a,l,{c,1}},self(),1},
  414. {{c,l,{c,1}},self(),1},
  415. {{n,l,{n,1}},self(),x},
  416. {{n,l,{n,2}},self(),y},
  417. {{p,l,{p,1}},self(),x},
  418. {{p,l,{p,2}},self(),y}
  419. ], gproc:select(
  420. {all,all},
  421. [{{'_',self(),'_'},[],['$_']}])).
  422. t_select_count() ->
  423. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  424. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  425. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  426. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  427. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  428. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  429. %% local names
  430. ?assertEqual(2, gproc:select_count(
  431. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  432. %% mactch local names on value
  433. ?assertEqual(1, gproc:select_count(
  434. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  435. %% match all on value
  436. ?assertEqual(2, gproc:select_count(
  437. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  438. %% match all on pid
  439. ?assertEqual(6, gproc:select_count(
  440. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  441. t_qlc() ->
  442. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  443. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  444. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  445. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  446. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  447. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  448. %% local names
  449. Exp1 = [{{n,l,{n,1}},self(),x},
  450. {{n,l,{n,2}},self(),y}],
  451. ?assertEqual(Exp1,
  452. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  453. ?assertEqual(Exp1,
  454. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  455. %% mactch local names on value
  456. Exp2 = [{{n,l,{n,1}},self(),x}],
  457. ?assertEqual(Exp2,
  458. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  459. %% match all on value
  460. Exp3 = [{{n,l,{n,1}},self(),x},
  461. {{p,l,{p,1}},self(),x}],
  462. ?assertEqual(Exp3,
  463. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  464. %% match all
  465. Exp4 = [{{a,l,{c,1}},self(),1},
  466. {{c,l,{c,1}},self(),1},
  467. {{n,l,{n,1}},self(),x},
  468. {{n,l,{n,2}},self(),y},
  469. {{p,l,{p,1}},self(),x},
  470. {{p,l,{p,2}},self(),y}
  471. ],
  472. ?assertEqual(Exp4,
  473. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  474. %% match on pid
  475. ?assertEqual(Exp4,
  476. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  477. gproc:table(all), P =:= self()]))),
  478. ?assertEqual(Exp4,
  479. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  480. gproc:table(all), P == self()]))).
  481. t_qlc_dead() ->
  482. P0 = self(),
  483. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  484. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  485. P1 = spawn(fun() ->
  486. Ref = erlang:monitor(process, P0),
  487. gproc:reg({n, l, {n,2}}, y),
  488. gproc:reg({p, l, {p,2}}, y),
  489. P0 ! {self(), ok},
  490. receive
  491. {_P, goodbye} -> ok;
  492. {'DOWN', Ref, _, _, _} ->
  493. ok
  494. end
  495. end),
  496. receive {P1, ok} -> ok end,
  497. %% now, suspend gproc so it doesn't do cleanup
  498. try sys:suspend(gproc),
  499. exit(P1, kill),
  500. %% local names
  501. Exp1 = [{{n,l,{n,1}},self(),x}],
  502. ?assertEqual(Exp1,
  503. qlc:e(qlc:q([N || N <-
  504. gproc:table(names, [check_pids])]))),
  505. ?assertEqual(Exp1,
  506. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <-
  507. gproc:table(names, [check_pids])]))),
  508. %% match local names on value
  509. Exp2 = [{{n,l,{n,1}},self(),x}],
  510. ?assertEqual(Exp2,
  511. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <-
  512. gproc:table(names, [check_pids])]))),
  513. ?assertEqual([],
  514. qlc:e(qlc:q([N || {{n,l,_},_,y} = N <-
  515. gproc:table(names, [check_pids])]))),
  516. %% match all on value
  517. Exp3 = [{{n,l,{n,1}},self(),x},
  518. {{p,l,{p,1}},self(),x}],
  519. ?assertEqual(Exp3,
  520. qlc:e(qlc:q([N || {_,_,x} = N <-
  521. gproc:table(all, [check_pids])]))),
  522. ?assertEqual([],
  523. qlc:e(qlc:q([N || {_,_,y} = N <-
  524. gproc:table(all, [check_pids])]))),
  525. Exp3b = [{{n,l,{n,2}},P1,y},
  526. {{p,l,{p,2}},P1,y}],
  527. ?assertEqual(Exp3b,
  528. qlc:e(qlc:q([N || {_,_,y} = N <-
  529. gproc:table(all)]))),
  530. %% match all
  531. Exp4 = [{{n,l,{n,1}},self(),x},
  532. {{p,l,{p,1}},self(),x}],
  533. ?assertEqual(Exp4,
  534. qlc:e(qlc:q([X || X <-
  535. gproc:table(all, [check_pids])]))),
  536. %% match on pid
  537. ?assertEqual(Exp4,
  538. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  539. gproc:table(all, [check_pids]),
  540. P =:= self()]))),
  541. ?assertEqual([],
  542. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  543. gproc:table(all, [check_pids]),
  544. P =:= P1]))),
  545. Exp4b = [{{n,l,{n,2}},P1,y},
  546. {{p,l,{p,2}},P1,y}],
  547. ?assertEqual(Exp4b,
  548. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  549. gproc:table(all),
  550. P =:= P1])))
  551. after
  552. sys:resume(gproc)
  553. end.
  554. t_get_env() ->
  555. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  556. ?assertEqual(true, os:putenv("SSSS", "s2")),
  557. ?assertEqual(true, os:putenv("TTTT", "s3")),
  558. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  559. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  560. %%
  561. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  562. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  563. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  564. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  565. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  566. %%
  567. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  568. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  569. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  570. {t, foo}, 3}])),
  571. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  572. t_get_set_env() ->
  573. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  574. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  575. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  576. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  577. t_set_env() ->
  578. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  579. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  580. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  581. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  582. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  583. %%
  584. ?assertEqual(true, os:putenv("SSSS", "s0")),
  585. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  586. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  587. ?assertEqual("s1", os:getenv("SSSS")),
  588. ?assertEqual(true, os:putenv("SSSS", "s0")),
  589. ?assertEqual([{self(),"s1"}],
  590. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  591. %%
  592. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  593. [{ram_copies,[node()]}])),
  594. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  595. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  596. {t_set_env,a},3}])),
  597. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  598. %% non-existing mnesia obj
  599. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  600. {t_set_env,b},3}])),
  601. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  602. t_get_env_inherit() ->
  603. P = spawn_link(fun() ->
  604. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  605. gproc:reg({n,l,get_env_p}),
  606. t_loop()
  607. end),
  608. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  609. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  610. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  611. [{inherit, {n,l,get_env_p}}])),
  612. ?assertEqual(ok, t_call(P, die)).
  613. %% What we test here is that we return the same current_function as the
  614. %% process_info() BIF. As we parse the backtrace dump, we check with some
  615. %% weirdly named functions.
  616. t_gproc_info() ->
  617. {A,B} = '-t1-'(),
  618. ?assertEqual(A,B),
  619. {C,D} = '\'t2'(),
  620. ?assertEqual(C,D),
  621. {E,F} = '\'t3\''(),
  622. ?assertEqual(E,F),
  623. {G,H} = t4(),
  624. ?assertEqual(G,H).
  625. '-t1-'() ->
  626. {_, I0} = process_info(self(), current_function),
  627. {_, I} = gproc:info(self(), current_function),
  628. {I0, I}.
  629. '\'t2'() ->
  630. {_, I0} = process_info(self(), current_function),
  631. {_, I} = gproc:info(self(), current_function),
  632. {I0, I}.
  633. '\'t3\''() ->
  634. {_, I0} = process_info(self(), current_function),
  635. {_, I} = gproc:info(self(), current_function),
  636. {I0, I}.
  637. t4() ->
  638. {_, I0} = process_info(self(), current_function),
  639. {_, I} = gproc:info(self(), current_function),
  640. {I0, I}.
  641. t_monitor() ->
  642. Me = self(),
  643. P = spawn_link(fun() ->
  644. gproc:reg({n,l,a}),
  645. Me ! continue,
  646. t_loop()
  647. end),
  648. receive continue ->
  649. ok
  650. end,
  651. Ref = gproc:monitor({n,l,a}),
  652. ?assertEqual(ok, t_call(P, die)),
  653. receive
  654. M ->
  655. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  656. end.
  657. t_monitor_give_away() ->
  658. Me = self(),
  659. P = spawn_link(fun() ->
  660. gproc:reg({n,l,a}),
  661. Me ! continue,
  662. t_loop()
  663. end),
  664. receive continue ->
  665. ok
  666. end,
  667. Ref = gproc:monitor({n,l,a}),
  668. ?assertEqual(ok, t_call(P, {give_away, {n,l,a}})),
  669. receive
  670. M ->
  671. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  672. end,
  673. ?assertEqual(ok, t_call(P, die)).
  674. t_subscribe() ->
  675. Key = {n,l,a},
  676. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  677. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  678. P = spawn_link(fun() ->
  679. gproc:reg({n,l,a}),
  680. t_loop()
  681. end),
  682. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  683. ?assertEqual(ok, t_call(P, {give_away, Key})),
  684. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  685. gproc:give_away(Key, P),
  686. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  687. ?assertEqual(ok, t_call(P, die)),
  688. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  689. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  690. get_msg() ->
  691. receive M ->
  692. M
  693. after 1000 ->
  694. timeout
  695. end.
  696. t_loop() ->
  697. receive
  698. {From, {give_away, Key}} ->
  699. ?assertEqual(From, gproc:give_away(Key, From)),
  700. From ! {self(), ok},
  701. t_loop();
  702. {From, die} ->
  703. From ! {self(), ok}
  704. end.
  705. t_call(P, Msg) ->
  706. P ! {self(), Msg},
  707. receive
  708. {P, Reply} ->
  709. Reply
  710. end.
  711. spawn_helper() ->
  712. Parent = self(),
  713. P = spawn(fun() ->
  714. ?assert(gproc:reg({n,l,self()}) =:= true),
  715. Ref = erlang:monitor(process, Parent),
  716. Parent ! {ok,self()},
  717. receive
  718. {'DOWN', Ref, _, _, _} ->
  719. ok
  720. end
  721. end),
  722. receive
  723. {ok,P} ->
  724. P
  725. end.
  726. give_gproc_some_time(T) ->
  727. timer:sleep(T),
  728. sys:get_status(gproc).
  729. -endif.