gproc_dist_tests.erl 6.1 KB

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