gproc_tests.erl 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf@wiger.net>
  17. %%
  18. -module(gproc_tests).
  19. -ifdef(TEST).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("stdlib/include/qlc.hrl").
  22. -define(T_NAME, {n, l, {?MODULE, ?LINE, erlang:now()}}).
  23. conf_test_() ->
  24. {foreach,
  25. fun() ->
  26. application:stop(gproc),
  27. application:unload(gproc)
  28. end,
  29. fun(_) ->
  30. application:stop(gproc)
  31. end,
  32. [?_test(t_server_opts()),
  33. ?_test(t_ets_opts())]}.
  34. t_server_opts() ->
  35. H = 10000,
  36. application:set_env(gproc, server_options, [{min_heap_size, H}]),
  37. ?assertMatch(ok, application:start(gproc)),
  38. {min_heap_size, H1} = process_info(whereis(gproc), min_heap_size),
  39. ?assert(is_integer(H1) andalso H1 > H).
  40. t_ets_opts() ->
  41. %% Cannot inspect the write_concurrency attribute on an ets table in
  42. %% any easy way, so trace on the ets:new/2 call and check the arguments.
  43. application:set_env(gproc, ets_options, [{write_concurrency, false}]),
  44. erlang:trace_pattern({ets,new, 2}, [{[gproc,'_'],[],[]}], [global]),
  45. erlang:trace(new, true, [call]),
  46. ?assert(ok == application:start(gproc)),
  47. erlang:trace(new, false, [call]),
  48. receive
  49. {trace,_,call,{ets,new,[gproc,Opts]}} ->
  50. ?assertMatch({write_concurrency, false},
  51. lists:keyfind(write_concurrency,1,Opts))
  52. after 3000 ->
  53. error(timeout)
  54. end.
  55. reg_test_() ->
  56. {setup,
  57. fun() ->
  58. application:start(gproc),
  59. application:start(mnesia)
  60. end,
  61. fun(_) ->
  62. application:stop(gproc),
  63. application:stop(mnesia)
  64. end,
  65. [
  66. {spawn, ?_test(?debugVal(t_simple_reg()))}
  67. , ?_test(t_is_clean())
  68. , {spawn, ?_test(?debugVal(t_simple_reg_or_locate()))}
  69. , ?_test(t_is_clean())
  70. , {spawn, ?_test(?debugVal(t_reg_or_locate2()))}
  71. , ?_test(t_is_clean())
  72. , {spawn, ?_test(?debugVal(t_reg_or_locate3()))}
  73. , ?_test(t_is_clean())
  74. , {spawn, ?_test(?debugVal(t_simple_counter()))}
  75. , ?_test(t_is_clean())
  76. , {spawn, ?_test(?debugVal(t_simple_aggr_counter()))}
  77. , ?_test(t_is_clean())
  78. , {spawn, ?_test(?debugVal(t_awaited_aggr_counter()))}
  79. , ?_test(t_is_clean())
  80. , {spawn, ?_test(?debugVal(t_simple_resource_count()))}
  81. , ?_test(t_is_clean())
  82. , {spawn, ?_test(?debugVal(t_awaited_resource_count()))}
  83. , ?_test(t_is_clean())
  84. , {spawn, ?_test(?debugVal(t_resource_count_on_zero_send()))}
  85. , ?_test(t_is_clean())
  86. , {spawn, ?_test(?debugVal(t_update_counters()))}
  87. , ?_test(t_is_clean())
  88. , {spawn, ?_test(?debugVal(t_simple_prop()))}
  89. , ?_test(t_is_clean())
  90. , {spawn, ?_test(?debugVal(t_await()))}
  91. , ?_test(t_is_clean())
  92. , {spawn, ?_test(?debugVal(t_await_self()))}
  93. , ?_test(t_is_clean())
  94. , {spawn, ?_test(?debugVal(t_await_crash()))}
  95. , ?_test(t_is_clean())
  96. , {spawn, ?_test(?debugVal(t_simple_mreg()))}
  97. , ?_test(t_is_clean())
  98. , {spawn, ?_test(?debugVal(t_mreg_props()))}
  99. , ?_test(t_is_clean())
  100. , {spawn, ?_test(?debugVal(t_gproc_crash()))}
  101. , ?_test(t_is_clean())
  102. , {spawn, ?_test(?debugVal(t_cancel_wait_and_register()))}
  103. , ?_test(t_is_clean())
  104. , {spawn, ?_test(?debugVal(t_give_away_to_pid()))}
  105. , ?_test(t_is_clean())
  106. , {spawn, ?_test(?debugVal(t_give_away_to_self()))}
  107. , ?_test(t_is_clean())
  108. , {spawn, ?_test(?debugVal(t_give_away_badarg()))}
  109. , ?_test(t_is_clean())
  110. , {spawn, ?_test(?debugVal(t_give_away_to_unknown()))}
  111. , ?_test(t_is_clean())
  112. , {spawn, ?_test(?debugVal(t_give_away_and_back()))}
  113. , ?_test(t_is_clean())
  114. , {spawn, ?_test(?debugVal(t_select()))}
  115. , ?_test(t_is_clean())
  116. , {spawn, ?_test(?debugVal(t_select_count()))}
  117. , ?_test(t_is_clean())
  118. , {spawn, ?_test(?debugVal(t_qlc()))}
  119. , ?_test(t_is_clean())
  120. , {spawn, ?_test(?debugVal(t_qlc_dead()))}
  121. , ?_test(t_is_clean())
  122. , {spawn, ?_test(?debugVal(t_get_env()))}
  123. , ?_test(t_is_clean())
  124. , {spawn, ?_test(?debugVal(t_get_set_env()))}
  125. , ?_test(t_is_clean())
  126. , {spawn, ?_test(?debugVal(t_set_env()))}
  127. , ?_test(t_is_clean())
  128. , {spawn, ?_test(?debugVal(t_get_env_inherit()))}
  129. , ?_test(t_is_clean())
  130. , {spawn, ?_test(?debugVal(t_monitor()))}
  131. , ?_test(t_is_clean())
  132. , {spawn, ?_test(?debugVal(t_monitor_give_away()))}
  133. , ?_test(t_is_clean())
  134. , {spawn, ?_test(?debugVal(t_monitor_standby()))}
  135. , ?_test(t_is_clean())
  136. , {spawn, ?_test(?debugVal(t_monitor_follow()))}
  137. , ?_test(t_is_clean())
  138. , {spawn, ?_test(?debugVal(t_subscribe()))}
  139. , ?_test(t_is_clean())
  140. , {spawn, ?_test(?debugVal(t_gproc_info()))}
  141. , ?_test(t_is_clean())
  142. , {spawn, ?_test(?debugVal(t_simple_pool()))}
  143. , ?_test(t_is_clean())
  144. ]}.
  145. t_simple_reg() ->
  146. ?assert(gproc:reg({n,l,name}) =:= true),
  147. ?assert(gproc:where({n,l,name}) =:= self()),
  148. ?assert(gproc:unreg({n,l,name}) =:= true),
  149. ?assert(gproc:where({n,l,name}) =:= undefined).
  150. t_simple_reg_or_locate() ->
  151. P = self(),
  152. ?assertMatch({P, undefined}, gproc:reg_or_locate({n,l,name})),
  153. ?assertMatch(P, gproc:where({n,l,name})),
  154. ?assertMatch({P, my_val}, gproc:reg_or_locate({n,l,name2}, my_val)),
  155. ?assertMatch(my_val, gproc:get_value({n,l,name2})).
  156. t_reg_or_locate2() ->
  157. P = self(),
  158. {P1,R1} = spawn_monitor(fun() ->
  159. Ref = erlang:monitor(process, P),
  160. gproc:reg({n,l,foo}, the_value),
  161. P ! {self(), ok},
  162. receive
  163. {'DOWN',Ref,_,_,_} -> ok
  164. end
  165. end),
  166. receive {P1, ok} -> ok end,
  167. ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
  168. exit(P1, kill),
  169. receive
  170. {'DOWN',R1,_,_,_} ->
  171. ok
  172. end.
  173. t_reg_or_locate3() ->
  174. P = self(),
  175. {P1, Value} = gproc:reg_or_locate(
  176. {n,l,foo}, the_value,
  177. fun() ->
  178. P ! {self(), ok},
  179. receive
  180. {'DOWN',_Ref,_,_,_} -> ok
  181. end
  182. end),
  183. ?assert(P =/= P1),
  184. ?assert(Value =:= the_value),
  185. _Ref = erlang:monitor(process, P1),
  186. receive
  187. {P1, ok} -> ok;
  188. {'DOWN', _Ref, _, _, _Reason} ->
  189. ?assert(process_died_unexpectedly)
  190. end,
  191. ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
  192. exit(P1, kill),
  193. receive
  194. {'DOWN',_R1,_,_,_} ->
  195. ok
  196. end.
  197. t_simple_counter() ->
  198. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  199. ?assert(gproc:get_value({c,l,c1}) =:= 3),
  200. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  201. ?assert(gproc:reset_counter({c,l,c1}) =:= {7, 3}).
  202. t_simple_aggr_counter() ->
  203. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  204. ?assert(gproc:reg({a,l,c1}) =:= true),
  205. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  206. P = self(),
  207. P1 = spawn_link(fun() ->
  208. gproc:reg({c,l,c1}, 5),
  209. P ! {self(), ok},
  210. receive
  211. {P, goodbye} -> ok
  212. end
  213. end),
  214. receive {P1, ok} -> ok end,
  215. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  216. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  217. ?assert(gproc:get_value({a,l,c1}) =:= 12),
  218. P1 ! {self(), goodbye},
  219. R = erlang:monitor(process, P1),
  220. receive {'DOWN', R, _, _, _} ->
  221. gproc:audit_process(P1)
  222. end,
  223. ?assert(gproc:get_value({a,l,c1}) =:= 7).
  224. t_awaited_aggr_counter() ->
  225. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  226. gproc:nb_wait({a,l,c1}),
  227. ?assert(gproc:reg({a,l,c1}) =:= true),
  228. receive {gproc,_,registered,{{a,l,c1},_,_}} -> ok
  229. after 1000 ->
  230. error(timeout)
  231. end,
  232. ?assertMatch(3, gproc:get_value({a,l,c1})).
  233. t_simple_resource_count() ->
  234. ?assert(gproc:reg({r,l,r1}, 1) =:= true),
  235. ?assert(gproc:reg({rc,l,r1}) =:= true),
  236. ?assert(gproc:get_value({rc,l,r1}) =:= 1),
  237. P = self(),
  238. P1 = spawn_link(fun() ->
  239. gproc:reg({r,l,r1}, 1),
  240. P ! {self(), ok},
  241. receive
  242. {P, goodbye} -> ok
  243. end
  244. end),
  245. receive {P1, ok} -> ok end,
  246. ?assert(gproc:get_value({rc,l,r1}) =:= 2),
  247. P1 ! {self(), goodbye},
  248. R = erlang:monitor(process, P1),
  249. receive {'DOWN', R, _, _, _} ->
  250. gproc:audit_process(P1)
  251. end,
  252. ?assert(gproc:get_value({rc,l,r1}) =:= 1).
  253. t_awaited_resource_count() ->
  254. ?assert(gproc:reg({r,l,r1}, 3) =:= true),
  255. ?assert(gproc:reg({r,l,r2}, 3) =:= true),
  256. ?assert(gproc:reg({r,l,r3}, 3) =:= true),
  257. gproc:nb_wait({rc,l,r1}),
  258. ?assert(gproc:reg({rc,l,r1}) =:= true),
  259. receive {gproc,_,registered,{{rc,l,r1},_,_}} -> ok
  260. after 1000 ->
  261. error(timeout)
  262. end,
  263. ?assertMatch(1, gproc:get_value({rc,l,r1})).
  264. t_resource_count_on_zero_send() ->
  265. Me = self(),
  266. ?assertMatch(true, gproc:reg({p,l,myp})),
  267. ?assertMatch(true, gproc:reg({r,l,r1})),
  268. ?assertMatch(true, gproc:reg({rc,l,r1}, 1, [{on_zero,
  269. [{send, {p,l,myp}}]}])),
  270. ?assertMatch(1, gproc:get_value({rc,l,r1})),
  271. ?assertMatch(true, gproc:unreg({r,l,r1})),
  272. ?assertMatch(0, gproc:get_value({rc,l,r1})),
  273. receive
  274. {gproc, resource_on_zero, l, r1, Me} ->
  275. ok
  276. after 1000 ->
  277. error(timeout)
  278. end.
  279. t_update_counters() ->
  280. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  281. ?assert(gproc:reg({a,l,c1}) =:= true),
  282. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  283. P = self(),
  284. P1 = spawn_link(fun() ->
  285. gproc:reg({c,l,c1}, 5),
  286. P ! {self(), ok},
  287. receive
  288. {P, goodbye} -> ok
  289. end
  290. end),
  291. receive {P1, ok} -> ok end,
  292. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  293. Me = self(),
  294. ?assertEqual([{{c,l,c1},Me,7},
  295. {{c,l,c1},P1,8}], gproc:update_counters(l, [{{c,l,c1}, Me, 4},
  296. {{c,l,c1}, P1, 3}])),
  297. ?assert(gproc:get_value({a,l,c1}) =:= 15),
  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_simple_prop() ->
  305. ?assert(gproc:reg({p,l,prop}) =:= true),
  306. ?assert(t_other_proc(fun() ->
  307. ?assert(gproc:reg({p,l,prop}) =:= true)
  308. end) =:= ok),
  309. ?assert(gproc:unreg({p,l,prop}) =:= true).
  310. t_other_proc(F) ->
  311. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  312. receive
  313. {'DOWN',Ref,_,_,R} ->
  314. R
  315. after 10000 ->
  316. erlang:error(timeout)
  317. end.
  318. t_await() ->
  319. Me = self(),
  320. Name = {n,l,t_await},
  321. {_Pid,Ref} = spawn_monitor(
  322. fun() ->
  323. exit(?assert(
  324. gproc:await(Name) =:= {Me,val}))
  325. end),
  326. ?assert(gproc:reg(Name, val) =:= true),
  327. receive
  328. {'DOWN', Ref, _, _, R} ->
  329. ?assertEqual(R, ok)
  330. after 10000 ->
  331. erlang:error(timeout)
  332. end,
  333. ?assertMatch(Me, gproc:where(Name)),
  334. ok.
  335. t_await_self() ->
  336. Me = self(),
  337. Ref = gproc:nb_wait({n, l, t_await_self}),
  338. ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
  339. ?assertEqual(true, receive
  340. {gproc, Ref, R, Wh} ->
  341. {registered, {{n, l, t_await_self},
  342. Me, some_value}} = {R, Wh},
  343. true
  344. after 10000 ->
  345. timeout
  346. end).
  347. t_await_crash() ->
  348. Name = {n,l,{dummy,?LINE}},
  349. {Pid,_} = spawn_regger(Name),
  350. ?assertEqual({Pid,undefined}, gproc:await(Name, 1000)),
  351. exit(Pid, kill),
  352. {NewPid,MRef} = spawn_regger(Name),
  353. ?assertEqual(false, is_process_alive(Pid)),
  354. ?assertEqual({NewPid,undefined}, gproc:await(Name, 1000)),
  355. exit(NewPid, kill),
  356. receive {'DOWN', MRef, _, _, _} -> ok end.
  357. spawn_regger(Name) ->
  358. spawn_monitor(fun() ->
  359. gproc:reg(Name),
  360. timer:sleep(60000)
  361. end).
  362. t_is_clean() ->
  363. sys:get_status(gproc), % in order to synch
  364. sys:get_status(gproc_monitor),
  365. T = ets:tab2list(gproc),
  366. Tm = ets:tab2list(gproc_monitor),
  367. ?assertMatch([], Tm),
  368. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
  369. t_simple_mreg() ->
  370. P = self(),
  371. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  372. {bar, bar_val}])),
  373. ?assertEqual(P, gproc:where({n,l,foo})),
  374. ?assertEqual(P, gproc:where({n,l,bar})),
  375. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  376. t_mreg_props() ->
  377. P = self(),
  378. ?assertEqual(true, gproc:mreg(p, l, [{p, v}])),
  379. ?assertEqual(v, gproc:get_value({p,l,p})),
  380. %% Force a context switch, since gproc:monitor_me() is asynchronous
  381. _ = sys:get_status(gproc),
  382. {monitors, Mons} = process_info(whereis(gproc), monitors),
  383. ?assertEqual(true, lists:keymember(P, 2, Mons)).
  384. t_gproc_crash() ->
  385. P = spawn_helper(),
  386. ?assert(gproc:where({n,l,P}) =:= P),
  387. exit(whereis(gproc), kill),
  388. give_gproc_some_time(100),
  389. ?assert(whereis(gproc) =/= undefined),
  390. %%
  391. %% Check that the registration is still there using an ets:lookup(),
  392. %% Once we've killed the process, gproc will always return undefined
  393. %% if the process is not alive, regardless of whether the registration
  394. %% is still there. So, here, the lookup should find something...
  395. %%
  396. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  397. ?assert(gproc:where({n,l,P}) =:= P),
  398. exit(P, kill),
  399. %% ...and here, it shouldn't.
  400. %% (sleep for a while first to let gproc handle the EXIT
  401. give_gproc_some_time(10),
  402. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  403. t_cancel_wait_and_register() ->
  404. Alias = {n, l, foo},
  405. Me = self(),
  406. P = spawn(fun() ->
  407. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  408. ?assert(element(1,sys:get_status(gproc)) == status),
  409. Me ! {self(), go_ahead},
  410. timer:sleep(infinity)
  411. end),
  412. receive
  413. {P, go_ahead} ->
  414. ?assertEqual(gproc:reg(Alias, undefined), true),
  415. exit(P, kill),
  416. timer:sleep(500),
  417. ?assert(element(1,sys:get_status(gproc)) == status)
  418. end.
  419. t_give_away_to_pid() ->
  420. From = {n, l, foo},
  421. Me = self(),
  422. P = spawn_link(fun t_loop/0),
  423. ?assertEqual(true, gproc:reg(From, undefined)),
  424. ?assertEqual(Me, gproc:where(From)),
  425. ?assertEqual(P, gproc:give_away(From, P)),
  426. ?assertEqual(P, gproc:where(From)),
  427. ?assertEqual(ok, t_lcall(P, die)).
  428. t_give_away_to_self() ->
  429. From = {n, l, foo},
  430. Me = self(),
  431. ?assertEqual(true, gproc:reg(From, undefined)),
  432. ?assertEqual(Me, gproc:where(From)),
  433. ?assertEqual(Me, gproc:give_away(From, Me)),
  434. ?assertEqual(Me, gproc:where(From)),
  435. ?assertEqual(true, gproc:unreg(From)).
  436. t_give_away_badarg() ->
  437. From = {n, l, foo},
  438. Me = self(),
  439. ?assertEqual(undefined, gproc:where(From)),
  440. ?assertError(badarg, gproc:give_away(From, Me)).
  441. t_give_away_to_unknown() ->
  442. From = {n, l, foo},
  443. Unknown = {n, l, unknown},
  444. Me = self(),
  445. ?assertEqual(true, gproc:reg(From, undefined)),
  446. ?assertEqual(Me, gproc:where(From)),
  447. ?assertEqual(undefined, gproc:where(Unknown)),
  448. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  449. ?assertEqual(undefined, gproc:where(From)).
  450. t_give_away_and_back() ->
  451. From = {n, l, foo},
  452. Me = self(),
  453. P = spawn_link(fun t_loop/0),
  454. ?assertEqual(true, gproc:reg(From, undefined)),
  455. ?assertEqual(Me, gproc:where(From)),
  456. ?assertEqual(P, gproc:give_away(From, P)),
  457. ?assertEqual(P, gproc:where(From)),
  458. ?assertEqual(ok, t_lcall(P, {give_away, From})),
  459. ?assertEqual(Me, gproc:where(From)),
  460. ?assertEqual(ok, t_lcall(P, die)).
  461. t_select() ->
  462. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  463. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  464. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  465. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  466. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  467. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  468. %% local names
  469. ?assertEqual(
  470. [{{n,l,{n,1}},self(),x},
  471. {{n,l,{n,2}},self(),y}], gproc:select(
  472. {local,names},
  473. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  474. %% mactch local names on value
  475. ?assertEqual(
  476. [{{n,l,{n,1}},self(),x}], gproc:select(
  477. {local,names},
  478. [{{{n,l,'_'},'_',x},[],['$_']}])),
  479. %% match all on value
  480. ?assertEqual(
  481. [{{n,l,{n,1}},self(),x},
  482. {{p,l,{p,1}},self(),x}], gproc:select(
  483. {all,all},
  484. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  485. %% match all on pid
  486. ?assertEqual(
  487. [{{a,l,{c,1}},self(),1},
  488. {{c,l,{c,1}},self(),1},
  489. {{n,l,{n,1}},self(),x},
  490. {{n,l,{n,2}},self(),y},
  491. {{p,l,{p,1}},self(),x},
  492. {{p,l,{p,2}},self(),y}
  493. ], gproc:select(
  494. {all,all},
  495. [{{'_',self(),'_'},[],['$_']}])).
  496. t_select_count() ->
  497. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  498. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  499. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  500. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  501. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  502. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  503. %% local names
  504. ?assertEqual(2, gproc:select_count(
  505. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  506. %% mactch local names on value
  507. ?assertEqual(1, gproc:select_count(
  508. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  509. %% match all on value
  510. ?assertEqual(2, gproc:select_count(
  511. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  512. %% match all on pid
  513. ?assertEqual(6, gproc:select_count(
  514. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  515. t_qlc() ->
  516. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  517. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  518. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  519. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  520. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  521. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  522. %% local names
  523. Exp1 = [{{n,l,{n,1}},self(),x},
  524. {{n,l,{n,2}},self(),y}],
  525. ?assertEqual(Exp1,
  526. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  527. ?assertEqual(Exp1,
  528. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  529. %% mactch local names on value
  530. Exp2 = [{{n,l,{n,1}},self(),x}],
  531. ?assertEqual(Exp2,
  532. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  533. %% match all on value
  534. Exp3 = [{{n,l,{n,1}},self(),x},
  535. {{p,l,{p,1}},self(),x}],
  536. ?assertEqual(Exp3,
  537. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  538. %% match all
  539. Exp4 = [{{a,l,{c,1}},self(),1},
  540. {{c,l,{c,1}},self(),1},
  541. {{n,l,{n,1}},self(),x},
  542. {{n,l,{n,2}},self(),y},
  543. {{p,l,{p,1}},self(),x},
  544. {{p,l,{p,2}},self(),y}
  545. ],
  546. ?assertEqual(Exp4,
  547. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  548. %% match on pid
  549. ?assertEqual(Exp4,
  550. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  551. gproc:table(all), P =:= self()]))),
  552. ?assertEqual(Exp4,
  553. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  554. gproc:table(all), P == self()]))).
  555. t_qlc_dead() ->
  556. P0 = self(),
  557. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  558. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  559. P1 = spawn(fun() ->
  560. Ref = erlang:monitor(process, P0),
  561. gproc:reg({n, l, {n,2}}, y),
  562. gproc:reg({p, l, {p,2}}, y),
  563. P0 ! {self(), ok},
  564. receive
  565. {_P, goodbye} -> ok;
  566. {'DOWN', Ref, _, _, _} ->
  567. ok
  568. end
  569. end),
  570. receive {P1, ok} -> ok end,
  571. %% now, suspend gproc so it doesn't do cleanup
  572. try sys:suspend(gproc),
  573. exit(P1, kill),
  574. %% local names
  575. Exp1 = [{{n,l,{n,1}},self(),x}],
  576. ?assertEqual(Exp1,
  577. qlc:e(qlc:q([N || N <-
  578. gproc:table(names, [check_pids])]))),
  579. ?assertEqual(Exp1,
  580. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <-
  581. gproc:table(names, [check_pids])]))),
  582. %% match local names on value
  583. Exp2 = [{{n,l,{n,1}},self(),x}],
  584. ?assertEqual(Exp2,
  585. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <-
  586. gproc:table(names, [check_pids])]))),
  587. ?assertEqual([],
  588. qlc:e(qlc:q([N || {{n,l,_},_,y} = N <-
  589. gproc:table(names, [check_pids])]))),
  590. %% match all on value
  591. Exp3 = [{{n,l,{n,1}},self(),x},
  592. {{p,l,{p,1}},self(),x}],
  593. ?assertEqual(Exp3,
  594. qlc:e(qlc:q([N || {_,_,x} = N <-
  595. gproc:table(all, [check_pids])]))),
  596. ?assertEqual([],
  597. qlc:e(qlc:q([N || {_,_,y} = N <-
  598. gproc:table(all, [check_pids])]))),
  599. Exp3b = [{{n,l,{n,2}},P1,y},
  600. {{p,l,{p,2}},P1,y}],
  601. ?assertEqual(Exp3b,
  602. qlc:e(qlc:q([N || {_,_,y} = N <-
  603. gproc:table(all)]))),
  604. %% match all
  605. Exp4 = [{{n,l,{n,1}},self(),x},
  606. {{p,l,{p,1}},self(),x}],
  607. ?assertEqual(Exp4,
  608. qlc:e(qlc:q([X || X <-
  609. gproc:table(all, [check_pids])]))),
  610. %% match on pid
  611. ?assertEqual(Exp4,
  612. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  613. gproc:table(all, [check_pids]),
  614. P =:= self()]))),
  615. ?assertEqual([],
  616. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  617. gproc:table(all, [check_pids]),
  618. P =:= P1]))),
  619. Exp4b = [{{n,l,{n,2}},P1,y},
  620. {{p,l,{p,2}},P1,y}],
  621. ?assertEqual(Exp4b,
  622. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  623. gproc:table(all),
  624. P =:= P1])))
  625. after
  626. sys:resume(gproc)
  627. end.
  628. t_get_env() ->
  629. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  630. ?assertEqual(true, os:putenv("SSSS", "s2")),
  631. ?assertEqual(true, os:putenv("TTTT", "s3")),
  632. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  633. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  634. %%
  635. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  636. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  637. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  638. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  639. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  640. %%
  641. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  642. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  643. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  644. {t, foo}, 3}])),
  645. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  646. t_get_set_env() ->
  647. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  648. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  649. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  650. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  651. t_set_env() ->
  652. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  653. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  654. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  655. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  656. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  657. %%
  658. ?assertEqual(true, os:putenv("SSSS", "s0")),
  659. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  660. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  661. ?assertEqual("s1", os:getenv("SSSS")),
  662. ?assertEqual(true, os:putenv("SSSS", "s0")),
  663. ?assertEqual([{self(),"s1"}],
  664. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  665. %%
  666. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  667. [{ram_copies,[node()]}])),
  668. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  669. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  670. {t_set_env,a},3}])),
  671. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  672. %% non-existing mnesia obj
  673. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  674. {t_set_env,b},3}])),
  675. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  676. t_get_env_inherit() ->
  677. P = spawn_link(fun() ->
  678. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  679. gproc:reg({n,l,get_env_p}),
  680. t_loop()
  681. end),
  682. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  683. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  684. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  685. [{inherit, {n,l,get_env_p}}])),
  686. ?assertEqual(ok, t_lcall(P, die)).
  687. %% What we test here is that we return the same current_function as the
  688. %% process_info() BIF. As we parse the backtrace dump, we check with some
  689. %% weirdly named functions.
  690. t_gproc_info() ->
  691. {A,B} = '-t1-'(),
  692. ?assertEqual(A,B),
  693. {C,D} = '\'t2'(),
  694. ?assertEqual(C,D),
  695. {E,F} = '\'t3\''(),
  696. ?assertEqual(E,F),
  697. {G,H} = t4(),
  698. ?assertEqual(G,H).
  699. '-t1-'() ->
  700. {_, I0} = process_info(self(), current_function),
  701. {_, I} = gproc:info(self(), current_function),
  702. {I0, I}.
  703. '\'t2'() ->
  704. {_, I0} = process_info(self(), current_function),
  705. {_, I} = gproc:info(self(), current_function),
  706. {I0, I}.
  707. '\'t3\''() ->
  708. {_, I0} = process_info(self(), current_function),
  709. {_, I} = gproc:info(self(), current_function),
  710. {I0, I}.
  711. t4() ->
  712. {_, I0} = process_info(self(), current_function),
  713. {_, I} = gproc:info(self(), current_function),
  714. {I0, I}.
  715. t_monitor() ->
  716. Me = self(),
  717. P = spawn_link(fun() ->
  718. gproc:reg({n,l,a}),
  719. Me ! continue,
  720. t_loop()
  721. end),
  722. receive continue ->
  723. ok
  724. end,
  725. Ref = gproc:monitor({n,l,a}),
  726. ?assertEqual(ok, t_lcall(P, die)),
  727. receive
  728. M ->
  729. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  730. end.
  731. t_monitor_give_away() ->
  732. Me = self(),
  733. P = spawn_link(fun() ->
  734. gproc:reg({n,l,a}),
  735. Me ! continue,
  736. t_loop()
  737. end),
  738. receive continue ->
  739. ok
  740. end,
  741. Ref = gproc:monitor({n,l,a}),
  742. ?assertEqual(ok, t_lcall(P, {give_away, {n,l,a}})),
  743. receive
  744. M ->
  745. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  746. end,
  747. ?assertEqual(ok, t_lcall(P, die)).
  748. t_monitor_standby() ->
  749. Me = self(),
  750. P = spawn(fun() ->
  751. gproc:reg({n,l,a}),
  752. Me ! continue,
  753. t_loop()
  754. end),
  755. receive continue ->
  756. ok
  757. end,
  758. Ref = gproc:monitor({n,l,a}, standby),
  759. exit(P, kill),
  760. receive
  761. M ->
  762. ?assertEqual({gproc,{failover,Me},Ref,{n,l,a}}, M)
  763. end,
  764. gproc:unreg({n,l,a}),
  765. ok.
  766. t_monitor_follow() ->
  767. Name = ?T_NAME,
  768. P1 = t_spawn(_Selective = true),
  769. Ref = t_call(P1, {apply, gproc, monitor, [Name, follow]}),
  770. {gproc,unreg,Ref,Name} = got_msg(P1),
  771. %% gproc_lib:dbg([gproc,gproc_lib]),
  772. P2 = t_spawn_reg(Name),
  773. {gproc,registered,Ref,Name} = got_msg(P1),
  774. exit(P2, kill),
  775. {gproc,unreg,Ref,Name} = got_msg(P1),
  776. P3 = t_spawn(true),
  777. Ref3 = t_call(P3, {apply, gproc, monitor, [Name, standby]}),
  778. {gproc,{failover,P3},Ref,Name} = got_msg(P1),
  779. {gproc,{failover,P3},Ref3,Name} = got_msg(P3),
  780. [exit(P,kill) || P <- [P1,P3]],
  781. ok.
  782. t_subscribe() ->
  783. Key = {n,l,a},
  784. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  785. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  786. P = spawn_link(fun() ->
  787. gproc:reg({n,l,a}),
  788. t_loop()
  789. end),
  790. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  791. ?assertEqual(ok, t_lcall(P, {give_away, Key})),
  792. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  793. gproc:give_away(Key, P),
  794. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  795. ?assertEqual(ok, t_lcall(P, die)),
  796. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  797. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  798. t_simple_pool()->
  799. Key = p1w1,
  800. From = {n,l,Key},
  801. _P = t_spawn_reg(From),
  802. %% create a new pool
  803. ?assertEqual(gproc_pool:new(p1), ok),
  804. %% add a worker to it
  805. ?assertEqual(gproc_pool:add_worker(p1,Key) , 1 ),
  806. ?assertEqual( length(gproc_pool:worker_pool(p1) ), 1),
  807. %% but it should not be active as yet
  808. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  809. ?assert( gproc_pool:pick(p1) =:= false ),
  810. %% connect to make the worker active
  811. ?assertEqual(gproc_pool:connect_worker(p1,Key) , true ),
  812. %% it should be active now
  813. ?assertEqual( length( gproc_pool:active_workers(p1)), 1),
  814. ?assertEqual( gproc_pool:pick(p1) , {n,l,[gproc_pool,p1,1,Key]}),
  815. Ref = erlang:make_ref(),
  816. gproc:send(From, {self(), Ref, die}),
  817. receive
  818. {_, Ref, Returned} ->
  819. ?assertEqual(Returned, ok)
  820. after 1000 ->
  821. %% the next 3 tests should fail if the worker is still alive
  822. ok
  823. end,
  824. %% disconnect the worker from the pool.
  825. ?assertEqual(gproc_pool:disconnect_worker(p1,Key), true),
  826. %% there should be no active workers now
  827. ?assertEqual( length( gproc_pool:active_workers(p1)), 0),
  828. %% remove the worker from the pool
  829. ?assertEqual(gproc_pool:remove_worker(p1,Key), true),
  830. %% there should be no workers now
  831. %% NOTE: value of worker_pool seems to vary after removing workers
  832. %% sometimes [1,2] , sometimes [1], and then []
  833. %% so relying on defined_workers
  834. ?assertEqual( length(gproc_pool:defined_workers(p1)), 0 ),
  835. ?assertEqual( length(gproc_pool:worker_pool(p1)), 0 ),
  836. ?assertEqual( false, gproc_test_lib:t_pool_contains_atleast(p1,1) ),
  837. %% should be able to delete the pool now
  838. ?assertEqual( gproc_pool:delete(p1), ok).
  839. get_msg() ->
  840. receive M ->
  841. M
  842. after 1000 ->
  843. timeout
  844. end.
  845. %% t_spawn() -> gproc_test_lib:t_spawn(node()).
  846. t_spawn(Sel) -> gproc_test_lib:t_spawn(node(), Sel).
  847. t_spawn_reg(N) -> gproc_test_lib:t_spawn_reg(node(), N).
  848. t_call(P, Req) -> gproc_test_lib:t_call(P, Req).
  849. %% got_msg(P, M) -> gproc_test_lib:got_msg(P, M).
  850. got_msg(P) -> gproc_test_lib:got_msg(P).
  851. t_loop() ->
  852. receive
  853. {From, {give_away, Key}} ->
  854. ?assertEqual(From, gproc:give_away(Key, From)),
  855. From ! {self(), ok},
  856. t_loop();
  857. {From, die} ->
  858. From ! {self(), ok};
  859. {From, {reg, Name}} ->
  860. From ! {self(), gproc:reg(Name,undefined)},
  861. t_loop();
  862. {From, {unreg, Name}} ->
  863. From ! {self(), gproc:unreg(Name)},
  864. t_loop()
  865. end.
  866. t_lcall(P, Msg) ->
  867. P ! {self(), Msg},
  868. receive
  869. {P, Reply} ->
  870. Reply
  871. end.
  872. spawn_helper() ->
  873. Parent = self(),
  874. P = spawn(fun() ->
  875. ?assert(gproc:reg({n,l,self()}) =:= true),
  876. Ref = erlang:monitor(process, Parent),
  877. Parent ! {ok,self()},
  878. receive
  879. {'DOWN', Ref, _, _, _} ->
  880. ok
  881. end
  882. end),
  883. receive
  884. {ok,P} ->
  885. P
  886. end.
  887. give_gproc_some_time(T) ->
  888. timer:sleep(T),
  889. sys:get_status(gproc).
  890. -endif.