gproc_tests.erl 28 KB

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