gproc_tests.erl 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. ?debugFmt("self() = ~p~n", [self()]),
  448. ?assertMatch([], Tm),
  449. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}},
  450. {{self(), l}}]).
  451. t_simple_mreg() ->
  452. P = self(),
  453. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  454. {bar, bar_val}])),
  455. ?assertEqual(P, gproc:where({n,l,foo})),
  456. ?assertEqual(P, gproc:where({n,l,bar})),
  457. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  458. t_mreg_props() ->
  459. P = self(),
  460. ?assertEqual(true, gproc:mreg(p, l, [{p, v}])),
  461. ?assertEqual(v, gproc:get_value({p,l,p})),
  462. %% Force a context switch, since gproc:monitor_me() is asynchronous
  463. _ = sys:get_status(gproc),
  464. {monitors, Mons} = process_info(whereis(gproc), monitors),
  465. ?assertEqual(true, lists:keymember(P, 2, Mons)).
  466. t_gproc_crash() ->
  467. P = spawn_helper(),
  468. ?assert(gproc:where({n,l,P}) =:= P),
  469. exit(whereis(gproc), kill),
  470. give_gproc_some_time(100),
  471. ?assert(whereis(gproc) =/= undefined),
  472. %%
  473. %% Check that the registration is still there using an ets:lookup(),
  474. %% Once we've killed the process, gproc will always return undefined
  475. %% if the process is not alive, regardless of whether the registration
  476. %% is still there. So, here, the lookup should find something...
  477. %%
  478. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  479. ?assert(gproc:where({n,l,P}) =:= P),
  480. exit(P, kill),
  481. %% ...and here, it shouldn't.
  482. %% (sleep for a while first to let gproc handle the EXIT
  483. give_gproc_some_time(10),
  484. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  485. t_cancel_wait_and_register() ->
  486. Alias = {n, l, foo},
  487. Me = self(),
  488. P = spawn(fun() ->
  489. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  490. ?assert(element(1,sys:get_status(gproc)) == status),
  491. Me ! {self(), go_ahead},
  492. timer:sleep(infinity)
  493. end),
  494. receive
  495. {P, go_ahead} ->
  496. ?assertEqual(gproc:reg(Alias, undefined), true),
  497. exit(P, kill),
  498. timer:sleep(500),
  499. ?assert(element(1,sys:get_status(gproc)) == status)
  500. end.
  501. t_give_away_to_pid() ->
  502. From = {n, l, foo},
  503. Me = self(),
  504. P = spawn_link(fun t_loop/0),
  505. ?assertEqual(true, gproc:reg(From, undefined)),
  506. ?assertEqual(Me, gproc:where(From)),
  507. ?assertEqual(P, gproc:give_away(From, P)),
  508. ?assertEqual(P, gproc:where(From)),
  509. ?assertEqual(ok, t_lcall(P, die)).
  510. t_give_away_to_self() ->
  511. From = {n, l, foo},
  512. Me = self(),
  513. ?assertEqual(true, gproc:reg(From, undefined)),
  514. ?assertEqual(Me, gproc:where(From)),
  515. ?assertEqual(Me, gproc:give_away(From, Me)),
  516. ?assertEqual(Me, gproc:where(From)),
  517. ?assertEqual(true, gproc:unreg(From)).
  518. t_give_away_badarg() ->
  519. From = {n, l, foo},
  520. Me = self(),
  521. ?assertEqual(undefined, gproc:where(From)),
  522. ?assertError(badarg, gproc:give_away(From, Me)).
  523. t_give_away_to_unknown() ->
  524. From = {n, l, foo},
  525. Unknown = {n, l, unknown},
  526. Me = self(),
  527. ?assertEqual(true, gproc:reg(From, undefined)),
  528. ?assertEqual(Me, gproc:where(From)),
  529. ?assertEqual(undefined, gproc:where(Unknown)),
  530. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  531. ?assertEqual(undefined, gproc:where(From)).
  532. t_give_away_and_back() ->
  533. From = {n, l, foo},
  534. Me = self(),
  535. P = spawn_link(fun t_loop/0),
  536. ?assertEqual(true, gproc:reg(From, undefined)),
  537. ?assertEqual(Me, gproc:where(From)),
  538. ?assertEqual(P, gproc:give_away(From, P)),
  539. ?assertEqual(P, gproc:where(From)),
  540. ?assertEqual(ok, t_lcall(P, {give_away, From})),
  541. ?assertEqual(Me, gproc:where(From)),
  542. ?assertEqual(ok, t_lcall(P, die)).
  543. t_select() ->
  544. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  545. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  546. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  547. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  548. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  549. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  550. %% local names
  551. ?assertEqual(
  552. [{{n,l,{n,1}},self(),x},
  553. {{n,l,{n,2}},self(),y}], gproc:select(
  554. {local,names},
  555. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  556. %% mactch local names on value
  557. ?assertEqual(
  558. [{{n,l,{n,1}},self(),x}], gproc:select(
  559. {local,names},
  560. [{{{n,l,'_'},'_',x},[],['$_']}])),
  561. %% match all on value
  562. ?assertEqual(
  563. [{{n,l,{n,1}},self(),x},
  564. {{p,l,{p,1}},self(),x}], gproc:select(
  565. {all,all},
  566. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  567. %% match all on pid
  568. ?assertEqual(
  569. [{{a,l,{c,1}},self(),1},
  570. {{c,l,{c,1}},self(),1},
  571. {{n,l,{n,1}},self(),x},
  572. {{n,l,{n,2}},self(),y},
  573. {{p,l,{p,1}},self(),x},
  574. {{p,l,{p,2}},self(),y}
  575. ], gproc:select(
  576. {all,all},
  577. [{{'_',self(),'_'},[],['$_']}])).
  578. t_select_count() ->
  579. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  580. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  581. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  582. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  583. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  584. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  585. %% local names
  586. ?assertEqual(2, gproc:select_count(
  587. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  588. %% mactch local names on value
  589. ?assertEqual(1, gproc:select_count(
  590. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  591. %% match all on value
  592. ?assertEqual(2, gproc:select_count(
  593. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  594. %% match all on pid
  595. ?assertEqual(6, gproc:select_count(
  596. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  597. t_qlc() ->
  598. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  599. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  600. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  601. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  602. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  603. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  604. %% local names
  605. Exp1 = [{{n,l,{n,1}},self(),x},
  606. {{n,l,{n,2}},self(),y}],
  607. ?assertEqual(Exp1,
  608. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  609. ?assertEqual(Exp1,
  610. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  611. %% mactch local names on value
  612. Exp2 = [{{n,l,{n,1}},self(),x}],
  613. ?assertEqual(Exp2,
  614. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  615. %% match all on value
  616. Exp3 = [{{n,l,{n,1}},self(),x},
  617. {{p,l,{p,1}},self(),x}],
  618. ?assertEqual(Exp3,
  619. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  620. %% match all
  621. Exp4 = [{{a,l,{c,1}},self(),1},
  622. {{c,l,{c,1}},self(),1},
  623. {{n,l,{n,1}},self(),x},
  624. {{n,l,{n,2}},self(),y},
  625. {{p,l,{p,1}},self(),x},
  626. {{p,l,{p,2}},self(),y}
  627. ],
  628. ?assertEqual(Exp4,
  629. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  630. %% match on pid
  631. ?assertEqual(Exp4,
  632. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  633. gproc:table(all), P =:= self()]))),
  634. ?assertEqual(Exp4,
  635. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  636. gproc:table(all), P == self()]))).
  637. t_qlc_dead() ->
  638. P0 = self(),
  639. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  640. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  641. P1 = spawn(fun() ->
  642. Ref = erlang:monitor(process, P0),
  643. gproc:reg({n, l, {n,2}}, y),
  644. gproc:reg({p, l, {p,2}}, y),
  645. P0 ! {self(), ok},
  646. receive
  647. {_P, goodbye} -> ok;
  648. {'DOWN', Ref, _, _, _} ->
  649. ok
  650. end
  651. end),
  652. receive {P1, ok} -> ok end,
  653. %% now, suspend gproc so it doesn't do cleanup
  654. try sys:suspend(gproc),
  655. exit(P1, kill),
  656. %% local names
  657. Exp1 = [{{n,l,{n,1}},self(),x}],
  658. ?assertEqual(Exp1,
  659. qlc:e(qlc:q([N || N <-
  660. gproc:table(names, [check_pids])]))),
  661. ?assertEqual(Exp1,
  662. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <-
  663. gproc:table(names, [check_pids])]))),
  664. %% match local names on value
  665. Exp2 = [{{n,l,{n,1}},self(),x}],
  666. ?assertEqual(Exp2,
  667. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <-
  668. gproc:table(names, [check_pids])]))),
  669. ?assertEqual([],
  670. qlc:e(qlc:q([N || {{n,l,_},_,y} = N <-
  671. gproc:table(names, [check_pids])]))),
  672. %% match all on value
  673. Exp3 = [{{n,l,{n,1}},self(),x},
  674. {{p,l,{p,1}},self(),x}],
  675. ?assertEqual(Exp3,
  676. qlc:e(qlc:q([N || {_,_,x} = N <-
  677. gproc:table(all, [check_pids])]))),
  678. ?assertEqual([],
  679. qlc:e(qlc:q([N || {_,_,y} = N <-
  680. gproc:table(all, [check_pids])]))),
  681. Exp3b = [{{n,l,{n,2}},P1,y},
  682. {{p,l,{p,2}},P1,y}],
  683. ?assertEqual(Exp3b,
  684. qlc:e(qlc:q([N || {_,_,y} = N <-
  685. gproc:table(all)]))),
  686. %% match all
  687. Exp4 = [{{n,l,{n,1}},self(),x},
  688. {{p,l,{p,1}},self(),x}],
  689. ?assertEqual(Exp4,
  690. qlc:e(qlc:q([X || X <-
  691. gproc:table(all, [check_pids])]))),
  692. %% match on pid
  693. ?assertEqual(Exp4,
  694. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  695. gproc:table(all, [check_pids]),
  696. P =:= self()]))),
  697. ?assertEqual([],
  698. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  699. gproc:table(all, [check_pids]),
  700. P =:= P1]))),
  701. Exp4b = [{{n,l,{n,2}},P1,y},
  702. {{p,l,{p,2}},P1,y}],
  703. ?assertEqual(Exp4b,
  704. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  705. gproc:table(all),
  706. P =:= P1])))
  707. after
  708. sys:resume(gproc)
  709. end.
  710. t_get_env() ->
  711. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  712. ?assertEqual(true, os:putenv("SSSS", "s2")),
  713. ?assertEqual(true, os:putenv("TTTT", "s3")),
  714. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  715. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  716. %%
  717. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  718. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  719. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  720. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  721. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  722. %%
  723. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  724. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  725. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  726. {t, foo}, 3}])),
  727. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  728. t_get_set_env() ->
  729. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  730. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  731. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  732. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  733. t_set_env() ->
  734. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  735. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  736. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  737. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  738. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  739. %%
  740. ?assertEqual(true, os:putenv("SSSS", "s0")),
  741. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  742. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  743. ?assertEqual("s1", os:getenv("SSSS")),
  744. ?assertEqual(true, os:putenv("SSSS", "s0")),
  745. ?assertEqual([{self(),"s1"}],
  746. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  747. %%
  748. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  749. [{ram_copies,[node()]}])),
  750. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  751. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  752. {t_set_env,a},3}])),
  753. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  754. %% non-existing mnesia obj
  755. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  756. {t_set_env,b},3}])),
  757. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  758. t_get_env_inherit() ->
  759. P = spawn_link(fun() ->
  760. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  761. gproc:reg({n,l,get_env_p}),
  762. t_loop()
  763. end),
  764. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  765. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  766. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  767. [{inherit, {n,l,get_env_p}}])),
  768. ?assertEqual(ok, t_lcall(P, die)).
  769. %% What we test here is that we return the same current_function as the
  770. %% process_info() BIF. As we parse the backtrace dump, we check with some
  771. %% weirdly named functions.
  772. t_gproc_info() ->
  773. {A,B} = '-t1-'(),
  774. ?assertEqual(A,B),
  775. {C,D} = '\'t2'(),
  776. ?assertEqual(C,D),
  777. {E,F} = '\'t3\''(),
  778. ?assertEqual(E,F),
  779. {G,H} = t4(),
  780. ?assertEqual(G,H).
  781. '-t1-'() ->
  782. {_, I0} = process_info(self(), current_function),
  783. {_, I} = gproc:info(self(), current_function),
  784. {I0, I}.
  785. '\'t2'() ->
  786. {_, I0} = process_info(self(), current_function),
  787. {_, I} = gproc:info(self(), current_function),
  788. {I0, I}.
  789. '\'t3\''() ->
  790. {_, I0} = process_info(self(), current_function),
  791. {_, I} = gproc:info(self(), current_function),
  792. {I0, I}.
  793. t4() ->
  794. {_, I0} = process_info(self(), current_function),
  795. {_, I} = gproc:info(self(), current_function),
  796. {I0, I}.
  797. t_monitor() ->
  798. Me = self(),
  799. P = spawn_link(fun() ->
  800. gproc:reg({n,l,a}),
  801. Me ! continue,
  802. t_loop()
  803. end),
  804. receive continue ->
  805. ok
  806. end,
  807. Ref = gproc:monitor({n,l,a}),
  808. ?assertEqual(ok, t_lcall(P, die)),
  809. receive
  810. M ->
  811. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  812. end.
  813. t_monitor_give_away() ->
  814. Me = self(),
  815. P = spawn_link(fun() ->
  816. gproc:reg({n,l,a}),
  817. Me ! continue,
  818. t_loop()
  819. end),
  820. receive continue ->
  821. ok
  822. end,
  823. Ref = gproc:monitor({n,l,a}),
  824. ?assertEqual(ok, t_lcall(P, {give_away, {n,l,a}})),
  825. receive
  826. M ->
  827. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  828. end,
  829. ?assertEqual(ok, t_lcall(P, die)).
  830. t_monitor_standby() ->
  831. Me = self(),
  832. P = spawn(fun() ->
  833. gproc:reg({n,l,a}),
  834. Me ! continue,
  835. t_loop()
  836. end),
  837. receive continue ->
  838. ok
  839. end,
  840. Ref = gproc:monitor({n,l,a}, standby),
  841. exit(P, kill),
  842. receive
  843. M ->
  844. ?assertEqual({gproc,{failover,Me},Ref,{n,l,a}}, M)
  845. end,
  846. gproc:unreg({n,l,a}),
  847. ok.
  848. t_monitor_follow() ->
  849. Name = ?T_NAME,
  850. P1 = t_spawn(_Selective = true),
  851. Ref = t_call(P1, {apply, gproc, monitor, [Name, follow]}),
  852. {gproc,unreg,Ref,Name} = got_msg(P1),
  853. %% gproc_lib:dbg([gproc,gproc_lib]),
  854. P2 = t_spawn_reg(Name),
  855. {gproc,registered,Ref,Name} = got_msg(P1),
  856. exit(P2, kill),
  857. {gproc,unreg,Ref,Name} = got_msg(P1),
  858. P3 = t_spawn(true),
  859. Ref3 = t_call(P3, {apply, gproc, monitor, [Name, standby]}),
  860. {gproc,{failover,P3},Ref,Name} = got_msg(P1),
  861. {gproc,{failover,P3},Ref3,Name} = got_msg(P3),
  862. [exit(P,kill) || P <- [P1,P3]],
  863. ok.
  864. t_two_monitoring_processes_one_dies() ->
  865. Name = ?T_NAME,
  866. P = t_spawn_reg(Name),
  867. Ref = gproc:monitor(Name),
  868. {P2, R2} = spawn_monitor(fun() -> gproc:monitor(Name) end),
  869. receive
  870. {'DOWN', R2, process, P2, normal} -> ok
  871. end,
  872. exit(P, kill),
  873. receive
  874. M ->
  875. ?assertEqual({gproc, unreg, Ref, Name}, M)
  876. after 1000 ->
  877. error(timeout)
  878. end
  879. .
  880. t_monitor_demonitor() ->
  881. Name = ?T_NAME,
  882. P1 = t_spawn(Selective = true),
  883. Ref = t_call(P1, {apply, gproc, monitor, [Name, follow]}),
  884. {gproc, unreg, Ref, Name} = got_msg(P1),
  885. ok = t_call(P1, {apply, gproc, demonitor, [Name, Ref]}),
  886. P2 = t_spawn(Selective),
  887. Ref2 = t_call(P2, {apply, gproc, monitor, [Name, follow]}),
  888. {gproc, unreg, Ref2, Name} = got_msg(P2),
  889. P3 = t_spawn_reg(Name),
  890. {gproc, registered, Ref2, Name} = got_msg(P2),
  891. ok = gproc_test_lib:no_msg(P1, 300),
  892. [exit(P, kill) || P <- [P1, P2, P3]],
  893. ok.
  894. t_subscribe() ->
  895. Key = {n,l,a},
  896. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  897. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  898. P = spawn_link(fun() ->
  899. gproc:reg({n,l,a}),
  900. t_loop()
  901. end),
  902. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  903. ?assertEqual(ok, t_lcall(P, {give_away, Key})),
  904. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  905. gproc:give_away(Key, P),
  906. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  907. ?assertEqual(ok, t_lcall(P, die)),
  908. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  909. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  910. t_simple_pool()->
  911. Key = p1w1,
  912. From = {n,l,Key},
  913. _P = t_spawn_reg(From),
  914. %% create a new pool
  915. ?assertEqual(gproc_pool:new(p1), ok),
  916. %% add a worker to it
  917. ?assertEqual(gproc_pool:add_worker(p1,Key) , 1 ),
  918. ?assertEqual( length(gproc_pool:worker_pool(p1) ), 1),
  919. %% but it should not be active as yet
  920. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  921. ?assert( gproc_pool:pick(p1) =:= false ),
  922. %% connect to make the worker active
  923. ?assertEqual(gproc_pool:connect_worker(p1,Key) , true ),
  924. %% it should be active now
  925. ?assertEqual( length( gproc_pool:active_workers(p1)), 1),
  926. ?assertEqual( gproc_pool:pick(p1) , {n,l,[gproc_pool,p1,1,Key]}),
  927. Ref = erlang:make_ref(),
  928. gproc:send(From, {self(), Ref, die}),
  929. receive
  930. {_, Ref, Returned} ->
  931. ?assertEqual(Returned, ok)
  932. after 1000 ->
  933. %% the next 3 tests should fail if the worker is still alive
  934. ok
  935. end,
  936. %% disconnect the worker from the pool.
  937. ?assertEqual(gproc_pool:disconnect_worker(p1,Key), true),
  938. %% there should be no active workers now
  939. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  940. %% remove the worker from the pool
  941. ?assertEqual(gproc_pool:remove_worker(p1,Key), true),
  942. %% there should be no workers now
  943. %% NOTE: value of worker_pool seems to vary after removing workers
  944. %% sometimes [1,2] , sometimes [1], and then []
  945. %% so relying on defined_workers
  946. ?assertEqual( length(gproc_pool:defined_workers(p1)), 0 ),
  947. ?assertEqual( length(gproc_pool:worker_pool(p1)), 0 ),
  948. ?assertEqual( false, gproc_test_lib:t_pool_contains_atleast(p1,1) ),
  949. %% should be able to delete the pool now
  950. ?assertEqual( gproc_pool:delete(p1), ok).
  951. %% verifying #167 - Removing a worker from a pool does not make room
  952. %% for a new worker (size was erroneously adjusted down, though auto_size = false)
  953. t_pool_add_worker_size_1_no_auto_size() ->
  954. ok = gproc_pool:new(p2, direct, [{size, 1}, {auto_size, false}]),
  955. [] = gproc_pool:defined_workers(p2),
  956. Pos1 = gproc_pool:add_worker(p2, worker1),
  957. [{worker1, Pos1, 0}] = gproc_pool:defined_workers(p2),
  958. io:fwrite("G = ~p~n", [ets:tab2list(gproc)]),
  959. true = gproc_pool:remove_worker(p2, worker1),
  960. [] = gproc_pool:defined_workers(p2), %% the pool seems to be empty
  961. Pos2 = gproc_pool:add_worker(p2, worker2), %% throws error:pool_full
  962. true = is_integer(Pos2),
  963. true = gproc_pool:force_delete(p2).
  964. t_pool_add_worker_size_2_no_auto_size() ->
  965. gproc_pool:new(p3, direct, [{size, 2}, {auto_size, false}]),
  966. gproc_pool:add_worker(p3, worker1),
  967. gproc_pool:add_worker(p3, worker2),
  968. gproc_pool:remove_worker(p3, worker2),
  969. gproc_pool:add_worker(p3, worker3),
  970. gproc_pool:force_delete(p3).
  971. t_pool_round_robin_disconnect_worker() ->
  972. gproc_pool:new(p),
  973. gproc_pool:add_worker(p, w1),
  974. gproc_pool:add_worker(p, w2),
  975. gproc_pool:add_worker(p, w3),
  976. Work = fun(W) -> spawn(fun() -> gproc_pool:connect_worker(p, W), fun F() -> F() end() end) end,
  977. %% disc first
  978. W1 = Work(w1),
  979. W2 = Work(w2),
  980. W3 = Work(w3),
  981. timer:sleep(10),
  982. ?assertEqual([W1, W2, W3], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  983. ?assertEqual([W1, W2, W3, W1, W2, W3, W1, W2, W3], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  984. exit(W1, die),
  985. timer:sleep(10),
  986. ?assertEqual([W2, W3], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  987. ?assertEqual([W2, W3, W2, W3, W2, W3, W2, W3, W2], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  988. %% disc middle
  989. W11 = Work(w1),
  990. timer:sleep(10),
  991. ?assertEqual([W11, W2, W3], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  992. ?assertEqual([W3, W11, W2, W3, W11, W2, W3, W11, W2], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  993. exit(W2, die),
  994. timer:sleep(10),
  995. ?assertEqual([W11, W3], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  996. ?assertEqual([W3, W11, W3, W11, W3, W11, W3, W11, W3], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  997. %% disc last
  998. W22 = Work(w2),
  999. timer:sleep(10),
  1000. ?assertEqual([W11, W22, W3], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  1001. ?assertEqual([W11, W22, W3, W11, W22, W3, W11, W22, W3], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  1002. exit(W3, die),
  1003. timer:sleep(10),
  1004. ?assertEqual([W11, W22], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  1005. ?assertEqual([W11, W22, W11, W22, W11, W22, W11, W22, W11], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  1006. %% restore
  1007. W33 = Work(w3),
  1008. timer:sleep(10),
  1009. ?assertEqual([W11, W22, W33], [ P || {_, P} <- gproc_pool:active_workers(p)]),
  1010. ?assertEqual([W22, W33, W11, W22, W33, W11, W22, W33, W11], [gproc_pool:pick_worker(p) || _ <- lists:seq(1, 9)]),
  1011. gproc_pool:force_delete(p).
  1012. get_msg() ->
  1013. receive M ->
  1014. M
  1015. after 1000 ->
  1016. timeout
  1017. end.
  1018. %% t_spawn() -> gproc_test_lib:t_spawn(node()).
  1019. t_spawn(Sel) -> gproc_test_lib:t_spawn(node(), Sel).
  1020. t_spawn_reg(N) -> gproc_test_lib:t_spawn_reg(node(), N).
  1021. t_call(P, Req) -> gproc_test_lib:t_call(P, Req).
  1022. %% got_msg(P, M) -> gproc_test_lib:got_msg(P, M).
  1023. got_msg(P) -> gproc_test_lib:got_msg(P).
  1024. t_loop() ->
  1025. receive
  1026. {From, {give_away, Key}} ->
  1027. ?assertEqual(From, gproc:give_away(Key, From)),
  1028. From ! {self(), ok},
  1029. t_loop();
  1030. {From, die} ->
  1031. From ! {self(), ok};
  1032. {From, {reg, Name}} ->
  1033. From ! {self(), gproc:reg(Name,undefined)},
  1034. t_loop();
  1035. {From, {unreg, Name}} ->
  1036. From ! {self(), gproc:unreg(Name)},
  1037. t_loop()
  1038. end.
  1039. t_lcall(P, Msg) ->
  1040. P ! {self(), Msg},
  1041. receive
  1042. {P, Reply} ->
  1043. Reply
  1044. end.
  1045. spawn_helper() ->
  1046. Parent = self(),
  1047. P = spawn(fun() ->
  1048. ?assert(gproc:reg({n,l,self()}) =:= true),
  1049. Ref = erlang:monitor(process, Parent),
  1050. Parent ! {ok,self()},
  1051. receive
  1052. {'DOWN', Ref, _, _, _} ->
  1053. ok
  1054. end
  1055. end),
  1056. receive
  1057. {ok,P} ->
  1058. P
  1059. end.
  1060. give_gproc_some_time(T) ->
  1061. timer:sleep(T),
  1062. sys:get_status(gproc).
  1063. -endif.