gproc_tests.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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@erlang-solutions.com>
  17. %%
  18. -module(gproc_tests).
  19. -ifdef(TEST).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("stdlib/include/qlc.hrl").
  22. conf_test_() ->
  23. {foreach,
  24. fun() ->
  25. application:unload(gproc)
  26. end,
  27. fun(_) ->
  28. application:stop(gproc)
  29. end,
  30. [?_test(t_server_opts()),
  31. ?_test(t_ets_opts())]}.
  32. t_server_opts() ->
  33. H = 10000,
  34. application:set_env(gproc, server_options, [{min_heap_size, H}]),
  35. ?assert(ok == application:start(gproc)),
  36. {min_heap_size, H1} = process_info(whereis(gproc), min_heap_size),
  37. ?assert(is_integer(H1) andalso H1 > H).
  38. t_ets_opts() ->
  39. %% Cannot inspect the write_concurrency attribute on an ets table in
  40. %% any easy way, so trace on the ets:new/2 call and check the arguments.
  41. application:set_env(gproc, ets_options, [{write_concurrency, false}]),
  42. erlang:trace_pattern({ets,new, 2}, [{[gproc,'_'],[],[]}], [global]),
  43. erlang:trace(new, true, [call]),
  44. ?assert(ok == application:start(gproc)),
  45. erlang:trace(new, false, [call]),
  46. receive
  47. {trace,_,call,{ets,new,[gproc,Opts]}} ->
  48. ?assertMatch({write_concurrency, false},
  49. lists:keyfind(write_concurrency,1,Opts))
  50. after 3000 ->
  51. error(timeout)
  52. end.
  53. reg_test_() ->
  54. {setup,
  55. fun() ->
  56. application:start(gproc),
  57. application:start(mnesia)
  58. end,
  59. fun(_) ->
  60. application:stop(gproc),
  61. application:stop(mnesia)
  62. end,
  63. [
  64. {spawn, ?_test(?debugVal(t_simple_reg()))}
  65. , ?_test(t_is_clean())
  66. , {spawn, ?_test(?debugVal(t_simple_counter()))}
  67. , ?_test(t_is_clean())
  68. , {spawn, ?_test(?debugVal(t_simple_aggr_counter()))}
  69. , ?_test(t_is_clean())
  70. , {spawn, ?_test(?debugVal(t_simple_prop()))}
  71. , ?_test(t_is_clean())
  72. , {spawn, ?_test(?debugVal(t_await()))}
  73. , ?_test(t_is_clean())
  74. , {spawn, ?_test(?debugVal(t_await_self()))}
  75. , ?_test(t_is_clean())
  76. , {spawn, ?_test(?debugVal(t_simple_mreg()))}
  77. , ?_test(t_is_clean())
  78. , {spawn, ?_test(?debugVal(t_gproc_crash()))}
  79. , ?_test(t_is_clean())
  80. , {spawn, ?_test(?debugVal(t_cancel_wait_and_register()))}
  81. , ?_test(t_is_clean())
  82. , {spawn, ?_test(?debugVal(t_give_away_to_pid()))}
  83. , ?_test(t_is_clean())
  84. , {spawn, ?_test(?debugVal(t_give_away_to_self()))}
  85. , ?_test(t_is_clean())
  86. , {spawn, ?_test(?debugVal(t_give_away_badarg()))}
  87. , ?_test(t_is_clean())
  88. , {spawn, ?_test(?debugVal(t_give_away_to_unknown()))}
  89. , ?_test(t_is_clean())
  90. , {spawn, ?_test(?debugVal(t_give_away_and_back()))}
  91. , ?_test(t_is_clean())
  92. , {spawn, ?_test(?debugVal(t_select()))}
  93. , ?_test(t_is_clean())
  94. , {spawn, ?_test(?debugVal(t_select_count()))}
  95. , ?_test(t_is_clean())
  96. , {spawn, ?_test(?debugVal(t_qlc()))}
  97. , ?_test(t_is_clean())
  98. , {spawn, ?_test(?debugVal(t_get_env()))}
  99. , ?_test(t_is_clean())
  100. , {spawn, ?_test(?debugVal(t_get_set_env()))}
  101. , ?_test(t_is_clean())
  102. , {spawn, ?_test(?debugVal(t_set_env()))}
  103. , ?_test(t_is_clean())
  104. , {spawn, ?_test(?debugVal(t_get_env_inherit()))}
  105. , ?_test(t_is_clean())
  106. , {spawn, ?_test(?debugVal(t_monitor()))}
  107. , ?_test(t_is_clean())
  108. , {spawn, ?_test(?debugVal(t_monitor_give_away()))}
  109. , ?_test(t_is_clean())
  110. , {spawn, ?_test(?debugVal(t_subscribe()))}
  111. , ?_test(t_is_clean())
  112. ]}.
  113. t_simple_reg() ->
  114. ?assert(gproc:reg({n,l,name}) =:= true),
  115. ?assert(gproc:where({n,l,name}) =:= self()),
  116. ?assert(gproc:unreg({n,l,name}) =:= true),
  117. ?assert(gproc:where({n,l,name}) =:= undefined).
  118. t_simple_counter() ->
  119. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  120. ?assert(gproc:get_value({c,l,c1}) =:= 3),
  121. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  122. ?assert(gproc:reset_counter({c,l,c1}) =:= {7, 3}).
  123. t_simple_aggr_counter() ->
  124. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  125. ?assert(gproc:reg({a,l,c1}) =:= true),
  126. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  127. P = self(),
  128. P1 = spawn_link(fun() ->
  129. gproc:reg({c,l,c1}, 5),
  130. P ! {self(), ok},
  131. receive
  132. {P, goodbye} -> ok
  133. end
  134. end),
  135. receive {P1, ok} -> ok end,
  136. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  137. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  138. ?assert(gproc:get_value({a,l,c1}) =:= 12),
  139. P1 ! {self(), goodbye},
  140. R = erlang:monitor(process, P1),
  141. receive {'DOWN', R, _, _, _} ->
  142. gproc:audit_process(P1)
  143. end,
  144. ?assert(gproc:get_value({a,l,c1}) =:= 7).
  145. t_simple_prop() ->
  146. ?assert(gproc:reg({p,l,prop}) =:= true),
  147. ?assert(t_other_proc(fun() ->
  148. ?assert(gproc:reg({p,l,prop}) =:= true)
  149. end) =:= ok),
  150. ?assert(gproc:unreg({p,l,prop}) =:= true).
  151. t_other_proc(F) ->
  152. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  153. receive
  154. {'DOWN',Ref,_,_,R} ->
  155. R
  156. after 10000 ->
  157. erlang:error(timeout)
  158. end.
  159. t_await() ->
  160. Me = self(),
  161. {_Pid,Ref} = spawn_monitor(
  162. fun() ->
  163. exit(?assert(
  164. gproc:await({n,l,t_await}) =:= {Me,val}))
  165. end),
  166. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  167. receive
  168. {'DOWN', Ref, _, _, R} ->
  169. ?assertEqual(R, ok)
  170. after 10000 ->
  171. erlang:error(timeout)
  172. end.
  173. t_await_self() ->
  174. Me = self(),
  175. Ref = gproc:nb_wait({n, l, t_await_self}),
  176. ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
  177. ?assertEqual(true, receive
  178. {gproc, Ref, R, Wh} ->
  179. {registered, {{n, l, t_await_self},
  180. Me, some_value}} = {R, Wh},
  181. true
  182. after 10000 ->
  183. timeout
  184. end).
  185. t_is_clean() ->
  186. sys:get_status(gproc), % in order to synch
  187. sys:get_status(gproc_monitor),
  188. T = ets:tab2list(gproc),
  189. Tm = ets:tab2list(gproc_monitor),
  190. ?assertMatch([], Tm),
  191. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
  192. t_simple_mreg() ->
  193. P = self(),
  194. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  195. {bar, bar_val}])),
  196. ?assertEqual(P, gproc:where({n,l,foo})),
  197. ?assertEqual(P, gproc:where({n,l,bar})),
  198. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  199. t_gproc_crash() ->
  200. P = spawn_helper(),
  201. ?assert(gproc:where({n,l,P}) =:= P),
  202. exit(whereis(gproc), kill),
  203. give_gproc_some_time(100),
  204. ?assert(whereis(gproc) =/= undefined),
  205. %%
  206. %% Check that the registration is still there using an ets:lookup(),
  207. %% Once we've killed the process, gproc will always return undefined
  208. %% if the process is not alive, regardless of whether the registration
  209. %% is still there. So, here, the lookup should find something...
  210. %%
  211. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  212. ?assert(gproc:where({n,l,P}) =:= P),
  213. exit(P, kill),
  214. %% ...and here, it shouldn't.
  215. %% (sleep for a while first to let gproc handle the EXIT
  216. give_gproc_some_time(10),
  217. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  218. t_cancel_wait_and_register() ->
  219. Alias = {n, l, foo},
  220. Me = self(),
  221. P = spawn(fun() ->
  222. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  223. ?assert(element(1,sys:get_status(gproc)) == status),
  224. Me ! {self(), go_ahead},
  225. timer:sleep(infinity)
  226. end),
  227. receive
  228. {P, go_ahead} ->
  229. ?assertEqual(gproc:reg(Alias, undefined), true),
  230. exit(P, kill),
  231. timer:sleep(500),
  232. ?assert(element(1,sys:get_status(gproc)) == status)
  233. end.
  234. t_give_away_to_pid() ->
  235. From = {n, l, foo},
  236. Me = self(),
  237. P = spawn_link(fun t_loop/0),
  238. ?assertEqual(true, gproc:reg(From, undefined)),
  239. ?assertEqual(Me, gproc:where(From)),
  240. ?assertEqual(P, gproc:give_away(From, P)),
  241. ?assertEqual(P, gproc:where(From)),
  242. ?assertEqual(ok, t_call(P, die)).
  243. t_give_away_to_self() ->
  244. From = {n, l, foo},
  245. Me = self(),
  246. ?assertEqual(true, gproc:reg(From, undefined)),
  247. ?assertEqual(Me, gproc:where(From)),
  248. ?assertEqual(Me, gproc:give_away(From, Me)),
  249. ?assertEqual(Me, gproc:where(From)),
  250. ?assertEqual(true, gproc:unreg(From)).
  251. t_give_away_badarg() ->
  252. From = {n, l, foo},
  253. Me = self(),
  254. ?assertEqual(undefined, gproc:where(From)),
  255. ?assertError(badarg, gproc:give_away(From, Me)).
  256. t_give_away_to_unknown() ->
  257. From = {n, l, foo},
  258. Unknown = {n, l, unknown},
  259. Me = self(),
  260. ?assertEqual(true, gproc:reg(From, undefined)),
  261. ?assertEqual(Me, gproc:where(From)),
  262. ?assertEqual(undefined, gproc:where(Unknown)),
  263. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  264. ?assertEqual(undefined, gproc:where(From)).
  265. t_give_away_and_back() ->
  266. From = {n, l, foo},
  267. Me = self(),
  268. P = spawn_link(fun t_loop/0),
  269. ?assertEqual(true, gproc:reg(From, undefined)),
  270. ?assertEqual(Me, gproc:where(From)),
  271. ?assertEqual(P, gproc:give_away(From, P)),
  272. ?assertEqual(P, gproc:where(From)),
  273. ?assertEqual(ok, t_call(P, {give_away, From})),
  274. ?assertEqual(Me, gproc:where(From)),
  275. ?assertEqual(ok, t_call(P, die)).
  276. t_select() ->
  277. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  278. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  279. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  280. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  281. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  282. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  283. %% local names
  284. ?assertEqual(
  285. [{{n,l,{n,1}},self(),x},
  286. {{n,l,{n,2}},self(),y}], gproc:select(
  287. {local,names},
  288. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  289. %% mactch local names on value
  290. ?assertEqual(
  291. [{{n,l,{n,1}},self(),x}], gproc:select(
  292. {local,names},
  293. [{{{n,l,'_'},'_',x},[],['$_']}])),
  294. %% match all on value
  295. ?assertEqual(
  296. [{{n,l,{n,1}},self(),x},
  297. {{p,l,{p,1}},self(),x}], gproc:select(
  298. {all,all},
  299. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  300. %% match all on pid
  301. ?assertEqual(
  302. [{{a,l,{c,1}},self(),1},
  303. {{c,l,{c,1}},self(),1},
  304. {{n,l,{n,1}},self(),x},
  305. {{n,l,{n,2}},self(),y},
  306. {{p,l,{p,1}},self(),x},
  307. {{p,l,{p,2}},self(),y}
  308. ], gproc:select(
  309. {all,all},
  310. [{{'_',self(),'_'},[],['$_']}])).
  311. t_select_count() ->
  312. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  313. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  314. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  315. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  316. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  317. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  318. %% local names
  319. ?assertEqual(2, gproc:select_count(
  320. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  321. %% mactch local names on value
  322. ?assertEqual(1, gproc:select_count(
  323. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  324. %% match all on value
  325. ?assertEqual(2, gproc:select_count(
  326. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  327. %% match all on pid
  328. ?assertEqual(6, gproc:select_count(
  329. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  330. t_qlc() ->
  331. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  332. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  333. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  334. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  335. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  336. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  337. %% local names
  338. Exp1 = [{{n,l,{n,1}},self(),x},
  339. {{n,l,{n,2}},self(),y}],
  340. ?assertEqual(Exp1,
  341. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  342. ?assertEqual(Exp1,
  343. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  344. %% mactch local names on value
  345. Exp2 = [{{n,l,{n,1}},self(),x}],
  346. ?assertEqual(Exp2,
  347. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  348. %% match all on value
  349. Exp3 = [{{n,l,{n,1}},self(),x},
  350. {{p,l,{p,1}},self(),x}],
  351. ?assertEqual(Exp3,
  352. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  353. %% match all
  354. Exp4 = [{{a,l,{c,1}},self(),1},
  355. {{c,l,{c,1}},self(),1},
  356. {{n,l,{n,1}},self(),x},
  357. {{n,l,{n,2}},self(),y},
  358. {{p,l,{p,1}},self(),x},
  359. {{p,l,{p,2}},self(),y}
  360. ],
  361. ?assertEqual(Exp4,
  362. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  363. %% match on pid
  364. ?assertEqual(Exp4,
  365. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  366. gproc:table(all), P =:= self()]))),
  367. ?assertEqual(Exp4,
  368. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  369. gproc:table(all), P == self()]))).
  370. t_get_env() ->
  371. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  372. ?assertEqual(true, os:putenv("SSSS", "s2")),
  373. ?assertEqual(true, os:putenv("TTTT", "s3")),
  374. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  375. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  376. %%
  377. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  378. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  379. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  380. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  381. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  382. %%
  383. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  384. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  385. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  386. {t, foo}, 3}])),
  387. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  388. t_get_set_env() ->
  389. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  390. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  391. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  392. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  393. t_set_env() ->
  394. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  395. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  396. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  397. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  398. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  399. %%
  400. ?assertEqual(true, os:putenv("SSSS", "s0")),
  401. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  402. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  403. ?assertEqual("s1", os:getenv("SSSS")),
  404. ?assertEqual(true, os:putenv("SSSS", "s0")),
  405. ?assertEqual([{self(),"s1"}],
  406. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  407. %%
  408. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  409. [{ram_copies,[node()]}])),
  410. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  411. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  412. {t_set_env,a},3}])),
  413. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  414. %% non-existing mnesia obj
  415. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  416. {t_set_env,b},3}])),
  417. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  418. t_get_env_inherit() ->
  419. P = spawn_link(fun() ->
  420. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  421. gproc:reg({n,l,get_env_p}),
  422. t_loop()
  423. end),
  424. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  425. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  426. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  427. [{inherit, {n,l,get_env_p}}])),
  428. ?assertEqual(ok, t_call(P, die)).
  429. t_monitor() ->
  430. Me = self(),
  431. P = spawn_link(fun() ->
  432. gproc:reg({n,l,a}),
  433. Me ! continue,
  434. t_loop()
  435. end),
  436. receive continue ->
  437. ok
  438. end,
  439. Ref = gproc:monitor({n,l,a}),
  440. ?assertEqual(ok, t_call(P, die)),
  441. receive
  442. M ->
  443. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  444. end.
  445. t_monitor_give_away() ->
  446. Me = self(),
  447. P = spawn_link(fun() ->
  448. gproc:reg({n,l,a}),
  449. Me ! continue,
  450. t_loop()
  451. end),
  452. receive continue ->
  453. ok
  454. end,
  455. Ref = gproc:monitor({n,l,a}),
  456. ?assertEqual(ok, t_call(P, {give_away, {n,l,a}})),
  457. receive
  458. M ->
  459. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  460. end,
  461. ?assertEqual(ok, t_call(P, die)).
  462. t_subscribe() ->
  463. Key = {n,l,a},
  464. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  465. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  466. P = spawn_link(fun() ->
  467. gproc:reg({n,l,a}),
  468. t_loop()
  469. end),
  470. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  471. ?assertEqual(ok, t_call(P, {give_away, Key})),
  472. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  473. gproc:give_away(Key, P),
  474. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  475. ?assertEqual(ok, t_call(P, die)),
  476. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  477. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  478. get_msg() ->
  479. receive M ->
  480. M
  481. after 1000 ->
  482. timeout
  483. end.
  484. t_loop() ->
  485. receive
  486. {From, {give_away, Key}} ->
  487. ?assertEqual(From, gproc:give_away(Key, From)),
  488. From ! {self(), ok},
  489. t_loop();
  490. {From, die} ->
  491. From ! {self(), ok}
  492. end.
  493. t_call(P, Msg) ->
  494. P ! {self(), Msg},
  495. receive
  496. {P, Reply} ->
  497. Reply
  498. end.
  499. spawn_helper() ->
  500. Parent = self(),
  501. P = spawn(fun() ->
  502. ?assert(gproc:reg({n,l,self()}) =:= true),
  503. Ref = erlang:monitor(process, Parent),
  504. Parent ! {ok,self()},
  505. receive
  506. {'DOWN', Ref, _, _, _} ->
  507. ok
  508. end
  509. end),
  510. receive
  511. {ok,P} ->
  512. P
  513. end.
  514. give_gproc_some_time(T) ->
  515. timer:sleep(T),
  516. sys:get_status(gproc).
  517. -endif.