gproc_dist_tests.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
  17. %%
  18. -module(gproc_dist_tests).
  19. -ifdef(TEST).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -export([t_spawn/1, t_spawn_reg/2]).
  22. dist_test_() ->
  23. {timeout, 120,
  24. [{setup,
  25. fun() ->
  26. Ns = start_slaves([dist_test_n1, dist_test_n2]),
  27. ?assertMatch({[ok,ok],[]},
  28. rpc:multicall(Ns, application, set_env,
  29. [gproc, gproc_dist, Ns])),
  30. ?assertMatch({[ok,ok],[]},
  31. rpc:multicall(Ns, application, start, [gproc])),
  32. Ns
  33. end,
  34. fun(Ns) ->
  35. [rpc:call(N, init, stop, []) || N <- Ns]
  36. end,
  37. fun(Ns) ->
  38. {inorder,
  39. [
  40. {inparallel, [
  41. fun() ->
  42. ?debugVal(t_simple_reg(Ns))
  43. end,
  44. fun() ->
  45. ?debugVal(t_simple_counter(Ns))
  46. end,
  47. fun() ->
  48. ?debugVal(t_aggr_counter(Ns))
  49. end,
  50. fun() ->
  51. ?debugVal(t_shared_counter(Ns))
  52. end,
  53. fun() ->
  54. ?debugVal(t_mreg(Ns))
  55. end,
  56. fun() ->
  57. ?debugVal(t_await_reg(Ns))
  58. end,
  59. fun() ->
  60. ?debugVal(t_await_self(Ns))
  61. end,
  62. fun() ->
  63. ?debugVal(t_await_reg_exists(Ns))
  64. end,
  65. fun() ->
  66. ?debugVal(t_give_away(Ns))
  67. end,
  68. fun() ->
  69. ?debugVal(t_sync(Ns))
  70. end
  71. ]
  72. },
  73. fun() ->
  74. ?debugVal(t_sync_cand_dies(Ns))
  75. end,
  76. {timeout, 90, [fun() ->
  77. ?debugVal(t_fail_node(Ns))
  78. end]}
  79. ]}
  80. end
  81. }]}.
  82. -define(T_NAME, {n, g, {?MODULE, ?LINE}}).
  83. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  84. -define(T_COUNTER, {c, g, {?MODULE, ?LINE}}).
  85. t_simple_reg([H|_] = Ns) ->
  86. Name = ?T_NAME,
  87. P = t_spawn_reg(H, Name),
  88. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  89. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  90. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  91. ?assertMatch(ok, t_call(P, die)).
  92. t_simple_counter([H|_] = Ns) ->
  93. Ctr = ?T_COUNTER,
  94. P = t_spawn_reg(H, Ctr, 3),
  95. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
  96. ?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
  97. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
  98. ?assertMatch(ok, t_call(P, die)).
  99. t_shared_counter([H|_] = Ns) ->
  100. Ctr = ?T_COUNTER,
  101. P = t_spawn_reg_shared(H, Ctr, 3),
  102. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 3)),
  103. ?assertMatch(5, t_call(P, {apply, gproc, update_shared_counter, [Ctr, 2]})),
  104. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  105. ?assertMatch(ok, t_call(P, die)),
  106. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  107. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)), % twice
  108. P1 = t_spawn(H),
  109. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_shared, [Ctr]})),
  110. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, badarg)).
  111. t_aggr_counter([H1,H2|_] = Ns) ->
  112. {c,g,Nm} = Ctr = ?T_COUNTER,
  113. Aggr = {a,g,Nm},
  114. Pc1 = t_spawn_reg(H1, Ctr, 3),
  115. Pa = t_spawn_reg(H2, Aggr),
  116. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 3)),
  117. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  118. Pc2 = t_spawn_reg(H2, Ctr, 3),
  119. ?assertMatch(ok, t_read_everywhere(Ctr, Pc2, Ns, 3)),
  120. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 6)),
  121. ?assertMatch(5, t_call(Pc1, {apply, gproc, update_counter, [Ctr, 2]})),
  122. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 5)),
  123. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 8)),
  124. ?assertMatch(ok, t_call(Pc1, die)),
  125. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  126. ?assertMatch(ok, t_call(Pc2, die)),
  127. ?assertMatch(ok, t_call(Pa, die)).
  128. t_mreg([H|_] = Ns) ->
  129. Kvl = ?T_KVL,
  130. Keys = [K || {K,_} <- Kvl],
  131. P = t_spawn_mreg(H, Kvl),
  132. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  133. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  134. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  135. ?assertMatch(ok, t_call(P, die)).
  136. t_await_reg([A,B|_]) ->
  137. Name = ?T_NAME,
  138. P = t_spawn(A),
  139. Ref = erlang:monitor(process, P),
  140. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  141. t_sleep(),
  142. P1 = t_spawn_reg(B, Name),
  143. ?assert(P1 == receive
  144. {P, Ref, Res} ->
  145. element(1, Res);
  146. {'DOWN', Ref, _, _, Reason} ->
  147. erlang:error(Reason);
  148. Other ->
  149. erlang:error({received,Other})
  150. end),
  151. ?assertMatch(ok, t_call(P, die)),
  152. ?assertMatch(ok, t_call(P1, die)).
  153. t_await_self([A|_]) ->
  154. Name = ?T_NAME,
  155. P = t_spawn(A, false), % buffer unknowns
  156. Ref = t_call(P, {apply, gproc, nb_wait, [Name]}),
  157. ?assertMatch(ok, t_call(P, {selective, true})),
  158. ?assertMatch(true, t_call(P, {apply, gproc, reg, [Name, some_value]})),
  159. ?assertMatch({registered, {Name, P, some_value}},
  160. t_call(P, {apply_fun, fun() ->
  161. receive
  162. {gproc, Ref, R, Wh} ->
  163. {R, Wh}
  164. after 10000 ->
  165. timeout
  166. end
  167. end})),
  168. ?assertMatch(ok, t_call(P, {selective, false})),
  169. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})).
  170. t_await_reg_exists([A,B|_]) ->
  171. Name = ?T_NAME,
  172. P = t_spawn(A),
  173. Ref = erlang:monitor(process, P),
  174. P1 = t_spawn_reg(B, Name),
  175. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  176. ?assert(P1 == receive
  177. {P, Ref, Res} ->
  178. element(1, Res);
  179. {'DOWN', Ref, _, _, Reason} ->
  180. erlang:error(Reason);
  181. Other ->
  182. erlang:error({received,Other})
  183. end),
  184. ?assertMatch(ok, t_call(P, die)),
  185. ?assertMatch(ok, t_call(P1, die)).
  186. t_give_away([A,B|_] = Ns) ->
  187. Na = ?T_NAME,
  188. Nb = ?T_NAME,
  189. Pa = t_spawn_reg(A, Na),
  190. Pb = t_spawn_reg(B, Nb),
  191. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  192. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  193. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  194. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  195. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  196. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  197. ?assertMatch(ok, t_call(Pa, die)),
  198. ?assertMatch(ok, t_call(Pb, die)).
  199. t_sync(Ns) ->
  200. %% Don't really know how to test this...
  201. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  202. || N <- Ns].
  203. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  204. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  205. %% the other candidate doesn't respond too quickly.
  206. t_sync_cand_dies([A,B|_]) ->
  207. Leader = rpc:call(A, gproc_dist, get_leader, []),
  208. Other = case Leader of
  209. A -> B;
  210. B -> A
  211. end,
  212. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  213. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  214. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  215. %% The overall timeout for gproc_dist:sync() is 5 seconds. Here, we should
  216. %% still be waiting.
  217. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  218. exit(P, kill),
  219. %% The leader should detect that the other candidate died and respond
  220. %% immediately. Therefore, we should have our answer well within 1 sec.
  221. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  222. t_fail_node([A,B|_] = Ns) ->
  223. Na = ?T_NAME,
  224. Nb = ?T_NAME,
  225. Pa = t_spawn_reg(A, Na),
  226. Pb = t_spawn_reg(B, Nb),
  227. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  228. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  229. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  230. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  231. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  232. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  233. ?assertMatch(ok, t_call(Pa, die)),
  234. ?assertMatch(ok, t_call(Pb, die)).
  235. t_sleep() ->
  236. timer:sleep(500).
  237. t_lookup_everywhere(Key, Nodes, Exp) ->
  238. t_lookup_everywhere(Key, Nodes, Exp, 3).
  239. t_lookup_everywhere(Key, _, Exp, 0) ->
  240. {lookup_failed, Key, Exp};
  241. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  242. Expected = [{N, Exp} || N <- Nodes],
  243. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  244. if Expected =/= Found ->
  245. ?debugFmt("lookup ~p failed~n"
  246. "(Expected: ~p;~n"
  247. " Found : ~p), retrying...~n",
  248. [Key, Expected, Found]),
  249. t_sleep(),
  250. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  251. true ->
  252. ok
  253. end.
  254. t_read_everywhere(Key, Pid, Nodes, Exp) ->
  255. t_read_everywhere(Key, Pid, Nodes, Exp, 3).
  256. t_read_everywhere(Key, _, _, Exp, 0) ->
  257. {read_failed, Key, Exp};
  258. t_read_everywhere(Key, Pid, Nodes, Exp, I) ->
  259. Expected = [{N, Exp} || N <- Nodes],
  260. Found = [{N, read_result(rpc:call(N, gproc, get_value, [Key, Pid]))}
  261. || N <- Nodes],
  262. if Expected =/= Found ->
  263. ?debugFmt("read ~p failed~n"
  264. "(Expected: ~p;~n"
  265. " Found : ~p), retrying...~n",
  266. [{Key, Pid}, Expected, Found]),
  267. t_sleep(),
  268. t_read_everywhere(Key, Pid, Nodes, Exp, I-1);
  269. true ->
  270. ok
  271. end.
  272. read_result({badrpc, {'EXIT', {badarg, _}}}) -> badarg;
  273. read_result(R) -> R.
  274. t_spawn(Node) ->
  275. t_spawn(Node, false).
  276. t_spawn(Node, Selective) when is_boolean(Selective) ->
  277. Me = self(),
  278. P = spawn(Node, fun() ->
  279. Me ! {self(), ok},
  280. t_loop(Selective)
  281. end),
  282. receive
  283. {P, ok} -> P
  284. end.
  285. t_spawn_reg(Node, Name) ->
  286. t_spawn_reg(Node, Name, default_value(Name)).
  287. t_spawn_reg(Node, Name, Value) ->
  288. Me = self(),
  289. spawn(Node, fun() ->
  290. ?assertMatch(true, gproc:reg(Name, Value)),
  291. Me ! {self(), ok},
  292. t_loop()
  293. end),
  294. receive
  295. {P, ok} -> P
  296. end.
  297. t_spawn_reg_shared(Node, Name, Value) ->
  298. Me = self(),
  299. spawn(Node, fun() ->
  300. ?assertMatch(true, gproc:reg_shared(Name, Value)),
  301. Me ! {self(), ok},
  302. t_loop()
  303. end),
  304. receive
  305. {P, ok} -> P
  306. end.
  307. default_value({c,_,_}) -> 0;
  308. default_value(_) -> undefined.
  309. t_spawn_mreg(Node, KVL) ->
  310. Me = self(),
  311. spawn(Node, fun() ->
  312. ?assertMatch(true, gproc:mreg(n, g, KVL)),
  313. Me ! {self(), ok},
  314. t_loop()
  315. end),
  316. receive
  317. {P, ok} -> P
  318. end.
  319. t_call(P, Req) ->
  320. Ref = erlang:monitor(process, P),
  321. P ! {self(), Ref, Req},
  322. receive
  323. {P, Ref, Res} ->
  324. erlang:demonitor(Ref),
  325. Res;
  326. {'DOWN', Ref, _, _, Error} ->
  327. erlang:error({'DOWN', P, Error})
  328. end.
  329. t_loop() ->
  330. t_loop(false).
  331. t_loop(Selective) when is_boolean(Selective) ->
  332. receive
  333. {From, Ref, die} ->
  334. From ! {self(), Ref, ok};
  335. {From, Ref, {selective, Bool}} when is_boolean(Bool) ->
  336. From ! {self(), Ref, ok},
  337. t_loop(Bool);
  338. {From, Ref, {apply, M, F, A}} ->
  339. From ! {self(), Ref, apply(M, F, A)},
  340. t_loop(Selective);
  341. {From, Ref, {apply_fun, F}} ->
  342. From ! {self(), Ref, F()},
  343. t_loop(Selective);
  344. Other when not Selective ->
  345. ?debugFmt("got unknown msg: ~p~n", [Other]),
  346. exit({unknown_msg, Other})
  347. end.
  348. start_slaves(Ns) ->
  349. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  350. _ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
  351. Nodes.
  352. start_slave(Name) ->
  353. case node() of
  354. nonode@nohost ->
  355. os:cmd("epmd -daemon"),
  356. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  357. _ ->
  358. ok
  359. end,
  360. {Pa, Pz} = paths(),
  361. Paths = "-pa ./ -pz ../ebin" ++
  362. lists:flatten([[" -pa " ++ Path || Path <- Pa],
  363. [" -pz " ++ Path || Path <- Pz]]),
  364. {ok, Node} = slave:start(host(), Name, Paths),
  365. %% io:fwrite(user, "Slave node: ~p~n", [Node]),
  366. Node.
  367. paths() ->
  368. Path = code:get_path(),
  369. {ok, [[Root]]} = init:get_argument(root),
  370. {Pas, Rest} = lists:splitwith(fun(P) ->
  371. not lists:prefix(Root, P)
  372. end, Path),
  373. {_, Pzs} = lists:splitwith(fun(P) ->
  374. lists:prefix(Root, P)
  375. end, Rest),
  376. {Pas, Pzs}.
  377. host() ->
  378. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  379. list_to_atom(Host).
  380. -endif.