gproc_tests.erl 33 KB

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