gproc_tests.erl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. , {spawn, ?_test(?debugVal(t_gproc_info()))}
  113. , ?_test(t_is_clean())
  114. ]}.
  115. t_simple_reg() ->
  116. ?assert(gproc:reg({n,l,name}) =:= true),
  117. ?assert(gproc:where({n,l,name}) =:= self()),
  118. ?assert(gproc:unreg({n,l,name}) =:= true),
  119. ?assert(gproc:where({n,l,name}) =:= undefined).
  120. t_simple_counter() ->
  121. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  122. ?assert(gproc:get_value({c,l,c1}) =:= 3),
  123. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  124. ?assert(gproc:reset_counter({c,l,c1}) =:= {7, 3}).
  125. t_simple_aggr_counter() ->
  126. ?assert(gproc:reg({c,l,c1}, 3) =:= true),
  127. ?assert(gproc:reg({a,l,c1}) =:= true),
  128. ?assert(gproc:get_value({a,l,c1}) =:= 3),
  129. P = self(),
  130. P1 = spawn_link(fun() ->
  131. gproc:reg({c,l,c1}, 5),
  132. P ! {self(), ok},
  133. receive
  134. {P, goodbye} -> ok
  135. end
  136. end),
  137. receive {P1, ok} -> ok end,
  138. ?assert(gproc:get_value({a,l,c1}) =:= 8),
  139. ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
  140. ?assert(gproc:get_value({a,l,c1}) =:= 12),
  141. P1 ! {self(), goodbye},
  142. R = erlang:monitor(process, P1),
  143. receive {'DOWN', R, _, _, _} ->
  144. gproc:audit_process(P1)
  145. end,
  146. ?assert(gproc:get_value({a,l,c1}) =:= 7).
  147. t_simple_prop() ->
  148. ?assert(gproc:reg({p,l,prop}) =:= true),
  149. ?assert(t_other_proc(fun() ->
  150. ?assert(gproc:reg({p,l,prop}) =:= true)
  151. end) =:= ok),
  152. ?assert(gproc:unreg({p,l,prop}) =:= true).
  153. t_other_proc(F) ->
  154. {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
  155. receive
  156. {'DOWN',Ref,_,_,R} ->
  157. R
  158. after 10000 ->
  159. erlang:error(timeout)
  160. end.
  161. t_await() ->
  162. Me = self(),
  163. {_Pid,Ref} = spawn_monitor(
  164. fun() ->
  165. exit(?assert(
  166. gproc:await({n,l,t_await}) =:= {Me,val}))
  167. end),
  168. ?assert(gproc:reg({n,l,t_await},val) =:= true),
  169. receive
  170. {'DOWN', Ref, _, _, R} ->
  171. ?assertEqual(R, ok)
  172. after 10000 ->
  173. erlang:error(timeout)
  174. end.
  175. t_await_self() ->
  176. Me = self(),
  177. Ref = gproc:nb_wait({n, l, t_await_self}),
  178. ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
  179. ?assertEqual(true, receive
  180. {gproc, Ref, R, Wh} ->
  181. {registered, {{n, l, t_await_self},
  182. Me, some_value}} = {R, Wh},
  183. true
  184. after 10000 ->
  185. timeout
  186. end).
  187. t_is_clean() ->
  188. sys:get_status(gproc), % in order to synch
  189. sys:get_status(gproc_monitor),
  190. T = ets:tab2list(gproc),
  191. Tm = ets:tab2list(gproc_monitor),
  192. ?assertMatch([], Tm),
  193. ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
  194. t_simple_mreg() ->
  195. P = self(),
  196. ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
  197. {bar, bar_val}])),
  198. ?assertEqual(P, gproc:where({n,l,foo})),
  199. ?assertEqual(P, gproc:where({n,l,bar})),
  200. ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
  201. t_gproc_crash() ->
  202. P = spawn_helper(),
  203. ?assert(gproc:where({n,l,P}) =:= P),
  204. exit(whereis(gproc), kill),
  205. give_gproc_some_time(100),
  206. ?assert(whereis(gproc) =/= undefined),
  207. %%
  208. %% Check that the registration is still there using an ets:lookup(),
  209. %% Once we've killed the process, gproc will always return undefined
  210. %% if the process is not alive, regardless of whether the registration
  211. %% is still there. So, here, the lookup should find something...
  212. %%
  213. ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
  214. ?assert(gproc:where({n,l,P}) =:= P),
  215. exit(P, kill),
  216. %% ...and here, it shouldn't.
  217. %% (sleep for a while first to let gproc handle the EXIT
  218. give_gproc_some_time(10),
  219. ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
  220. t_cancel_wait_and_register() ->
  221. Alias = {n, l, foo},
  222. Me = self(),
  223. P = spawn(fun() ->
  224. {'EXIT',_} = (catch gproc:await(Alias, 100)),
  225. ?assert(element(1,sys:get_status(gproc)) == status),
  226. Me ! {self(), go_ahead},
  227. timer:sleep(infinity)
  228. end),
  229. receive
  230. {P, go_ahead} ->
  231. ?assertEqual(gproc:reg(Alias, undefined), true),
  232. exit(P, kill),
  233. timer:sleep(500),
  234. ?assert(element(1,sys:get_status(gproc)) == status)
  235. end.
  236. t_give_away_to_pid() ->
  237. From = {n, l, foo},
  238. Me = self(),
  239. P = spawn_link(fun t_loop/0),
  240. ?assertEqual(true, gproc:reg(From, undefined)),
  241. ?assertEqual(Me, gproc:where(From)),
  242. ?assertEqual(P, gproc:give_away(From, P)),
  243. ?assertEqual(P, gproc:where(From)),
  244. ?assertEqual(ok, t_call(P, die)).
  245. t_give_away_to_self() ->
  246. From = {n, l, foo},
  247. Me = self(),
  248. ?assertEqual(true, gproc:reg(From, undefined)),
  249. ?assertEqual(Me, gproc:where(From)),
  250. ?assertEqual(Me, gproc:give_away(From, Me)),
  251. ?assertEqual(Me, gproc:where(From)),
  252. ?assertEqual(true, gproc:unreg(From)).
  253. t_give_away_badarg() ->
  254. From = {n, l, foo},
  255. Me = self(),
  256. ?assertEqual(undefined, gproc:where(From)),
  257. ?assertError(badarg, gproc:give_away(From, Me)).
  258. t_give_away_to_unknown() ->
  259. From = {n, l, foo},
  260. Unknown = {n, l, unknown},
  261. Me = self(),
  262. ?assertEqual(true, gproc:reg(From, undefined)),
  263. ?assertEqual(Me, gproc:where(From)),
  264. ?assertEqual(undefined, gproc:where(Unknown)),
  265. ?assertEqual(undefined, gproc:give_away(From, Unknown)),
  266. ?assertEqual(undefined, gproc:where(From)).
  267. t_give_away_and_back() ->
  268. From = {n, l, foo},
  269. Me = self(),
  270. P = spawn_link(fun t_loop/0),
  271. ?assertEqual(true, gproc:reg(From, undefined)),
  272. ?assertEqual(Me, gproc:where(From)),
  273. ?assertEqual(P, gproc:give_away(From, P)),
  274. ?assertEqual(P, gproc:where(From)),
  275. ?assertEqual(ok, t_call(P, {give_away, From})),
  276. ?assertEqual(Me, gproc:where(From)),
  277. ?assertEqual(ok, t_call(P, die)).
  278. t_select() ->
  279. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  280. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  281. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  282. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  283. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  284. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  285. %% local names
  286. ?assertEqual(
  287. [{{n,l,{n,1}},self(),x},
  288. {{n,l,{n,2}},self(),y}], gproc:select(
  289. {local,names},
  290. [{{{n,l,'_'},'_','_'},[],['$_']}])),
  291. %% mactch local names on value
  292. ?assertEqual(
  293. [{{n,l,{n,1}},self(),x}], gproc:select(
  294. {local,names},
  295. [{{{n,l,'_'},'_',x},[],['$_']}])),
  296. %% match all on value
  297. ?assertEqual(
  298. [{{n,l,{n,1}},self(),x},
  299. {{p,l,{p,1}},self(),x}], gproc:select(
  300. {all,all},
  301. [{{{'_',l,'_'},'_',x},[],['$_']}])),
  302. %% match all on pid
  303. ?assertEqual(
  304. [{{a,l,{c,1}},self(),1},
  305. {{c,l,{c,1}},self(),1},
  306. {{n,l,{n,1}},self(),x},
  307. {{n,l,{n,2}},self(),y},
  308. {{p,l,{p,1}},self(),x},
  309. {{p,l,{p,2}},self(),y}
  310. ], gproc:select(
  311. {all,all},
  312. [{{'_',self(),'_'},[],['$_']}])).
  313. t_select_count() ->
  314. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  315. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  316. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  317. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  318. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  319. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  320. %% local names
  321. ?assertEqual(2, gproc:select_count(
  322. {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
  323. %% mactch local names on value
  324. ?assertEqual(1, gproc:select_count(
  325. {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
  326. %% match all on value
  327. ?assertEqual(2, gproc:select_count(
  328. {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
  329. %% match all on pid
  330. ?assertEqual(6, gproc:select_count(
  331. {all,all}, [{{'_',self(),'_'},[],[true]}])).
  332. t_qlc() ->
  333. ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
  334. ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
  335. ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
  336. ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
  337. ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
  338. ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
  339. %% local names
  340. Exp1 = [{{n,l,{n,1}},self(),x},
  341. {{n,l,{n,2}},self(),y}],
  342. ?assertEqual(Exp1,
  343. qlc:e(qlc:q([N || N <- gproc:table(names)]))),
  344. ?assertEqual(Exp1,
  345. qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
  346. %% mactch local names on value
  347. Exp2 = [{{n,l,{n,1}},self(),x}],
  348. ?assertEqual(Exp2,
  349. qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
  350. %% match all on value
  351. Exp3 = [{{n,l,{n,1}},self(),x},
  352. {{p,l,{p,1}},self(),x}],
  353. ?assertEqual(Exp3,
  354. qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
  355. %% match all
  356. Exp4 = [{{a,l,{c,1}},self(),1},
  357. {{c,l,{c,1}},self(),1},
  358. {{n,l,{n,1}},self(),x},
  359. {{n,l,{n,2}},self(),y},
  360. {{p,l,{p,1}},self(),x},
  361. {{p,l,{p,2}},self(),y}
  362. ],
  363. ?assertEqual(Exp4,
  364. qlc:e(qlc:q([X || X <- gproc:table(all)]))),
  365. %% match on pid
  366. ?assertEqual(Exp4,
  367. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  368. gproc:table(all), P =:= self()]))),
  369. ?assertEqual(Exp4,
  370. qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
  371. gproc:table(all), P == self()]))).
  372. t_get_env() ->
  373. ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
  374. ?assertEqual(true, os:putenv("SSSS", "s2")),
  375. ?assertEqual(true, os:putenv("TTTT", "s3")),
  376. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  377. ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
  378. %%
  379. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
  380. ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
  381. ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
  382. ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
  383. ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
  384. %%
  385. ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
  386. ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
  387. ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
  388. {t, foo}, 3}])),
  389. ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
  390. t_get_set_env() ->
  391. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  392. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  393. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  394. ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
  395. t_set_env() ->
  396. ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
  397. ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
  398. ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
  399. ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
  400. ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
  401. %%
  402. ?assertEqual(true, os:putenv("SSSS", "s0")),
  403. ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
  404. ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
  405. ?assertEqual("s1", os:getenv("SSSS")),
  406. ?assertEqual(true, os:putenv("SSSS", "s0")),
  407. ?assertEqual([{self(),"s1"}],
  408. gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
  409. %%
  410. ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
  411. [{ram_copies,[node()]}])),
  412. ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
  413. ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
  414. {t_set_env,a},3}])),
  415. ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
  416. %% non-existing mnesia obj
  417. ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
  418. {t_set_env,b},3}])),
  419. ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
  420. t_get_env_inherit() ->
  421. P = spawn_link(fun() ->
  422. ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
  423. gproc:reg({n,l,get_env_p}),
  424. t_loop()
  425. end),
  426. ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
  427. ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
  428. ?assertEqual(bar, gproc:get_env(l, gproc, foo,
  429. [{inherit, {n,l,get_env_p}}])),
  430. ?assertEqual(ok, t_call(P, die)).
  431. %% What we test here is that we return the same current_function as the
  432. %% process_info() BIF. As we parse the backtrace dump, we check with some
  433. %% weirdly named functions.
  434. t_gproc_info() ->
  435. {A,B} = '-t1-'(),
  436. ?assertEqual(A,B),
  437. {C,D} = '\'t2'(),
  438. ?assertEqual(C,D),
  439. {E,F} = '\'t3\''(),
  440. ?assertEqual(E,F),
  441. {G,H} = t4(),
  442. ?assertEqual(G,H).
  443. '-t1-'() ->
  444. {_, I0} = process_info(self(), current_function),
  445. {_, I} = gproc:info(self(), current_function),
  446. {I0, I}.
  447. '\'t2'() ->
  448. {_, I0} = process_info(self(), current_function),
  449. {_, I} = gproc:info(self(), current_function),
  450. {I0, I}.
  451. '\'t3\''() ->
  452. {_, I0} = process_info(self(), current_function),
  453. {_, I} = gproc:info(self(), current_function),
  454. {I0, I}.
  455. t4() ->
  456. {_, I0} = process_info(self(), current_function),
  457. {_, I} = gproc:info(self(), current_function),
  458. {I0, I}.
  459. t_monitor() ->
  460. Me = self(),
  461. P = spawn_link(fun() ->
  462. gproc:reg({n,l,a}),
  463. Me ! continue,
  464. t_loop()
  465. end),
  466. receive continue ->
  467. ok
  468. end,
  469. Ref = gproc:monitor({n,l,a}),
  470. ?assertEqual(ok, t_call(P, die)),
  471. receive
  472. M ->
  473. ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
  474. end.
  475. t_monitor_give_away() ->
  476. Me = self(),
  477. P = spawn_link(fun() ->
  478. gproc:reg({n,l,a}),
  479. Me ! continue,
  480. t_loop()
  481. end),
  482. receive continue ->
  483. ok
  484. end,
  485. Ref = gproc:monitor({n,l,a}),
  486. ?assertEqual(ok, t_call(P, {give_away, {n,l,a}})),
  487. receive
  488. M ->
  489. ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
  490. end,
  491. ?assertEqual(ok, t_call(P, die)).
  492. t_subscribe() ->
  493. Key = {n,l,a},
  494. ?assertEqual(ok, gproc_monitor:subscribe(Key)),
  495. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  496. P = spawn_link(fun() ->
  497. gproc:reg({n,l,a}),
  498. t_loop()
  499. end),
  500. ?assertEqual({gproc_monitor, Key, P}, get_msg()),
  501. ?assertEqual(ok, t_call(P, {give_away, Key})),
  502. ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
  503. gproc:give_away(Key, P),
  504. ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
  505. ?assertEqual(ok, t_call(P, die)),
  506. ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
  507. ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
  508. get_msg() ->
  509. receive M ->
  510. M
  511. after 1000 ->
  512. timeout
  513. end.
  514. t_loop() ->
  515. receive
  516. {From, {give_away, Key}} ->
  517. ?assertEqual(From, gproc:give_away(Key, From)),
  518. From ! {self(), ok},
  519. t_loop();
  520. {From, die} ->
  521. From ! {self(), ok}
  522. end.
  523. t_call(P, Msg) ->
  524. P ! {self(), Msg},
  525. receive
  526. {P, Reply} ->
  527. Reply
  528. end.
  529. spawn_helper() ->
  530. Parent = self(),
  531. P = spawn(fun() ->
  532. ?assert(gproc:reg({n,l,self()}) =:= true),
  533. Ref = erlang:monitor(process, Parent),
  534. Parent ! {ok,self()},
  535. receive
  536. {'DOWN', Ref, _, _, _} ->
  537. ok
  538. end
  539. end),
  540. receive
  541. {ok,P} ->
  542. P
  543. end.
  544. give_gproc_some_time(T) ->
  545. timer:sleep(T),
  546. sys:get_status(gproc).
  547. -endif.