gproc_tests.erl 37 KB

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