gproc_dist_tests.erl 7.8 KB

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