gproc_tests.erl 26 KB

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