gproc_tests.erl 23 KB

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