gproc_tests.erl 31 KB

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