gproc_dist_tests.erl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. {timeout, 90, [fun() ->
  62. ?debugVal(t_fail_node(Ns))
  63. end]}
  64. ]}
  65. end
  66. }]}.
  67. -define(T_NAME, {n, g, {?MODULE, ?LINE}}).
  68. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  69. t_simple_reg([H|_] = Ns) ->
  70. Name = ?T_NAME,
  71. P = t_spawn_reg(H, Name),
  72. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  73. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  74. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  75. ?assertMatch(ok, t_call(P, die)).
  76. t_mreg([H|_]) ->
  77. Kvl = ?T_KVL,
  78. P = t_spawn_mreg(H, Kvl),
  79. ?assertMatch(ok, t_call(P, die)).
  80. t_await_reg([A,B|_]) ->
  81. Name = ?T_NAME,
  82. P = t_spawn(A),
  83. Ref = erlang:monitor(process, P),
  84. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  85. t_sleep(),
  86. P1 = t_spawn_reg(B, Name),
  87. ?assert(P1 == receive
  88. {P, Ref, Res} ->
  89. element(1, Res);
  90. {'DOWN', Ref, _, _, Reason} ->
  91. erlang:error(Reason);
  92. Other ->
  93. erlang:error({received,Other})
  94. end),
  95. ?assertMatch(ok, t_call(P, die)),
  96. ?assertMatch(ok, t_call(P1, die)).
  97. t_await_reg_exists([A,B|_]) ->
  98. Name = ?T_NAME,
  99. P = t_spawn(A),
  100. Ref = erlang:monitor(process, P),
  101. P1 = t_spawn_reg(B, Name),
  102. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  103. ?assert(P1 == receive
  104. {P, Ref, Res} ->
  105. element(1, Res);
  106. {'DOWN', Ref, _, _, Reason} ->
  107. erlang:error(Reason);
  108. Other ->
  109. erlang:error({received,Other})
  110. end),
  111. ?assertMatch(ok, t_call(P, die)),
  112. ?assertMatch(ok, t_call(P1, die)).
  113. t_give_away([A,B|_] = Ns) ->
  114. Na = ?T_NAME,
  115. Nb = ?T_NAME,
  116. Pa = t_spawn_reg(A, Na),
  117. Pb = t_spawn_reg(B, Nb),
  118. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  119. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  120. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  121. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  122. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  123. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  124. ?assertMatch(ok, t_call(Pa, die)),
  125. ?assertMatch(ok, t_call(Pb, die)).
  126. t_sync(Ns) ->
  127. %% Don't really know how to test this...
  128. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  129. || N <- Ns].
  130. t_fail_node([A,B|_] = Ns) ->
  131. Na = ?T_NAME,
  132. Nb = ?T_NAME,
  133. Pa = t_spawn_reg(A, Na),
  134. Pb = t_spawn_reg(B, Nb),
  135. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  136. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  137. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  138. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  139. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  140. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  141. ?assertMatch(ok, t_call(Pa, die)),
  142. ?assertMatch(ok, t_call(Pb, die)).
  143. t_sleep() ->
  144. timer:sleep(500).
  145. t_lookup_everywhere(Key, Nodes, Exp) ->
  146. t_lookup_everywhere(Key, Nodes, Exp, 3).
  147. t_lookup_everywhere(Key, _, Exp, 0) ->
  148. {lookup_failed, Key, Exp};
  149. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  150. Expected = [{N, Exp} || N <- Nodes],
  151. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  152. if Expected =/= Found ->
  153. ?debugFmt("lookup ~p failed (~p), retrying...~n", [Key, Found]),
  154. t_sleep(),
  155. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  156. true ->
  157. ok
  158. end.
  159. t_spawn(Node) ->
  160. Me = self(),
  161. P = spawn(Node, fun() ->
  162. Me ! {self(), ok},
  163. t_loop()
  164. end),
  165. receive
  166. {P, ok} -> P
  167. end.
  168. t_spawn_reg(Node, Name) ->
  169. Me = self(),
  170. spawn(Node, fun() ->
  171. ?assertMatch(true, gproc:reg(Name)),
  172. Me ! {self(), ok},
  173. t_loop()
  174. end),
  175. receive
  176. {P, ok} -> P
  177. end.
  178. t_spawn_mreg(Node, KVL) ->
  179. Me = self(),
  180. spawn(Node, fun() ->
  181. ?assertMatch(true, gproc:mreg(p, g, KVL)),
  182. Me ! {self(), ok},
  183. t_loop()
  184. end),
  185. receive
  186. {P, ok} -> P
  187. end.
  188. t_call(P, Req) ->
  189. Ref = erlang:monitor(process, P),
  190. P ! {self(), Ref, Req},
  191. receive
  192. {P, Ref, Res} ->
  193. erlang:demonitor(Ref),
  194. Res;
  195. {'DOWN', Ref, _, _, Error} ->
  196. erlang:error({'DOWN', P, Error})
  197. end.
  198. t_loop() ->
  199. receive
  200. {From, Ref, die} ->
  201. From ! {self(), Ref, ok};
  202. {From, Ref, {apply, M, F, A}} ->
  203. From ! {self(), Ref, apply(M, F, A)},
  204. t_loop();
  205. Other ->
  206. ?debugFmt("got unknown msg: ~p~n", [Other]),
  207. exit({unknown_msg, Other})
  208. end.
  209. start_slaves(Ns) ->
  210. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  211. _ = [rpc:call(H, net, ping, [N]) || N <- T],
  212. Nodes.
  213. start_slave(Name) ->
  214. case node() of
  215. nonode@nohost ->
  216. os:cmd("epmd -daemon"),
  217. {ok, _} = net_kernel:start([gproc_master, longnames]);
  218. _ ->
  219. ok
  220. end,
  221. {ok, Node} = slave:start(
  222. host(), Name,
  223. "-pa . -pz ../ebin -pa ../deps/gen_leader/ebin "),
  224. %% io:fwrite(user, "Slave node: ~p~n", [Node]),
  225. Node.
  226. host() ->
  227. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  228. list_to_atom(Host).
  229. -endif.