gproc_dist_tests.erl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_mreg(Ns))
  46. end,
  47. fun() ->
  48. ?debugVal(t_await_reg(Ns))
  49. end,
  50. fun() ->
  51. ?debugVal(t_await_reg_exists(Ns))
  52. end,
  53. fun() ->
  54. ?debugVal(t_give_away(Ns))
  55. end,
  56. fun() ->
  57. ?debugVal(t_sync(Ns))
  58. end
  59. ]
  60. },
  61. fun() ->
  62. ?debugVal(t_sync_cand_dies(Ns))
  63. end,
  64. {timeout, 90, [fun() ->
  65. ?debugVal(t_fail_node(Ns))
  66. end]}
  67. ]}
  68. end
  69. }]}.
  70. -define(T_NAME, {n, g, {?MODULE, ?LINE}}).
  71. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  72. t_simple_reg([H|_] = Ns) ->
  73. Name = ?T_NAME,
  74. P = t_spawn_reg(H, Name),
  75. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  76. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  77. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  78. ?assertMatch(ok, t_call(P, die)).
  79. t_mreg([H|_] = Ns) ->
  80. Kvl = ?T_KVL,
  81. Keys = [K || {K,_} <- Kvl],
  82. P = t_spawn_mreg(H, Kvl),
  83. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  84. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  85. timer:sleep(1000),
  86. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  87. ?assertMatch(ok, t_call(P, die)).
  88. t_await_reg([A,B|_]) ->
  89. Name = ?T_NAME,
  90. P = t_spawn(A),
  91. Ref = erlang:monitor(process, P),
  92. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  93. t_sleep(),
  94. P1 = t_spawn_reg(B, Name),
  95. ?assert(P1 == receive
  96. {P, Ref, Res} ->
  97. element(1, Res);
  98. {'DOWN', Ref, _, _, Reason} ->
  99. erlang:error(Reason);
  100. Other ->
  101. erlang:error({received,Other})
  102. end),
  103. ?assertMatch(ok, t_call(P, die)),
  104. ?assertMatch(ok, t_call(P1, die)).
  105. t_await_reg_exists([A,B|_]) ->
  106. Name = ?T_NAME,
  107. P = t_spawn(A),
  108. Ref = erlang:monitor(process, P),
  109. P1 = t_spawn_reg(B, Name),
  110. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  111. ?assert(P1 == receive
  112. {P, Ref, Res} ->
  113. element(1, Res);
  114. {'DOWN', Ref, _, _, Reason} ->
  115. erlang:error(Reason);
  116. Other ->
  117. erlang:error({received,Other})
  118. end),
  119. ?assertMatch(ok, t_call(P, die)),
  120. ?assertMatch(ok, t_call(P1, die)).
  121. t_give_away([A,B|_] = Ns) ->
  122. Na = ?T_NAME,
  123. Nb = ?T_NAME,
  124. Pa = t_spawn_reg(A, Na),
  125. Pb = t_spawn_reg(B, Nb),
  126. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  127. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  128. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  129. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  130. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  131. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  132. ?assertMatch(ok, t_call(Pa, die)),
  133. ?assertMatch(ok, t_call(Pb, die)).
  134. t_sync(Ns) ->
  135. %% Don't really know how to test this...
  136. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  137. || N <- Ns].
  138. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  139. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  140. %% the other candidate doesn't respond too quickly.
  141. t_sync_cand_dies([A,B|_]) ->
  142. Leader = rpc:call(A, gproc_dist, get_leader, []),
  143. Other = case Leader of
  144. A -> B;
  145. B -> A
  146. end,
  147. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  148. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  149. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  150. %% The overall timeout for gproc_dist:sync() is 5 seconds. Here, we should
  151. %% still be waiting.
  152. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  153. exit(P, kill),
  154. %% The leader should detect that the other candidate died and respond
  155. %% immediately. Therefore, we should have our answer well within 1 sec.
  156. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  157. t_fail_node([A,B|_] = Ns) ->
  158. Na = ?T_NAME,
  159. Nb = ?T_NAME,
  160. Pa = t_spawn_reg(A, Na),
  161. Pb = t_spawn_reg(B, Nb),
  162. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  163. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  164. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  165. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  166. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  167. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  168. ?assertMatch(ok, t_call(Pa, die)),
  169. ?assertMatch(ok, t_call(Pb, die)).
  170. t_sleep() ->
  171. timer:sleep(500).
  172. t_lookup_everywhere(Key, Nodes, Exp) ->
  173. t_lookup_everywhere(Key, Nodes, Exp, 3).
  174. t_lookup_everywhere(Key, _, Exp, 0) ->
  175. {lookup_failed, Key, Exp};
  176. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  177. Expected = [{N, Exp} || N <- Nodes],
  178. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  179. if Expected =/= Found ->
  180. ?debugFmt("lookup ~p failed (~p), retrying...~n", [Key, Found]),
  181. t_sleep(),
  182. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  183. true ->
  184. ok
  185. end.
  186. t_spawn(Node) ->
  187. Me = self(),
  188. P = spawn(Node, fun() ->
  189. Me ! {self(), ok},
  190. t_loop()
  191. end),
  192. receive
  193. {P, ok} -> P
  194. end.
  195. t_spawn_reg(Node, Name) ->
  196. Me = self(),
  197. spawn(Node, fun() ->
  198. ?assertMatch(true, gproc:reg(Name)),
  199. Me ! {self(), ok},
  200. t_loop()
  201. end),
  202. receive
  203. {P, ok} -> P
  204. end.
  205. t_spawn_mreg(Node, KVL) ->
  206. Me = self(),
  207. spawn(Node, fun() ->
  208. ?assertMatch(true, gproc:mreg(n, g, KVL)),
  209. Me ! {self(), ok},
  210. t_loop()
  211. end),
  212. receive
  213. {P, ok} -> P
  214. end.
  215. t_call(P, Req) ->
  216. Ref = erlang:monitor(process, P),
  217. P ! {self(), Ref, Req},
  218. receive
  219. {P, Ref, Res} ->
  220. erlang:demonitor(Ref),
  221. Res;
  222. {'DOWN', Ref, _, _, Error} ->
  223. erlang:error({'DOWN', P, Error})
  224. end.
  225. t_loop() ->
  226. receive
  227. {From, Ref, die} ->
  228. From ! {self(), Ref, ok};
  229. {From, Ref, {apply, M, F, A}} ->
  230. From ! {self(), Ref, apply(M, F, A)},
  231. t_loop();
  232. Other ->
  233. ?debugFmt("got unknown msg: ~p~n", [Other]),
  234. exit({unknown_msg, Other})
  235. end.
  236. start_slaves(Ns) ->
  237. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  238. _ = [rpc:call(H, net, ping, [N]) || N <- T],
  239. Nodes.
  240. start_slave(Name) ->
  241. case node() of
  242. nonode@nohost ->
  243. os:cmd("epmd -daemon"),
  244. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  245. _ ->
  246. ok
  247. end,
  248. {ok, Node} = slave:start(
  249. host(), Name,
  250. "-pa . -pz ../ebin -pa ../deps/gen_leader/ebin "),
  251. %% io:fwrite(user, "Slave node: ~p~n", [Node]),
  252. Node.
  253. host() ->
  254. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  255. list_to_atom(Host).
  256. -endif.