gproc_tests.erl 28 KB

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