gproc_tests.erl 39 KB

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