gproc_tests.erl 34 KB

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