gproc_tests.erl 24 KB

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