gproc_tests.erl 20 KB

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