gproc_tests.erl 24 KB

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