gproc_tests.erl 33 KB

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