gproc_tests.erl 20 KB

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