gproc_tests.erl 39 KB

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