gproc_tests.erl 23 KB

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