gproc_tests.erl 37 KB

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