gproc_dist_tests.erl 25 KB

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