gproc_dist_tests.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_simple_counter(Ns))
  46. end,
  47. fun() ->
  48. ?debugVal(t_aggr_counter(Ns))
  49. end,
  50. fun() ->
  51. ?debugVal(t_update_counters(Ns))
  52. end,
  53. fun() ->
  54. ?debugVal(t_shared_counter(Ns))
  55. end,
  56. fun() ->
  57. ?debugVal(t_mreg(Ns))
  58. end,
  59. fun() ->
  60. ?debugVal(t_await_reg(Ns))
  61. end,
  62. fun() ->
  63. ?debugVal(t_await_self(Ns))
  64. end,
  65. fun() ->
  66. ?debugVal(t_await_reg_exists(Ns))
  67. end,
  68. fun() ->
  69. ?debugVal(t_give_away(Ns))
  70. end,
  71. fun() ->
  72. ?debugVal(t_sync(Ns))
  73. end,
  74. fun() ->
  75. ?debugVal(t_monitor(Ns))
  76. end,
  77. fun() ->
  78. ?debugVal(t_subscribe(Ns))
  79. end
  80. ]
  81. },
  82. fun() ->
  83. ?debugVal(t_sync_cand_dies(Ns))
  84. end,
  85. {timeout, 90, [fun() ->
  86. ?debugVal(t_fail_node(Ns))
  87. end]}
  88. ]}
  89. end
  90. }]}.
  91. -define(T_NAME, {n, g, {?MODULE, ?LINE}}).
  92. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  93. -define(T_COUNTER, {c, g, {?MODULE, ?LINE}}).
  94. t_simple_reg([H|_] = Ns) ->
  95. Name = ?T_NAME,
  96. P = t_spawn_reg(H, Name),
  97. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  98. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  99. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  100. ?assertMatch(ok, t_call(P, die)).
  101. t_simple_counter([H|_] = Ns) ->
  102. Ctr = ?T_COUNTER,
  103. P = t_spawn_reg(H, Ctr, 3),
  104. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
  105. ?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
  106. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
  107. ?assertMatch(ok, t_call(P, die)).
  108. t_shared_counter([H|_] = Ns) ->
  109. Ctr = ?T_COUNTER,
  110. P = t_spawn_reg_shared(H, Ctr, 3),
  111. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 3)),
  112. ?assertMatch(5, t_call(P, {apply, gproc, update_shared_counter, [Ctr, 2]})),
  113. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  114. ?assertMatch(ok, t_call(P, die)),
  115. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  116. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)), % twice
  117. P1 = t_spawn(H),
  118. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_shared, [Ctr]})),
  119. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, badarg)).
  120. t_aggr_counter([H1,H2|_] = Ns) ->
  121. {c,g,Nm} = Ctr = ?T_COUNTER,
  122. Aggr = {a,g,Nm},
  123. Pc1 = t_spawn_reg(H1, Ctr, 3),
  124. Pa = t_spawn_reg(H2, Aggr),
  125. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 3)),
  126. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  127. Pc2 = t_spawn_reg(H2, Ctr, 3),
  128. ?assertMatch(ok, t_read_everywhere(Ctr, Pc2, Ns, 3)),
  129. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 6)),
  130. ?assertMatch(5, t_call(Pc1, {apply, gproc, update_counter, [Ctr, 2]})),
  131. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 5)),
  132. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 8)),
  133. ?assertMatch(ok, t_call(Pc1, die)),
  134. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  135. ?assertMatch(ok, t_call(Pc2, die)),
  136. ?assertMatch(ok, t_call(Pa, die)).
  137. t_update_counters([H1,H2|_] = Ns) ->
  138. {c,g,N1} = C1 = ?T_COUNTER,
  139. A1 = {a,g,N1},
  140. C2 = ?T_COUNTER,
  141. P1 = t_spawn_reg(H1, C1, 2),
  142. P12 = t_spawn_reg(H2, C1, 2),
  143. P2 = t_spawn_reg(H2, C2, 1),
  144. Pa1 = t_spawn_reg(H2, A1),
  145. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 2)),
  146. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 2)),
  147. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 1)),
  148. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4)),
  149. ?debugFmt("code:which(gproc_dist) = ~p~n", [code:which(gproc_dist)]),
  150. ?assertMatch([{C1,P1, 3},
  151. {C1,P12,4},
  152. {C2,P2, 0}], t_call(P1, {apply, gproc, update_counters,
  153. [g, [{C1,P1,1},{C1,P12,2},{C2,P2,{-2,0,0}}]]})),
  154. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 3)),
  155. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 4)),
  156. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 0)),
  157. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7)),
  158. ?assertMatch(ok, t_call(P1, die)),
  159. ?assertMatch(ok, t_call(P12, die)),
  160. ?assertMatch(ok, t_call(P2, die)).
  161. t_mreg([H|_] = Ns) ->
  162. Kvl = ?T_KVL,
  163. Keys = [K || {K,_} <- Kvl],
  164. P = t_spawn_mreg(H, Kvl),
  165. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  166. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  167. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  168. ?assertMatch(ok, t_call(P, die)).
  169. t_await_reg([A,B|_]) ->
  170. Name = ?T_NAME,
  171. P = t_spawn(A),
  172. Ref = erlang:monitor(process, P),
  173. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  174. t_sleep(),
  175. P1 = t_spawn_reg(B, Name),
  176. ?assert(P1 == receive
  177. {P, Ref, Res} ->
  178. element(1, Res);
  179. {'DOWN', Ref, _, _, Reason} ->
  180. erlang:error(Reason);
  181. Other ->
  182. erlang:error({received,Other})
  183. end),
  184. ?assertMatch(ok, t_call(P, die)),
  185. ?assertMatch(ok, t_call(P1, die)).
  186. t_await_self([A|_]) ->
  187. Name = ?T_NAME,
  188. P = t_spawn(A, false), % don't buffer unknowns
  189. Ref = t_call(P, {apply, gproc, nb_wait, [Name]}),
  190. ?assertMatch(ok, t_call(P, {selective, true})),
  191. ?assertMatch(true, t_call(P, {apply, gproc, reg, [Name, some_value]})),
  192. ?assertMatch({registered, {Name, P, some_value}},
  193. t_call(P, {apply_fun, fun() ->
  194. receive
  195. {gproc, Ref, R, Wh} ->
  196. {R, Wh}
  197. after 10000 ->
  198. timeout
  199. end
  200. end})),
  201. ?assertMatch(ok, t_call(P, {selective, false})),
  202. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})).
  203. t_await_reg_exists([A,B|_]) ->
  204. Name = ?T_NAME,
  205. P = t_spawn(A),
  206. Ref = erlang:monitor(process, P),
  207. P1 = t_spawn_reg(B, Name),
  208. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  209. ?assert(P1 == receive
  210. {P, Ref, Res} ->
  211. element(1, Res);
  212. {'DOWN', Ref, _, _, Reason} ->
  213. erlang:error(Reason);
  214. Other ->
  215. erlang:error({received,Other})
  216. end),
  217. ?assertMatch(ok, t_call(P, die)),
  218. ?assertMatch(ok, t_call(P1, die)).
  219. t_give_away([A,B|_] = Ns) ->
  220. Na = ?T_NAME,
  221. Nb = ?T_NAME,
  222. Pa = t_spawn_reg(A, Na),
  223. Pb = t_spawn_reg(B, Nb),
  224. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  225. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  226. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  227. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  228. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  229. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  230. ?assertMatch(ok, t_call(Pa, die)),
  231. ?assertMatch(ok, t_call(Pb, die)).
  232. t_sync(Ns) ->
  233. %% Don't really know how to test this...
  234. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  235. || N <- Ns].
  236. t_monitor([A,B|_]) ->
  237. Na = ?T_NAME,
  238. Pa = t_spawn_reg(A, Na),
  239. Pb = t_spawn(B, _Selective = true),
  240. Ref = t_call(Pb, {apply, gproc, monitor, [Na]}),
  241. ?assert(is_reference(Ref)),
  242. ?assertMatch(ok, t_call(Pa, die)),
  243. ?assertMatch({gproc,unreg,Ref,Na}, got_msg(Pb, gproc)),
  244. Pc = t_spawn_reg(A, Na),
  245. Ref1 = t_call(Pb, {apply, gproc, monitor, [Na]}),
  246. ?assertMatch(true, t_call(Pc, {apply, gproc, unreg, [Na]})),
  247. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pb, gproc)).
  248. t_subscribe([A,B|_] = Ns) ->
  249. Na = ?T_NAME,
  250. Pb = t_spawn(B, _Selective = true),
  251. ?assertEqual(ok, t_call(Pb, {apply, gproc_monitor, subscribe, [Na]})),
  252. ?assertMatch({gproc_monitor, Na, undefined}, got_msg(Pb, gproc_monitor)),
  253. Pa = t_spawn_reg(A, Na),
  254. ?assertMatch({gproc_monitor, Na, Pa}, got_msg(Pb, gproc_monitor)),
  255. Pc = t_spawn(A),
  256. t_call(Pa, {apply, gproc, give_away, [Na, Pc]}),
  257. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pc)),
  258. ?assertEqual({gproc_monitor,Na,{migrated,Pc}}, got_msg(Pb, gproc_monitor)),
  259. ?assertEqual(ok, t_call(Pc, die)),
  260. ?assertEqual({gproc_monitor,Na,undefined}, got_msg(Pb, gproc_monitor)).
  261. got_msg(Pb, Tag) ->
  262. t_call(Pb,
  263. {apply_fun,
  264. fun() ->
  265. receive
  266. M when element(1, M) == Tag ->
  267. M
  268. after 1000 ->
  269. timeout
  270. end
  271. end}).
  272. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  273. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  274. %% the other candidate doesn't respond too quickly.
  275. t_sync_cand_dies([A,B|_]) ->
  276. Leader = rpc:call(A, gproc_dist, get_leader, []),
  277. Other = case Leader of
  278. A -> B;
  279. B -> A
  280. end,
  281. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  282. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  283. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  284. %% The overall timeout for gproc_dist:sync() is 5 seconds. Here, we should
  285. %% still be waiting.
  286. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  287. exit(P, kill),
  288. %% The leader should detect that the other candidate died and respond
  289. %% immediately. Therefore, we should have our answer well within 1 sec.
  290. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  291. t_fail_node([A,B|_] = Ns) ->
  292. Na = ?T_NAME,
  293. Nb = ?T_NAME,
  294. Pa = t_spawn_reg(A, Na),
  295. Pb = t_spawn_reg(B, Nb),
  296. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  297. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  298. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  299. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  300. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  301. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  302. ?assertMatch(ok, t_call(Pa, die)),
  303. ?assertMatch(ok, t_call(Pb, die)).
  304. t_sleep() ->
  305. timer:sleep(500).
  306. t_lookup_everywhere(Key, Nodes, Exp) ->
  307. t_lookup_everywhere(Key, Nodes, Exp, 3).
  308. t_lookup_everywhere(Key, _, Exp, 0) ->
  309. {lookup_failed, Key, Exp};
  310. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  311. Expected = [{N, Exp} || N <- Nodes],
  312. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  313. if Expected =/= Found ->
  314. ?debugFmt("lookup ~p failed~n"
  315. "(Expected: ~p;~n"
  316. " Found : ~p), retrying...~n",
  317. [Key, Expected, Found]),
  318. t_sleep(),
  319. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  320. true ->
  321. ok
  322. end.
  323. t_read_everywhere(Key, Pid, Nodes, Exp) ->
  324. t_read_everywhere(Key, Pid, Nodes, Exp, 3).
  325. t_read_everywhere(Key, _, _, Exp, 0) ->
  326. {read_failed, Key, Exp};
  327. t_read_everywhere(Key, Pid, Nodes, Exp, I) ->
  328. Expected = [{N, Exp} || N <- Nodes],
  329. Found = [{N, read_result(rpc:call(N, gproc, get_value, [Key, Pid]))}
  330. || N <- Nodes],
  331. if Expected =/= Found ->
  332. ?debugFmt("read ~p failed~n"
  333. "(Expected: ~p;~n"
  334. " Found : ~p), retrying...~n",
  335. [{Key, Pid}, Expected, Found]),
  336. t_sleep(),
  337. t_read_everywhere(Key, Pid, Nodes, Exp, I-1);
  338. true ->
  339. ok
  340. end.
  341. read_result({badrpc, {'EXIT', {badarg, _}}}) -> badarg;
  342. read_result(R) -> R.
  343. t_spawn(Node) ->
  344. t_spawn(Node, false).
  345. t_spawn(Node, Selective) when is_boolean(Selective) ->
  346. Me = self(),
  347. P = spawn(Node, fun() ->
  348. Me ! {self(), ok},
  349. t_loop(Selective)
  350. end),
  351. receive
  352. {P, ok} -> P
  353. end.
  354. t_spawn_reg(Node, Name) ->
  355. t_spawn_reg(Node, Name, default_value(Name)).
  356. t_spawn_reg(Node, Name, Value) ->
  357. Me = self(),
  358. spawn(Node, fun() ->
  359. ?assertMatch(true, gproc:reg(Name, Value)),
  360. Me ! {self(), ok},
  361. t_loop()
  362. end),
  363. receive
  364. {P, ok} -> P
  365. end.
  366. t_spawn_reg_shared(Node, Name, Value) ->
  367. Me = self(),
  368. spawn(Node, fun() ->
  369. ?assertMatch(true, gproc:reg_shared(Name, Value)),
  370. Me ! {self(), ok},
  371. t_loop()
  372. end),
  373. receive
  374. {P, ok} -> P
  375. end.
  376. default_value({c,_,_}) -> 0;
  377. default_value(_) -> undefined.
  378. t_spawn_mreg(Node, KVL) ->
  379. Me = self(),
  380. spawn(Node, fun() ->
  381. ?assertMatch(true, gproc:mreg(n, g, KVL)),
  382. Me ! {self(), ok},
  383. t_loop()
  384. end),
  385. receive
  386. {P, ok} -> P
  387. end.
  388. t_call(P, Req) ->
  389. Ref = erlang:monitor(process, P),
  390. P ! {self(), Ref, Req},
  391. receive
  392. {P, Ref, Res} ->
  393. erlang:demonitor(Ref),
  394. Res;
  395. {'DOWN', Ref, _, _, Error} ->
  396. erlang:error({'DOWN', P, Error})
  397. end.
  398. t_loop() ->
  399. t_loop(false).
  400. t_loop(Selective) when is_boolean(Selective) ->
  401. receive
  402. {From, Ref, die} ->
  403. From ! {self(), Ref, ok};
  404. {From, Ref, {selective, Bool}} when is_boolean(Bool) ->
  405. From ! {self(), Ref, ok},
  406. t_loop(Bool);
  407. {From, Ref, {apply, M, F, A}} ->
  408. From ! {self(), Ref, apply(M, F, A)},
  409. t_loop(Selective);
  410. {From, Ref, {apply_fun, F}} ->
  411. From ! {self(), Ref, F()},
  412. t_loop(Selective);
  413. Other when not Selective ->
  414. ?debugFmt("got unknown msg: ~p~n", [Other]),
  415. exit({unknown_msg, Other})
  416. end.
  417. start_slaves(Ns) ->
  418. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  419. _ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
  420. Nodes.
  421. start_slave(Name) ->
  422. case node() of
  423. nonode@nohost ->
  424. os:cmd("epmd -daemon"),
  425. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  426. _ ->
  427. ok
  428. end,
  429. {Pa, Pz} = paths(),
  430. Paths = "-pa ./ -pz ../ebin" ++
  431. lists:flatten([[" -pa " ++ Path || Path <- Pa],
  432. [" -pz " ++ Path || Path <- Pz]]),
  433. {ok, Node} = slave:start(host(), Name, Paths),
  434. %% io:fwrite(user, "Slave node: ~p~n", [Node]),
  435. Node.
  436. paths() ->
  437. Path = code:get_path(),
  438. {ok, [[Root]]} = init:get_argument(root),
  439. {Pas, Rest} = lists:splitwith(fun(P) ->
  440. not lists:prefix(Root, P)
  441. end, Path),
  442. {_, Pzs} = lists:splitwith(fun(P) ->
  443. lists:prefix(Root, P)
  444. end, Rest),
  445. {Pas, Pzs}.
  446. host() ->
  447. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  448. list_to_atom(Host).
  449. -endif.