gproc_tests.erl 19 KB

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