PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/test/gproc_tests.erl

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