gproc_dist_tests.erl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. %% -*- erlang-indent-level: 4; indent-tabs-mode: nil -*-
  2. %% ``The contents of this file are subject to the Erlang Public License,
  3. %% Version 1.1, (the "License"); you may not use this file except in
  4. %% compliance with the License. You should have received a copy of the
  5. %% Erlang Public License along with this software. If not, it can be
  6. %% retrieved via the world wide web at http://www.erlang.org/.
  7. %%
  8. %% Software distributed under the License is distributed on an "AS IS"
  9. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  10. %% the License for the specific language governing rights and limitations
  11. %% under the License.
  12. %%
  13. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  14. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  15. %% AB. All Rights Reserved.''
  16. %%
  17. %% @author Ulf Wiger <ulf@wiger.net>
  18. %%
  19. -module(gproc_dist_tests).
  20. -ifdef(TEST).
  21. -include_lib("eunit/include/eunit.hrl").
  22. -export([t_spawn/1, t_spawn_reg/2]).
  23. -define(f(E), fun() -> ?debugVal(E) end).
  24. dist_test_() ->
  25. {timeout, 120,
  26. [
  27. %% {setup,
  28. %% fun dist_setup/0,
  29. %% fun dist_cleanup/1,
  30. %% fun(skip) -> [];
  31. %% (Ns) when is_list(Ns) ->
  32. %% {inorder, basic_tests(Ns)}
  33. %% end
  34. %% },
  35. {foreach,
  36. fun dist_setup/0,
  37. fun dist_cleanup/1,
  38. [
  39. fun(Ns) ->
  40. [{inorder, basic_tests(Ns)}]
  41. end,
  42. fun(Ns) ->
  43. [?f(t_sync_cand_dies(Ns))]
  44. end,
  45. fun(Ns) ->
  46. [?f(t_fail_node(Ns))]
  47. end,
  48. fun(Ns) ->
  49. [?f(t_master_dies(Ns))]
  50. end
  51. ]}
  52. ]}.
  53. basic_tests(Ns) ->
  54. [
  55. ?f(t_simple_reg(Ns)),
  56. ?f(t_simple_reg_other(Ns)),
  57. ?f(t_simple_reg_or_locate(Ns)),
  58. ?f(t_simple_counter(Ns)),
  59. ?f(t_aggr_counter(Ns)),
  60. ?f(t_awaited_aggr_counter(Ns)),
  61. ?f(t_simple_resource_count(Ns)),
  62. ?f(t_awaited_resource_count(Ns)),
  63. ?f(t_resource_count_on_zero(Ns)),
  64. ?f(t_update_counters(Ns)),
  65. ?f(t_shared_counter(Ns)),
  66. ?f(t_prop(Ns)),
  67. ?f(t_mreg(Ns)),
  68. ?f(t_await_reg(Ns)),
  69. ?f(t_await_self(Ns)),
  70. ?f(t_await_reg_exists(Ns)),
  71. ?f(t_give_away(Ns)),
  72. ?f(t_sync(Ns)),
  73. ?f(t_monitor(Ns)),
  74. ?f(t_standby_monitor(Ns)),
  75. ?f(t_standby_monitor_unreg(Ns)),
  76. ?f(t_follow_monitor(Ns)),
  77. ?f(t_subscribe(Ns))
  78. ].
  79. dist_setup() ->
  80. case run_dist_tests() of
  81. true ->
  82. Ns = start_slaves([dist_test_n1, dist_test_n2, dist_test_n3]),
  83. ?assertMatch({[ok,ok,ok],[]},
  84. rpc:multicall(Ns, application, set_env,
  85. [gproc, gproc_dist, Ns])),
  86. ?assertMatch({[ok,ok,ok],[]},
  87. rpc:multicall(
  88. Ns, application, start, [gproc])),
  89. Ns;
  90. false ->
  91. skip
  92. end.
  93. dist_cleanup(Ns) ->
  94. [slave:stop(N) || N <- Ns],
  95. ok.
  96. run_dist_tests() ->
  97. case os:getenv("GPROC_DIST") of
  98. "true" -> true;
  99. "false" -> false;
  100. false ->
  101. case code:ensure_loaded(gen_leader) of
  102. {error, nofile} ->
  103. false;
  104. _ ->
  105. true
  106. end
  107. end.
  108. -define(T_NAME, {n, g, {?MODULE, ?LINE, erlang:now()}}).
  109. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  110. -define(T_COUNTER, {c, g, {?MODULE, ?LINE}}).
  111. -define(T_RESOURCE, {r, g, {?MODULE, ?LINE}}).
  112. -define(T_PROP, {p, g, ?MODULE}).
  113. t_simple_reg([H|_] = Ns) ->
  114. Name = ?T_NAME,
  115. P = t_spawn_reg(H, Name),
  116. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  117. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  118. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  119. ?assertMatch(ok, t_call(P, die)).
  120. t_simple_reg_other([A, B|_] = Ns) ->
  121. Name = ?T_NAME,
  122. P1 = t_spawn(A),
  123. P2 = t_spawn(B),
  124. ?assertMatch(true, t_call(P1, {apply, gproc, reg_other, [Name, P2]})),
  125. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P2)),
  126. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_other, [Name, P2]})),
  127. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  128. ?assertMatch(ok, t_call(P1, die)),
  129. ?assertMatch(ok, t_call(P2, die)).
  130. t_simple_reg_or_locate([A,B|_] = _Ns) ->
  131. Name = ?T_NAME,
  132. P1 = t_spawn(A),
  133. Ref = erlang:monitor(process, P1),
  134. ?assertMatch({P1, the_value},
  135. t_call(P1, {apply, gproc, reg_or_locate, [Name, the_value]})),
  136. P2 = t_spawn(B),
  137. Ref2 = erlang:monitor(process, P2),
  138. ?assertMatch({P1, the_value},
  139. t_call(P2, {apply, gproc, reg_or_locate, [Name, other_value]})),
  140. ?assertMatch(ok, t_call(P1, die)),
  141. ?assertMatch(ok, t_call(P2, die)),
  142. flush_down(Ref),
  143. flush_down(Ref2).
  144. flush_down(Ref) ->
  145. receive
  146. {'DOWN', Ref, _, _, _} ->
  147. ok
  148. after 1000 ->
  149. erlang:error({timeout, [flush_down, Ref]})
  150. end.
  151. t_simple_counter([H|_] = Ns) ->
  152. Ctr = ?T_COUNTER,
  153. P = t_spawn_reg(H, Ctr, 3),
  154. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
  155. ?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
  156. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
  157. ?assertMatch(ok, t_call(P, die)).
  158. t_shared_counter([H|_] = Ns) ->
  159. Ctr = ?T_COUNTER,
  160. P = t_spawn_reg_shared(H, Ctr, 3),
  161. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 3)),
  162. ?assertMatch(5, t_call(P, {apply, gproc, update_shared_counter, [Ctr, 2]})),
  163. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  164. ?assertMatch(ok, t_call(P, die)),
  165. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  166. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)), % twice
  167. P1 = t_spawn(H),
  168. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_shared, [Ctr]})),
  169. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, badarg)).
  170. t_aggr_counter([H1,H2|_] = Ns) ->
  171. {c,g,Nm} = Ctr = ?T_COUNTER,
  172. Aggr = {a,g,Nm},
  173. Pc1 = t_spawn_reg(H1, Ctr, 3),
  174. Pa = t_spawn_reg(H2, Aggr),
  175. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 3)),
  176. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  177. Pc2 = t_spawn_reg(H2, Ctr, 3),
  178. ?assertMatch(ok, t_read_everywhere(Ctr, Pc2, Ns, 3)),
  179. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 6)),
  180. ?assertMatch(5, t_call(Pc1, {apply, gproc, update_counter, [Ctr, 2]})),
  181. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 5)),
  182. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 8)),
  183. ?assertMatch(ok, t_call(Pc1, die)),
  184. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  185. ?assertMatch(ok, t_call(Pc2, die)),
  186. ?assertMatch(ok, t_call(Pa, die)).
  187. t_awaited_aggr_counter([H1,H2|_] = Ns) ->
  188. {c,g,Nm} = Ctr = ?T_COUNTER,
  189. Aggr = {a,g,Nm},
  190. Pc1 = t_spawn_reg(H1, Ctr, 3),
  191. P = t_spawn(H2),
  192. Ref = erlang:monitor(process, P),
  193. P ! {self(), Ref, {apply, gproc, await, [Aggr]}},
  194. t_sleep(),
  195. P1 = t_spawn_reg(H2, Aggr),
  196. ?assert(P1 == receive
  197. {P, Ref, Res} ->
  198. element(1, Res);
  199. {'DOWN', Ref, _, _, Reason} ->
  200. erlang:error(Reason);
  201. Other ->
  202. erlang:error({received, Other})
  203. end),
  204. ?assertMatch(ok, t_read_everywhere(Aggr, P1, Ns, 3)),
  205. ?assertMatch(ok, t_call(Pc1, die)),
  206. ?assertMatch(ok, t_call(P, die)),
  207. flush_down(Ref),
  208. ?assertMatch(ok, t_call(P1, die)).
  209. t_simple_resource_count([H1,H2|_] = Ns) ->
  210. {r,g,Nm} = R = ?T_RESOURCE,
  211. RC = {rc,g,Nm},
  212. Pr1 = t_spawn_reg(H1, R, 3),
  213. Prc = t_spawn_reg(H2, RC),
  214. ?assertMatch(ok, t_read_everywhere(R, Pr1, Ns, 3)),
  215. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  216. Pr2 = t_spawn_reg(H2, R, 4),
  217. ?assertMatch(ok, t_read_everywhere(R, Pr2, Ns, 4)),
  218. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 2)),
  219. ?assertMatch(ok, t_call(Pr1, die)),
  220. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  221. ?assertMatch(ok, t_call(Pr2, die)),
  222. ?assertMatch(ok, t_call(Prc, die)).
  223. t_awaited_resource_count([H1,H2|_] = Ns) ->
  224. {r,g,Nm} = R = ?T_RESOURCE,
  225. RC = {rc,g,Nm},
  226. Pr1 = t_spawn_reg(H1, R, 3),
  227. P = t_spawn(H2),
  228. Ref = erlang:monitor(process, P),
  229. P ! {self(), Ref, {apply, gproc, await, [RC]}},
  230. t_sleep(),
  231. P1 = t_spawn_reg(H2, RC),
  232. ?assert(P1 == receive
  233. {P, Ref, Res} ->
  234. element(1, Res);
  235. {'DOWN', Ref, _, _, Reason} ->
  236. erlang:error(Reason);
  237. Other ->
  238. erlang:error({received, Other})
  239. end),
  240. ?assertMatch(ok, t_read_everywhere(RC, P1, Ns, 1)),
  241. ?assertMatch(ok, t_call(Pr1, die)),
  242. ?assertMatch(ok, t_call(P, die)),
  243. flush_down(Ref),
  244. ?assertMatch(ok, t_call(P1, die)).
  245. t_resource_count_on_zero([H1,H2|_] = Ns) ->
  246. {r,g,Nm} = R = ?T_RESOURCE,
  247. Prop = ?T_PROP,
  248. RC = {rc,g,Nm},
  249. Pr1 = t_spawn_reg(H1, R, 3),
  250. Pp = t_spawn_reg(H2, Prop),
  251. ?assertMatch(ok, t_call(Pp, {selective, true})),
  252. Prc = t_spawn_reg(H2, RC, undefined, [{on_zero, [{send, Prop}]}]),
  253. ?assertMatch(ok, t_read_everywhere(R, Pr1, Ns, 3)),
  254. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  255. ?assertMatch(ok, t_call(Pr1, die)),
  256. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 0)),
  257. ?assertMatch({gproc, resource_on_zero, g, Nm, Prc},
  258. t_call(Pp, {apply_fun, fun() ->
  259. receive
  260. {gproc, _, _, _, _} = M ->
  261. M
  262. after 10000 ->
  263. timeout
  264. end
  265. end})),
  266. ?assertMatch(ok, t_call(Pp, {selective, false})),
  267. ?assertMatch(ok, t_call(Pp, die)),
  268. ?assertMatch(ok, t_call(Prc, die)).
  269. t_update_counters([H1,H2|_] = Ns) ->
  270. {c,g,N1} = C1 = ?T_COUNTER,
  271. A1 = {a,g,N1},
  272. C2 = ?T_COUNTER,
  273. P1 = t_spawn_reg(H1, C1, 2),
  274. P12 = t_spawn_reg(H2, C1, 2),
  275. P2 = t_spawn_reg(H2, C2, 1),
  276. Pa1 = t_spawn_reg(H2, A1),
  277. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 2)),
  278. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 2)),
  279. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 1)),
  280. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4)),
  281. ?assertMatch([{C1,P1, 3},
  282. {C1,P12,4},
  283. {C2,P2, 0}], t_call(P1, {apply, gproc, update_counters,
  284. [g, [{C1,P1,1},{C1,P12,2},{C2,P2,{-2,0,0}}]]})),
  285. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 3)),
  286. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 4)),
  287. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 0)),
  288. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7)),
  289. ?assertMatch(ok, t_call(P1, die)),
  290. ?assertMatch(ok, t_call(P12, die)),
  291. ?assertMatch(ok, t_call(P2, die)).
  292. t_prop([H1,H2|_] = Ns) ->
  293. {p, g, _} = P = ?T_PROP,
  294. P1 = t_spawn_reg(H1, P, 1),
  295. P2 = t_spawn_reg(H2, P, 2),
  296. ?assertMatch(ok, t_read_everywhere(P, P1, Ns, 1)),
  297. ?assertMatch(ok, t_read_everywhere(P, P2, Ns, 2)),
  298. ?assertMatch(ok, t_call(P1, die)),
  299. ?assertMatch(ok, t_read_everywhere(P, P1, Ns, badarg)),
  300. ?assertMatch(ok, t_call(P2, die)).
  301. t_mreg([H|_] = Ns) ->
  302. Kvl = ?T_KVL,
  303. Keys = [K || {K,_} <- Kvl],
  304. P = t_spawn_mreg(H, Kvl),
  305. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  306. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  307. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  308. ?assertMatch(ok, t_call(P, die)).
  309. t_await_reg([A,B|_] = Ns) ->
  310. Name = ?T_NAME,
  311. P = t_spawn(A),
  312. Ref = erlang:monitor(process, P),
  313. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  314. t_sleep(),
  315. P1 = t_spawn_reg(B, Name),
  316. ?assert(P1 == receive
  317. {P, Ref, Res} ->
  318. element(1, Res);
  319. {'DOWN', Ref, _, _, Reason} ->
  320. erlang:error(Reason);
  321. Other ->
  322. erlang:error({received,Other})
  323. end),
  324. ?assertMatch(ok, t_call(P, die)),
  325. flush_down(Ref),
  326. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P1)),
  327. ?assertMatch(ok, t_call(P1, die)).
  328. t_await_self([A|_]) ->
  329. Name = ?T_NAME,
  330. P = t_spawn(A, false), % don't buffer unknowns
  331. Ref = t_call(P, {apply, gproc, nb_wait, [Name]}),
  332. ?assertMatch(ok, t_call(P, {selective, true})),
  333. ?assertMatch(true, t_call(P, {apply, gproc, reg, [Name, some_value]})),
  334. ?assertMatch({registered, {Name, P, some_value}},
  335. t_call(P, {apply_fun, fun() ->
  336. receive
  337. {gproc, Ref, R, Wh} ->
  338. {R, Wh}
  339. after 10000 ->
  340. timeout
  341. end
  342. end})),
  343. ?assertMatch(ok, t_call(P, {selective, false})),
  344. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})).
  345. t_await_reg_exists([A,B|_]) ->
  346. Name = ?T_NAME,
  347. P = t_spawn(A),
  348. Ref = erlang:monitor(process, P),
  349. P1 = t_spawn_reg(B, Name),
  350. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  351. ?assert(P1 == receive
  352. {P, Ref, Res} ->
  353. element(1, Res);
  354. {'DOWN', Ref, _, _, Reason} ->
  355. erlang:error(Reason);
  356. Other ->
  357. erlang:error({received,Other})
  358. end),
  359. ?assertMatch(ok, t_call(P, die)),
  360. ?assertMatch(ok, t_call(P1, die)).
  361. t_give_away([A,B|_] = Ns) ->
  362. Na = ?T_NAME,
  363. Nb = ?T_NAME,
  364. Pa = t_spawn_reg(A, Na),
  365. Pb = t_spawn_reg(B, Nb),
  366. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  367. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  368. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  369. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  370. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  371. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  372. ?assertMatch(ok, t_call(Pa, die)),
  373. ?assertMatch(ok, t_call(Pb, die)).
  374. t_sync(Ns) ->
  375. %% Don't really know how to test this...
  376. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  377. || N <- Ns].
  378. t_monitor([A,B|_]) ->
  379. Na = ?T_NAME,
  380. Pa = t_spawn_reg(A, Na),
  381. Pb = t_spawn(B, _Selective = true),
  382. Ref = t_call(Pb, {apply, gproc, monitor, [Na]}),
  383. ?assert(is_reference(Ref)),
  384. ?assertMatch(ok, t_call(Pa, die)),
  385. ?assertMatch({gproc,unreg,Ref,Na}, got_msg(Pb, gproc)),
  386. Pc = t_spawn_reg(A, Na),
  387. Ref1 = t_call(Pb, {apply, gproc, monitor, [Na]}),
  388. ?assertMatch(true, t_call(Pc, {apply, gproc, unreg, [Na]})),
  389. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pb, gproc)).
  390. t_standby_monitor([A,B|_] = Ns) ->
  391. Na = ?T_NAME,
  392. Pa = t_spawn_reg(A, Na),
  393. Pb = t_spawn(B, _Selective = true),
  394. Ref = t_call(Pb, {apply, gproc, monitor, [Na, standby]}),
  395. ?assert(is_reference(Ref)),
  396. ?assertMatch(ok, t_call(Pa, die)),
  397. ?assertMatch({gproc,{failover,Pb},Ref,Na}, got_msg(Pb, gproc)),
  398. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  399. Pc = t_spawn(A, true),
  400. Ref1 = t_call(Pc, {apply, gproc, monitor, [Na, standby]}),
  401. ?assertMatch(true, t_call(Pb, {apply, gproc, unreg, [Na]})),
  402. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pc, gproc)),
  403. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)).
  404. t_standby_monitor_unreg([A,B|_] = Ns) ->
  405. Na = ?T_NAME,
  406. Pa = t_spawn(A, _Selective = true),
  407. Ref = t_call(Pa, {apply, gproc, monitor, [Na, standby]}),
  408. ?assert(is_reference(Ref)),
  409. ?assertMatch({gproc,{failover,Pa},Ref,Na}, got_msg(Pa, gproc)),
  410. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  411. ?assertMatch(ok, t_call(Pa, die)),
  412. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)).
  413. t_follow_monitor([A,B|_]) ->
  414. Na = ?T_NAME,
  415. Pa = t_spawn(A, _Selective = true),
  416. Ref = t_call(Pa, {apply, gproc, monitor, [Na, follow]}),
  417. {gproc,unreg,Ref,Na} = got_msg(Pa),
  418. Pb = t_spawn_reg(B, Na),
  419. {gproc,registered,Ref,Na} = got_msg(Pa),
  420. ok = t_call(Pb, die),
  421. ok = t_call(Pa, die).
  422. t_subscribe([A,B|_] = Ns) ->
  423. Na = ?T_NAME,
  424. Pb = t_spawn(B, _Selective = true),
  425. ?assertEqual(ok, t_call(Pb, {apply, gproc_monitor, subscribe, [Na]})),
  426. ?assertMatch({gproc_monitor, Na, undefined}, got_msg(Pb, gproc_monitor)),
  427. Pa = t_spawn_reg(A, Na),
  428. ?assertMatch({gproc_monitor, Na, Pa}, got_msg(Pb, gproc_monitor)),
  429. Pc = t_spawn(A),
  430. t_call(Pa, {apply, gproc, give_away, [Na, Pc]}),
  431. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pc)),
  432. ?assertEqual({gproc_monitor,Na,{migrated,Pc}}, got_msg(Pb, gproc_monitor)),
  433. ?assertEqual(ok, t_call(Pc, die)),
  434. ?assertEqual({gproc_monitor,Na,undefined}, got_msg(Pb, gproc_monitor)),
  435. ok.
  436. %% got_msg(Pb, Tag) ->
  437. %% t_call(Pb,
  438. %% {apply_fun,
  439. %% fun() ->
  440. %% receive
  441. %% M when element(1, M) == Tag ->
  442. %% M
  443. %% after 1000 ->
  444. %% erlang:error({timeout, got_msg, [Pb, Tag]})
  445. %% end
  446. %% end}).
  447. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  448. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  449. %% the other candidate doesn't respond too quickly.
  450. t_sync_cand_dies([A,B|_]) ->
  451. Leader = rpc:call(A, gproc_dist, get_leader, []),
  452. Other = case Leader of
  453. A -> B;
  454. B -> A
  455. end,
  456. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  457. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  458. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  459. %% The overall timeout for gproc_dist:sync() is 5 seconds. Here, we should
  460. %% still be waiting.
  461. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  462. exit(P, kill),
  463. %% The leader should detect that the other candidate died and respond
  464. %% immediately. Therefore, we should have our answer well within 1 sec.
  465. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  466. t_fail_node([A,B|_] = Ns) ->
  467. Na = ?T_NAME,
  468. Nb = ?T_NAME,
  469. Pa = t_spawn_reg(A, Na),
  470. Pb = t_spawn_reg(B, Nb),
  471. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  472. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  473. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  474. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  475. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  476. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  477. ?assertMatch(ok, t_call(Pa, die)),
  478. ?assertMatch(ok, t_call(Pb, die)).
  479. t_master_dies([A,B,C] = Ns) ->
  480. Na = ?T_NAME,
  481. Nb = ?T_NAME,
  482. Nc = ?T_NAME,
  483. Pa = t_spawn_reg(A, Na),
  484. Pb = t_spawn_reg(B, Nb),
  485. Pc = t_spawn_reg(C, Nc),
  486. L = rpc:call(A, gproc_dist, get_leader, []),
  487. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  488. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  489. ?assertMatch(ok, t_lookup_everywhere(Nc, Ns, Pc)),
  490. {Nl, Pl} = case L of
  491. A -> {Na, Pa};
  492. B -> {Nb, Pb};
  493. C -> {Nc, Pc}
  494. end,
  495. ?assertMatch(true, rpc:call(A, gproc_dist, sync, [])),
  496. ?assertMatch(ok, rpc:call(L, application, stop, [gproc])),
  497. Names = [{Na,Pa}, {Nb,Pb}, {Nc,Pc}] -- [{Nl, Pl}],
  498. RestNs = Ns -- [L],
  499. ?assertMatch(true, rpc:call(hd(RestNs), gproc_dist, sync, [])),
  500. ?assertMatch(ok, t_lookup_everywhere(Nl, RestNs, undefined)),
  501. [?assertMatch(ok, t_lookup_everywhere(Nx, RestNs, Px))
  502. || {Nx, Px} <- Names],
  503. ok.
  504. t_sleep() ->
  505. timer:sleep(500).
  506. t_lookup_everywhere(Key, Nodes, Exp) ->
  507. t_lookup_everywhere(Key, Nodes, Exp, 3).
  508. t_lookup_everywhere(Key, _, Exp, 0) ->
  509. {lookup_failed, Key, Exp};
  510. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  511. Expected = [{N, Exp} || N <- Nodes],
  512. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  513. if Expected =/= Found ->
  514. ?debugFmt("lookup ~p failed~n"
  515. "(Expected: ~p;~n"
  516. " Found : ~p), retrying...~n",
  517. [Key, Expected, Found]),
  518. t_sleep(),
  519. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  520. true ->
  521. ok
  522. end.
  523. t_read_everywhere(Key, Pid, Nodes, Exp) ->
  524. t_read_everywhere(Key, Pid, Nodes, Exp, 3).
  525. t_read_everywhere(Key, _, _, Exp, 0) ->
  526. {read_failed, Key, Exp};
  527. t_read_everywhere(Key, Pid, Nodes, Exp, I) ->
  528. Expected = [{N, Exp} || N <- Nodes],
  529. Found = [{N, read_result(rpc:call(N, gproc, get_value, [Key, Pid]))}
  530. || N <- Nodes],
  531. if Expected =/= Found ->
  532. ?debugFmt("read ~p failed~n"
  533. "(Expected: ~p;~n"
  534. " Found : ~p), retrying...~n",
  535. [{Key, Pid}, Expected, Found]),
  536. t_sleep(),
  537. t_read_everywhere(Key, Pid, Nodes, Exp, I-1);
  538. true ->
  539. ok
  540. end.
  541. read_result({badrpc, {'EXIT', {badarg, _}}}) -> badarg;
  542. read_result(R) -> R.
  543. t_spawn(Node) -> gproc_test_lib:t_spawn(Node).
  544. t_spawn(Node, Selective) -> gproc_test_lib:t_spawn(Node, Selective).
  545. t_spawn_mreg(Node, KVL) -> gproc_test_lib:t_spawn_mreg(Node, KVL).
  546. t_spawn_reg(Node, N) -> gproc_test_lib:t_spawn_reg(Node, N).
  547. t_spawn_reg(Node, N, V) -> gproc_test_lib:t_spawn_reg(Node, N, V).
  548. t_spawn_reg(Node, N, V, As) -> gproc_test_lib:t_spawn_reg(Node, N, V, As).
  549. t_spawn_reg_shared(Node, N, V) -> gproc_test_lib:t_spawn_reg_shared(Node, N, V).
  550. got_msg(P) -> gproc_test_lib:got_msg(P).
  551. got_msg(P, Tag) -> gproc_test_lib:got_msg(P, Tag).
  552. t_call(P, Req) ->
  553. gproc_test_lib:t_call(P, Req).
  554. start_slaves(Ns) ->
  555. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  556. _ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
  557. Nodes.
  558. start_slave(Name) ->
  559. case node() of
  560. nonode@nohost ->
  561. os:cmd("epmd -daemon"),
  562. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  563. _ ->
  564. ok
  565. end,
  566. {Pa, Pz} = paths(),
  567. Paths = "-pa ./ -pz ../ebin" ++
  568. lists:flatten([[" -pa " ++ Path || Path <- Pa],
  569. [" -pz " ++ Path || Path <- Pz]]),
  570. {ok, Node} = slave:start(host(), Name, Paths),
  571. Node.
  572. paths() ->
  573. Path = code:get_path(),
  574. {ok, [[Root]]} = init:get_argument(root),
  575. {Pas, Rest} = lists:splitwith(fun(P) ->
  576. not lists:prefix(Root, P)
  577. end, Path),
  578. {_, Pzs} = lists:splitwith(fun(P) ->
  579. lists:prefix(Root, P)
  580. end, Rest),
  581. {Pas, Pzs}.
  582. host() ->
  583. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  584. list_to_atom(Host).
  585. -endif.