gproc_tests.erl 25 KB

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