gproc_dist_tests.erl 8.0 KB

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