gproc_tests.erl 35 KB

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