gproc_dist_tests.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, 90,
  24. [{setup,
  25. fun() ->
  26. Ns = start_slaves([n1, n2]),
  27. ?assertMatch({[ok,ok],[]},
  28. rpc:multicall(Ns, application, start, [gproc])),
  29. %% Without this trace output, the test times out on my Mac...
  30. dbg:tracer(),
  31. dbg:tpl(?MODULE, x),
  32. dbg:p(all,[c]),
  33. ?debugVal(Ns)
  34. end,
  35. fun(Ns) ->
  36. [rpc:call(N, init, stop, []) || N <- Ns]
  37. end,
  38. fun(Ns) ->
  39. {inorder,
  40. [
  41. {inparallel, [fun() ->
  42. ?debugVal(t_simple_reg(Ns))
  43. end,
  44. fun() ->
  45. ?debugVal(t_await_reg(Ns))
  46. end,
  47. fun() ->
  48. ?debugVal(t_await_reg_exists(Ns))
  49. end,
  50. fun() ->
  51. ?debugVal(t_give_away(Ns))
  52. end
  53. ]
  54. },
  55. {timeout, 60, [fun() ->
  56. ?debugVal(t_fail_node(Ns))
  57. end]}
  58. ]}
  59. end
  60. }]}.
  61. -define(T_NAME, {n, g, {?MODULE, ?LINE}}).
  62. t_simple_reg([H|_] = Ns) ->
  63. ?debugMsg(t_simple_reg),
  64. Name = ?T_NAME,
  65. P = t_spawn_reg(H, Name),
  66. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  67. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  68. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  69. ?assertMatch(ok, t_call(P, die)).
  70. t_await_reg([A,B|_]) ->
  71. ?debugMsg(t_await_reg),
  72. Name = ?T_NAME,
  73. P = t_spawn(A),
  74. Ref = erlang:monitor(process, P),
  75. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  76. t_sleep(),
  77. P1 = t_spawn_reg(B, Name),
  78. ?assert(P1 == receive
  79. {P, Ref, Res} ->
  80. element(1, Res);
  81. {'DOWN', Ref, _, _, Reason} ->
  82. erlang:error(Reason);
  83. Other ->
  84. erlang:error({received,Other})
  85. end),
  86. ?assertMatch(ok, t_call(P, die)),
  87. ?assertMatch(ok, t_call(P1, die)).
  88. t_await_reg_exists([A,B|_]) ->
  89. ?debugMsg(t_await_reg_exists),
  90. Name = ?T_NAME,
  91. P = t_spawn(A),
  92. Ref = erlang:monitor(process, P),
  93. %% dbg:tracer(),
  94. %% [dbg:n(N) || N <- Ns],
  95. %% dbg:tpl(gproc_dist,x),
  96. %% dbg:tpl(gproc_lib,await,x),
  97. %% dbg:p(all,[c]),
  98. P1 = t_spawn_reg(B, Name),
  99. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  100. ?assert(P1 == receive
  101. {P, Ref, Res} ->
  102. element(1, Res);
  103. {'DOWN', Ref, _, _, Reason} ->
  104. erlang:error(Reason);
  105. Other ->
  106. erlang:error({received,Other})
  107. end),
  108. ?assertMatch(ok, t_call(P, die)),
  109. ?assertMatch(ok, t_call(P1, die)).
  110. t_give_away([A,B|_] = Ns) ->
  111. ?debugMsg(t_give_away),
  112. Na = ?T_NAME,
  113. Nb = ?T_NAME,
  114. Pa = t_spawn_reg(A, Na),
  115. Pb = t_spawn_reg(B, Nb),
  116. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  117. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  118. ?assertMatch(Pb, t_call(Pa, {apply, {gproc, give_away, [Na, Nb]}})),
  119. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  120. ?assertMatch(Pa, t_call(Pa, {apply, {gproc, give_away, [Na, Pa]}})),
  121. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  122. ?assertMatch(ok, t_call(Pa, die)),
  123. ?assertMatch(ok, t_call(Pb, die)).
  124. t_fail_node([A,B|_] = Ns) ->
  125. ?debugMsg(t_fail_node),
  126. Na = ?T_NAME,
  127. Nb = ?T_NAME,
  128. Pa = t_spawn_reg(A, Na),
  129. Pb = t_spawn_reg(B, Nb),
  130. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  131. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  132. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  133. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  134. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  135. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  136. ?assertMatch(ok, t_call(Pa, die)),
  137. ?assertMatch(ok, t_call(Pb, die)).
  138. t_sleep() ->
  139. timer:sleep(500).
  140. t_lookup_everywhere(Key, Nodes, Exp) ->
  141. t_lookup_everywhere(Key, Nodes, Exp, 3).
  142. t_lookup_everywhere(Key, _, Exp, 0) ->
  143. {lookup_failed, Key, Exp};
  144. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  145. Expected = [{N, Exp} || N <- Nodes],
  146. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  147. if Expected =/= Found ->
  148. ?debugFmt("lookup ~p failed (~p), retrying...~n", [Key, Found]),
  149. t_sleep(),
  150. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  151. true ->
  152. ok
  153. end.
  154. t_spawn(Node) ->
  155. Me = self(),
  156. P = spawn(Node, fun() ->
  157. Me ! {self(), ok},
  158. t_loop()
  159. end),
  160. receive
  161. {P, ok} -> P
  162. end.
  163. t_spawn_reg(Node, Name) ->
  164. Me = self(),
  165. spawn(Node, fun() ->
  166. ?assertMatch(true, gproc:reg(Name)),
  167. Me ! {self(), ok},
  168. t_loop()
  169. end),
  170. receive
  171. {P, ok} -> P
  172. end.
  173. t_call(P, Req) ->
  174. Ref = erlang:monitor(process, P),
  175. P ! {self(), Ref, Req},
  176. receive
  177. {P, Ref, Res} ->
  178. erlang:demonitor(Ref),
  179. Res;
  180. {'DOWN', Ref, _, _, Error} ->
  181. erlang:error({'DOWN', P, Error})
  182. end.
  183. t_loop() ->
  184. receive
  185. {From, Ref, die} ->
  186. From ! {self(), Ref, ok};
  187. {From, Ref, {apply, M, F, A}} ->
  188. From ! {self(), Ref, apply(M, F, A)},
  189. t_loop()
  190. end.
  191. start_slaves(Ns) ->
  192. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  193. _ = [{N, rpc:call(H, net, ping, [N])} || N <- T],
  194. Nodes.
  195. start_slave(Name) ->
  196. case node() of
  197. nonode@nohost ->
  198. os:cmd("epmd -daemon"),
  199. {ok, _} = net_kernel:start([gproc_master, longnames]);
  200. _ ->
  201. ok
  202. end,
  203. {ok, Node} = slave:start(
  204. host(), Name,
  205. "-pa . -pz ../ebin -pa ../deps/gen_leader/ebin "
  206. "-gproc gproc_dist all"),
  207. %% io:fwrite(user, "Slave node: ~p~n", [Node]),
  208. Node.
  209. host() ->
  210. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  211. list_to_atom(Host).
  212. -endif.