gproc_dist_tests.erl 23 KB

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