gproc_tests.erl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
  17. %%
  18. -module(gproc_tests).
  19. -ifdef(TEST).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("stdlib/include/qlc.hrl").
  22. -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. {_Pid,Ref} = spawn_monitor(
  258. fun() ->
  259. exit(?assert(
  260. gproc:await({n,l,t_await}) =:= {Me,val}))
  261. end),
  262. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  263. receive
  264. {'DOWN', Ref, _, _, R} ->
  265. ?assertEqual(R, ok)
  266. after 10000 ->
  267. erlang:error(timeout)
  268. end.
  269. t_await_self() ->
  270. Me = self(),
  271. Ref = gproc:nb_wait({n, l, t_await_self}),
  272. ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
  273. ?assertEqual(true, receive
  274. {gproc, Ref, R, Wh} ->
  275. {registered, {{n, l, t_await_self},
  276. Me, some_value}} = {R, Wh},
  277. true
  278. after 10000 ->
  279. timeout
  280. end).
  281. t_await_crash() ->
  282. Name = {n,l,{dummy,?LINE}},
  283. {Pid,_} = spawn_regger(Name),
  284. ?assertEqual({Pid,undefined}, gproc:await(Name, 1000)),
  285. exit(Pid, kill),
  286. {NewPid,MRef} = spawn_regger(Name),
  287. ?assertEqual(false, is_process_alive(Pid)),
  288. ?assertEqual({NewPid,undefined}, gproc:await(Name, 1000)),
  289. exit(NewPid, kill),
  290. receive {'DOWN', MRef, _, _, _} -> ok end.
  291. spawn_regger(Name) ->
  292. spawn_monitor(fun() ->
  293. gproc:reg(Name),
  294. timer:sleep(60000)
  295. end).
  296. t_is_clean() ->
  297. sys:get_status(gproc), % in order to synch
  298. sys:get_status(gproc_monitor),
  299. T = ets:tab2list(gproc),
  300. Tm = ets:tab2list(gproc_monitor),
  301. ?assertMatch([], Tm),
  302. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
  303. t_simple_mreg() ->
  304. P = self(),
  305. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  306. {bar, bar_val}])),
  307. ?assertEqual(P, gproc:where({n,l,foo})),
  308. ?assertEqual(P, gproc:where({n,l,bar})),
  309. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  310. t_mreg_props() ->
  311. P = self(),
  312. ?assertEqual(true, gproc:mreg(p, l, [{p, v}])),
  313. ?assertEqual(v, gproc:get_value({p,l,p})),
  314. %% Force a context switch, since gproc:monitor_me() is asynchronous
  315. _ = sys:get_status(gproc),
  316. {monitors, Mons} = process_info(whereis(gproc), monitors),
  317. ?assertEqual(true, lists:keymember(P, 2, Mons)).
  318. t_gproc_crash() ->
  319. P = spawn_helper(),
  320. ?assert(gproc:where({n,l,P}) =:= P),
  321. exit(whereis(gproc), kill),
  322. give_gproc_some_time(100),
  323. ?assert(whereis(gproc) =/= undefined),
  324. %%
  325. %% Check that the registration is still there using an ets:lookup(),
  326. %% Once we've killed the process, gproc will always return undefined
  327. %% if the process is not alive, regardless of whether the registration
  328. %% is still there. So, here, the lookup should find something...
  329. %%
  330. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  331. ?assert(gproc:where({n,l,P}) =:= P),
  332. exit(P, kill),
  333. %% ...and here, it shouldn't.
  334. %% (sleep for a while first to let gproc handle the EXIT
  335. give_gproc_some_time(10),
  336. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  337. t_cancel_wait_and_register() ->
  338. Alias = {n, l, foo},
  339. Me = self(),
  340. P = spawn(fun() ->
  341. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  342. ?assert(element(1,sys:get_status(gproc)) == status),
  343. Me ! {self(), go_ahead},
  344. timer:sleep(infinity)
  345. end),
  346. receive
  347. {P, go_ahead} ->
  348. ?assertEqual(gproc:reg(Alias, undefined), true),
  349. exit(P, kill),
  350. timer:sleep(500),
  351. ?assert(element(1,sys:get_status(gproc)) == status)
  352. end.
  353. t_give_away_to_pid() ->
  354. From = {n, l, foo},
  355. Me = self(),
  356. P = spawn_link(fun t_loop/0),
  357. ?assertEqual(true, gproc:reg(From, undefined)),
  358. ?assertEqual(Me, gproc:where(From)),
  359. ?assertEqual(P, gproc:give_away(From, P)),
  360. ?assertEqual(P, gproc:where(From)),
  361. ?assertEqual(ok, t_lcall(P, die)).
  362. t_give_away_to_self() ->
  363. From = {n, l, foo},
  364. Me = self(),
  365. ?assertEqual(true, gproc:reg(From, undefined)),
  366. ?assertEqual(Me, gproc:where(From)),
  367. ?assertEqual(Me, gproc:give_away(From, Me)),
  368. ?assertEqual(Me, gproc:where(From)),
  369. ?assertEqual(true, gproc:unreg(From)).
  370. t_give_away_badarg() ->
  371. From = {n, l, foo},
  372. Me = self(),
  373. ?assertEqual(undefined, gproc:where(From)),
  374. ?assertError(badarg, gproc:give_away(From, Me)).
  375. t_give_away_to_unknown() ->
  376. From = {n, l, foo},
  377. Unknown = {n, l, unknown},
  378. Me = self(),
  379. ?assertEqual(true, gproc:reg(From, undefined)),
  380. ?assertEqual(Me, gproc:where(From)),
  381. ?assertEqual(undefined, gproc:where(Unknown)),
  382. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  383. ?assertEqual(undefined, gproc:where(From)).
  384. t_give_away_and_back() ->
  385. From = {n, l, foo},
  386. Me = self(),
  387. P = spawn_link(fun t_loop/0),
  388. ?assertEqual(true, gproc:reg(From, undefined)),
  389. ?assertEqual(Me, gproc:where(From)),
  390. ?assertEqual(P, gproc:give_away(From, P)),
  391. ?assertEqual(P, gproc:where(From)),
  392. ?assertEqual(ok, t_lcall(P, {give_away, From})),
  393. ?assertEqual(Me, gproc:where(From)),
  394. ?assertEqual(ok, t_lcall(P, die)).
  395. t_select() ->
  396. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  397. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  398. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  399. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  400. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  401. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  402. %% local names
  403. ?assertEqual(
  404. [{{n,l,{n,1}},self(),x},
  405. {{n,l,{n,2}},self(),y}], gproc:select(
  406. {local,names},
  407. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  408. %% mactch local names on value
  409. ?assertEqual(
  410. [{{n,l,{n,1}},self(),x}], gproc:select(
  411. {local,names},
  412. [{{{n,l,'_'},'_',x},[],['$_']}])),
  413. %% match all on value
  414. ?assertEqual(
  415. [{{n,l,{n,1}},self(),x},
  416. {{p,l,{p,1}},self(),x}], gproc:select(
  417. {all,all},
  418. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  419. %% match all on pid
  420. ?assertEqual(
  421. [{{a,l,{c,1}},self(),1},
  422. {{c,l,{c,1}},self(),1},
  423. {{n,l,{n,1}},self(),x},
  424. {{n,l,{n,2}},self(),y},
  425. {{p,l,{p,1}},self(),x},
  426. {{p,l,{p,2}},self(),y}
  427. ], gproc:select(
  428. {all,all},
  429. [{{'_',self(),'_'},[],['$_']}])).
  430. t_select_count() ->
  431. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  432. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  433. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  434. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  435. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  436. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  437. %% local names
  438. ?assertEqual(2, gproc:select_count(
  439. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  440. %% mactch local names on value
  441. ?assertEqual(1, gproc:select_count(
  442. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  443. %% match all on value
  444. ?assertEqual(2, gproc:select_count(
  445. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  446. %% match all on pid
  447. ?assertEqual(6, gproc:select_count(
  448. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  449. t_qlc() ->
  450. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  451. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  452. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  453. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  454. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  455. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  456. %% local names
  457. Exp1 = [{{n,l,{n,1}},self(),x},
  458. {{n,l,{n,2}},self(),y}],
  459. ?assertEqual(Exp1,
  460. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  461. ?assertEqual(Exp1,
  462. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  463. %% mactch local names on value
  464. Exp2 = [{{n,l,{n,1}},self(),x}],
  465. ?assertEqual(Exp2,
  466. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  467. %% match all on value
  468. Exp3 = [{{n,l,{n,1}},self(),x},
  469. {{p,l,{p,1}},self(),x}],
  470. ?assertEqual(Exp3,
  471. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  472. %% match all
  473. Exp4 = [{{a,l,{c,1}},self(),1},
  474. {{c,l,{c,1}},self(),1},
  475. {{n,l,{n,1}},self(),x},
  476. {{n,l,{n,2}},self(),y},
  477. {{p,l,{p,1}},self(),x},
  478. {{p,l,{p,2}},self(),y}
  479. ],
  480. ?assertEqual(Exp4,
  481. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  482. %% match on pid
  483. ?assertEqual(Exp4,
  484. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  485. gproc:table(all), P =:= self()]))),
  486. ?assertEqual(Exp4,
  487. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  488. gproc:table(all), P == self()]))).
  489. t_qlc_dead() ->
  490. P0 = self(),
  491. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  492. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  493. P1 = spawn(fun() ->
  494. Ref = erlang:monitor(process, P0),
  495. gproc:reg({n, l, {n,2}}, y),
  496. gproc:reg({p, l, {p,2}}, y),
  497. P0 ! {self(), ok},
  498. receive
  499. {_P, goodbye} -> ok;
  500. {'DOWN', Ref, _, _, _} ->
  501. ok
  502. end
  503. end),
  504. receive {P1, ok} -> ok end,
  505. %% now, suspend gproc so it doesn't do cleanup
  506. try sys:suspend(gproc),
  507. exit(P1, kill),
  508. %% local names
  509. Exp1 = [{{n,l,{n,1}},self(),x}],
  510. ?assertEqual(Exp1,
  511. qlc:e(qlc:q([N || N <-
  512. gproc:table(names, [check_pids])]))),
  513. ?assertEqual(Exp1,
  514. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <-
  515. gproc:table(names, [check_pids])]))),
  516. %% match local names on value
  517. Exp2 = [{{n,l,{n,1}},self(),x}],
  518. ?assertEqual(Exp2,
  519. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <-
  520. gproc:table(names, [check_pids])]))),
  521. ?assertEqual([],
  522. qlc:e(qlc:q([N || {{n,l,_},_,y} = N <-
  523. gproc:table(names, [check_pids])]))),
  524. %% match all on value
  525. Exp3 = [{{n,l,{n,1}},self(),x},
  526. {{p,l,{p,1}},self(),x}],
  527. ?assertEqual(Exp3,
  528. qlc:e(qlc:q([N || {_,_,x} = N <-
  529. gproc:table(all, [check_pids])]))),
  530. ?assertEqual([],
  531. qlc:e(qlc:q([N || {_,_,y} = N <-
  532. gproc:table(all, [check_pids])]))),
  533. Exp3b = [{{n,l,{n,2}},P1,y},
  534. {{p,l,{p,2}},P1,y}],
  535. ?assertEqual(Exp3b,
  536. qlc:e(qlc:q([N || {_,_,y} = N <-
  537. gproc:table(all)]))),
  538. %% match all
  539. Exp4 = [{{n,l,{n,1}},self(),x},
  540. {{p,l,{p,1}},self(),x}],
  541. ?assertEqual(Exp4,
  542. qlc:e(qlc:q([X || X <-
  543. gproc:table(all, [check_pids])]))),
  544. %% match on pid
  545. ?assertEqual(Exp4,
  546. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  547. gproc:table(all, [check_pids]),
  548. P =:= self()]))),
  549. ?assertEqual([],
  550. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  551. gproc:table(all, [check_pids]),
  552. P =:= P1]))),
  553. Exp4b = [{{n,l,{n,2}},P1,y},
  554. {{p,l,{p,2}},P1,y}],
  555. ?assertEqual(Exp4b,
  556. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  557. gproc:table(all),
  558. P =:= P1])))
  559. after
  560. sys:resume(gproc)
  561. end.
  562. t_get_env() ->
  563. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  564. ?assertEqual(true, os:putenv("SSSS", "s2")),
  565. ?assertEqual(true, os:putenv("TTTT", "s3")),
  566. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  567. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  568. %%
  569. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  570. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  571. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  572. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  573. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  574. %%
  575. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  576. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  577. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  578. {t, foo}, 3}])),
  579. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  580. t_get_set_env() ->
  581. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  582. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  583. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  584. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  585. t_set_env() ->
  586. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  587. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  588. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  589. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  590. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  591. %%
  592. ?assertEqual(true, os:putenv("SSSS", "s0")),
  593. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  594. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  595. ?assertEqual("s1", os:getenv("SSSS")),
  596. ?assertEqual(true, os:putenv("SSSS", "s0")),
  597. ?assertEqual([{self(),"s1"}],
  598. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  599. %%
  600. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  601. [{ram_copies,[node()]}])),
  602. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  603. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  604. {t_set_env,a},3}])),
  605. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  606. %% non-existing mnesia obj
  607. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  608. {t_set_env,b},3}])),
  609. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  610. t_get_env_inherit() ->
  611. P = spawn_link(fun() ->
  612. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  613. gproc:reg({n,l,get_env_p}),
  614. t_loop()
  615. end),
  616. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  617. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  618. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  619. [{inherit, {n,l,get_env_p}}])),
  620. ?assertEqual(ok, t_lcall(P, die)).
  621. %% What we test here is that we return the same current_function as the
  622. %% process_info() BIF. As we parse the backtrace dump, we check with some
  623. %% weirdly named functions.
  624. t_gproc_info() ->
  625. {A,B} = '-t1-'(),
  626. ?assertEqual(A,B),
  627. {C,D} = '\'t2'(),
  628. ?assertEqual(C,D),
  629. {E,F} = '\'t3\''(),
  630. ?assertEqual(E,F),
  631. {G,H} = t4(),
  632. ?assertEqual(G,H).
  633. '-t1-'() ->
  634. {_, I0} = process_info(self(), current_function),
  635. {_, I} = gproc:info(self(), current_function),
  636. {I0, I}.
  637. '\'t2'() ->
  638. {_, I0} = process_info(self(), current_function),
  639. {_, I} = gproc:info(self(), current_function),
  640. {I0, I}.
  641. '\'t3\''() ->
  642. {_, I0} = process_info(self(), current_function),
  643. {_, I} = gproc:info(self(), current_function),
  644. {I0, I}.
  645. t4() ->
  646. {_, I0} = process_info(self(), current_function),
  647. {_, I} = gproc:info(self(), current_function),
  648. {I0, I}.
  649. t_monitor() ->
  650. Me = self(),
  651. P = spawn_link(fun() ->
  652. gproc:reg({n,l,a}),
  653. Me ! continue,
  654. t_loop()
  655. end),
  656. receive continue ->
  657. ok
  658. end,
  659. Ref = gproc:monitor({n,l,a}),
  660. ?assertEqual(ok, t_lcall(P, die)),
  661. receive
  662. M ->
  663. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  664. end.
  665. t_monitor_give_away() ->
  666. Me = self(),
  667. P = spawn_link(fun() ->
  668. gproc:reg({n,l,a}),
  669. Me ! continue,
  670. t_loop()
  671. end),
  672. receive continue ->
  673. ok
  674. end,
  675. Ref = gproc:monitor({n,l,a}),
  676. ?assertEqual(ok, t_lcall(P, {give_away, {n,l,a}})),
  677. receive
  678. M ->
  679. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  680. end,
  681. ?assertEqual(ok, t_lcall(P, die)).
  682. t_monitor_standby() ->
  683. Me = self(),
  684. P = spawn(fun() ->
  685. gproc:reg({n,l,a}),
  686. Me ! continue,
  687. t_loop()
  688. end),
  689. receive continue ->
  690. ok
  691. end,
  692. Ref = gproc:monitor({n,l,a}, standby),
  693. exit(P, kill),
  694. receive
  695. M ->
  696. ?assertEqual({gproc,{failover,Me},Ref,{n,l,a}}, M)
  697. end,
  698. gproc:unreg({n,l,a}),
  699. ok.
  700. t_monitor_follow() ->
  701. Name = ?T_NAME,
  702. P1 = t_spawn(_Selective = true),
  703. Ref = t_call(P1, {apply, gproc, monitor, [Name, follow]}),
  704. {gproc,unreg,Ref,Name} = got_msg(P1),
  705. %% gproc_lib:dbg([gproc,gproc_lib]),
  706. P2 = t_spawn_reg(Name),
  707. {gproc,registered,Ref,Name} = got_msg(P1),
  708. exit(P2, kill),
  709. {gproc,unreg,Ref,Name} = got_msg(P1),
  710. P3 = t_spawn(true),
  711. Ref3 = t_call(P3, {apply, gproc, monitor, [Name, standby]}),
  712. {gproc,{failover,P3},Ref,Name} = got_msg(P1),
  713. {gproc,{failover,P3},Ref3,Name} = got_msg(P3),
  714. [exit(P,kill) || P <- [P1,P3]],
  715. ok.
  716. t_subscribe() ->
  717. Key = {n,l,a},
  718. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  719. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  720. P = spawn_link(fun() ->
  721. gproc:reg({n,l,a}),
  722. t_loop()
  723. end),
  724. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  725. ?assertEqual(ok, t_lcall(P, {give_away, Key})),
  726. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  727. gproc:give_away(Key, P),
  728. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  729. ?assertEqual(ok, t_lcall(P, die)),
  730. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  731. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  732. t_simple_pool()->
  733. Key = p1w1,
  734. From = {n,l,Key},
  735. P = spawn_link(fun() ->
  736. t_loop()
  737. end),
  738. P ! {self(), {reg, From}},
  739. receive
  740. {_,Registered} ->
  741. ?assertEqual(Registered, true)
  742. after 5000 ->
  743. ?debugFmt("registration timeout ", []),
  744. ok
  745. end,
  746. %% create a new pool
  747. ?assertEqual(gproc_pool:new(p1), ok),
  748. %% add a worker to it
  749. ?assertEqual(gproc_pool:add_worker(p1,Key) , 1 ),
  750. ?assertEqual( length(gproc_pool:worker_pool(p1) ), 1),
  751. %% but it should not be active as yet
  752. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  753. ?assert( gproc_pool:pick(p1) =:= false ),
  754. %% connect to make the worker active
  755. ?assertEqual(gproc_pool:connect_worker(p1,Key) , true ),
  756. %% it should be active now
  757. ?assertEqual( length( gproc_pool:active_workers(p1)), 1),
  758. ?assertEqual( gproc_pool:pick(p1) , {n,l,[gproc_pool,p1,1,Key]}),
  759. gproc:send(From, {self(), die}),
  760. receive
  761. {_,Returned}=X ->
  762. ?assertEqual(Returned, ok)
  763. after 5000 ->
  764. %% the next 3 tests should fail if the worker is still alive
  765. ok
  766. end,
  767. %% disconnect the worker from the pool.
  768. ?assertEqual(gproc_pool:disconnect_worker(p1,Key), true),
  769. %% there should be no active workers now
  770. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  771. %% remove the worker from the pool
  772. ?assertEqual(gproc_pool:remove_worker(p1,Key), true),
  773. %% there should be no workers now
  774. %% NOTE: value of worker_pool seems to vary after removing workers
  775. %% sometimes [1,2] , sometimes [1], and then []
  776. %% so relying on defined_workers
  777. ?assertEqual( length(gproc_pool:defined_workers(p1)), 0 ),
  778. ?assertEqual( length(gproc_pool:worker_pool(p1)), 0 ),
  779. %% should be able to delete the pool now
  780. ?assertEqual( gproc_pool:delete(p1), ok).
  781. get_msg() ->
  782. receive M ->
  783. M
  784. after 1000 ->
  785. timeout
  786. end.
  787. %% t_spawn() -> gproc_test_lib:t_spawn(node()).
  788. t_spawn(Sel) -> gproc_test_lib:t_spawn(node(), Sel).
  789. t_spawn_reg(N) -> gproc_test_lib:t_spawn_reg(node(), N).
  790. t_call(P, Req) -> gproc_test_lib:t_call(P, Req).
  791. %% got_msg(P, M) -> gproc_test_lib:got_msg(P, M).
  792. got_msg(P) -> gproc_test_lib:got_msg(P).
  793. t_loop() ->
  794. receive
  795. {From, {give_away, Key}} ->
  796. ?assertEqual(From, gproc:give_away(Key, From)),
  797. From ! {self(), ok},
  798. t_loop();
  799. {From, die} ->
  800. From ! {self(), ok};
  801. {From, {reg, Name}} ->
  802. From ! {self(), gproc:reg(Name,undefined)},
  803. t_loop();
  804. {From, {unreg, Name}} ->
  805. From ! {self(), gproc:unreg(Name)},
  806. t_loop()
  807. end.
  808. t_lcall(P, Msg) ->
  809. P ! {self(), Msg},
  810. receive
  811. {P, Reply} ->
  812. Reply
  813. end.
  814. spawn_helper() ->
  815. Parent = self(),
  816. P = spawn(fun() ->
  817. ?assert(gproc:reg({n,l,self()}) =:= true),
  818. Ref = erlang:monitor(process, Parent),
  819. Parent ! {ok,self()},
  820. receive
  821. {'DOWN', Ref, _, _, _} ->
  822. ok
  823. end
  824. end),
  825. receive
  826. {ok,P} ->
  827. P
  828. end.
  829. give_gproc_some_time(T) ->
  830. timer:sleep(T),
  831. sys:get_status(gproc).
  832. -endif.