gproc_dist_tests.erl 21 KB

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