gproc_dist_tests.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. %% -*- erlang-indent-level: 4; indent-tabs-mode: nil -*-
  2. %% ``The contents of this file are subject to the Erlang Public License,
  3. %% Version 1.1, (the "License"); you may not use this file except in
  4. %% compliance with the License. You should have received a copy of the
  5. %% Erlang Public License along with this software. If not, it can be
  6. %% retrieved via the world wide web at http://www.erlang.org/.
  7. %%
  8. %% Software distributed under the License is distributed on an "AS IS"
  9. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  10. %% the License for the specific language governing rights and limitations
  11. %% under the License.
  12. %%
  13. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  14. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  15. %% AB. All Rights Reserved.''
  16. %%
  17. %% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
  18. %%
  19. -module(gproc_dist_tests).
  20. -ifdef(TEST).
  21. -include_lib("eunit/include/eunit.hrl").
  22. -export([t_spawn/1, t_spawn_reg/2]).
  23. dist_test_() ->
  24. {timeout, 120,
  25. [{setup,
  26. fun() ->
  27. case run_dist_tests() of
  28. true ->
  29. Ns = start_slaves([dist_test_n1, dist_test_n2]),
  30. ?assertMatch({[ok,ok],[]},
  31. rpc:multicall(Ns, application, set_env,
  32. [gproc, gproc_dist, Ns])),
  33. ?assertMatch({[ok,ok],[]},
  34. rpc:multicall(
  35. Ns, application, start, [gproc])),
  36. Ns;
  37. false ->
  38. skip
  39. end
  40. end,
  41. fun(_Ns) ->
  42. ok
  43. end,
  44. fun(skip) -> [];
  45. (Ns) when is_list(Ns) ->
  46. {inorder,
  47. [
  48. {inorder, [
  49. fun() ->
  50. ?debugVal(t_simple_reg(Ns))
  51. end,
  52. fun() ->
  53. ?debugVal(t_simple_reg_or_locate(Ns))
  54. end,
  55. fun() ->
  56. ?debugVal(t_simple_counter(Ns))
  57. end,
  58. fun() ->
  59. ?debugVal(t_aggr_counter(Ns))
  60. end,
  61. fun() ->
  62. ?debugVal(t_update_counters(Ns))
  63. end,
  64. fun() ->
  65. ?debugVal(t_shared_counter(Ns))
  66. end,
  67. fun() ->
  68. ?debugVal(t_mreg(Ns))
  69. end,
  70. fun() ->
  71. ?debugVal(t_await_reg(Ns))
  72. end,
  73. fun() ->
  74. ?debugVal(t_await_self(Ns))
  75. end,
  76. fun() ->
  77. ?debugVal(t_await_reg_exists(Ns))
  78. end,
  79. fun() ->
  80. ?debugVal(t_give_away(Ns))
  81. end,
  82. fun() ->
  83. ?debugVal(t_sync(Ns))
  84. end,
  85. fun() ->
  86. ?debugVal(t_monitor(Ns))
  87. end,
  88. fun() ->
  89. ?debugVal(t_subscribe(Ns))
  90. end
  91. ]
  92. },
  93. fun() ->
  94. ?debugVal(t_sync_cand_dies(Ns))
  95. end,
  96. {timeout, 90, [fun() ->
  97. ?debugVal(t_fail_node(Ns))
  98. end]}
  99. ]}
  100. end
  101. }]}.
  102. run_dist_tests() ->
  103. case os:getenv("GPROC_DIST") of
  104. "true" -> true;
  105. "false" -> false;
  106. false ->
  107. case code:ensure_loaded(gen_leader) of
  108. {error, nofile} ->
  109. false;
  110. _ ->
  111. true
  112. end
  113. end.
  114. -define(T_NAME, {n, g, {?MODULE, ?LINE, erlang:now()}}).
  115. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  116. -define(T_COUNTER, {c, g, {?MODULE, ?LINE}}).
  117. t_simple_reg([H|_] = Ns) ->
  118. Name = ?T_NAME,
  119. P = t_spawn_reg(H, Name),
  120. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  121. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  122. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  123. ?assertMatch(ok, t_call(P, die)).
  124. t_simple_reg_or_locate([A,B|_] = _Ns) ->
  125. Name = ?T_NAME,
  126. P1 = t_spawn(A),
  127. Ref = erlang:monitor(process, P1),
  128. ?assertMatch({P1, the_value},
  129. t_call(P1, {apply, gproc, reg_or_locate, [Name, the_value]})),
  130. P2 = t_spawn(B),
  131. Ref2 = erlang:monitor(process, P2),
  132. ?assertMatch({P1, the_value},
  133. t_call(P2, {apply, gproc, reg_or_locate, [Name, other_value]})),
  134. ?assertMatch(ok, t_call(P1, die)),
  135. ?assertMatch(ok, t_call(P2, die)),
  136. flush_down(Ref),
  137. flush_down(Ref2).
  138. flush_down(Ref) ->
  139. receive
  140. {'DOWN', Ref, _, _, _} ->
  141. ok
  142. after 1000 ->
  143. erlang:error({timeout, [flush_down, Ref]})
  144. end.
  145. t_simple_counter([H|_] = Ns) ->
  146. Ctr = ?T_COUNTER,
  147. P = t_spawn_reg(H, Ctr, 3),
  148. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
  149. ?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
  150. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
  151. ?assertMatch(ok, t_call(P, die)).
  152. t_shared_counter([H|_] = Ns) ->
  153. Ctr = ?T_COUNTER,
  154. P = t_spawn_reg_shared(H, Ctr, 3),
  155. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 3)),
  156. ?assertMatch(5, t_call(P, {apply, gproc, update_shared_counter, [Ctr, 2]})),
  157. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  158. ?assertMatch(ok, t_call(P, die)),
  159. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  160. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)), % twice
  161. P1 = t_spawn(H),
  162. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_shared, [Ctr]})),
  163. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, badarg)).
  164. t_aggr_counter([H1,H2|_] = Ns) ->
  165. {c,g,Nm} = Ctr = ?T_COUNTER,
  166. Aggr = {a,g,Nm},
  167. Pc1 = t_spawn_reg(H1, Ctr, 3),
  168. Pa = t_spawn_reg(H2, Aggr),
  169. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 3)),
  170. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  171. Pc2 = t_spawn_reg(H2, Ctr, 3),
  172. ?assertMatch(ok, t_read_everywhere(Ctr, Pc2, Ns, 3)),
  173. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 6)),
  174. ?assertMatch(5, t_call(Pc1, {apply, gproc, update_counter, [Ctr, 2]})),
  175. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 5)),
  176. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 8)),
  177. ?assertMatch(ok, t_call(Pc1, die)),
  178. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  179. ?assertMatch(ok, t_call(Pc2, die)),
  180. ?assertMatch(ok, t_call(Pa, die)).
  181. t_update_counters([H1,H2|_] = Ns) ->
  182. {c,g,N1} = C1 = ?T_COUNTER,
  183. A1 = {a,g,N1},
  184. C2 = ?T_COUNTER,
  185. P1 = t_spawn_reg(H1, C1, 2),
  186. P12 = t_spawn_reg(H2, C1, 2),
  187. P2 = t_spawn_reg(H2, C2, 1),
  188. Pa1 = t_spawn_reg(H2, A1),
  189. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 2)),
  190. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 2)),
  191. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 1)),
  192. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4)),
  193. ?debugFmt("code:which(gproc_dist) = ~p~n", [code:which(gproc_dist)]),
  194. ?assertMatch([{C1,P1, 3},
  195. {C1,P12,4},
  196. {C2,P2, 0}], t_call(P1, {apply, gproc, update_counters,
  197. [g, [{C1,P1,1},{C1,P12,2},{C2,P2,{-2,0,0}}]]})),
  198. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 3)),
  199. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 4)),
  200. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 0)),
  201. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7)),
  202. ?assertMatch(ok, t_call(P1, die)),
  203. ?assertMatch(ok, t_call(P12, die)),
  204. ?assertMatch(ok, t_call(P2, die)).
  205. t_mreg([H|_] = Ns) ->
  206. Kvl = ?T_KVL,
  207. Keys = [K || {K,_} <- Kvl],
  208. P = t_spawn_mreg(H, Kvl),
  209. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  210. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  211. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  212. ?assertMatch(ok, t_call(P, die)).
  213. t_await_reg([A,B|_]) ->
  214. Name = ?T_NAME,
  215. P = t_spawn(A),
  216. Ref = erlang:monitor(process, P),
  217. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  218. t_sleep(),
  219. P1 = t_spawn_reg(B, Name),
  220. ?assert(P1 == receive
  221. {P, Ref, Res} ->
  222. element(1, Res);
  223. {'DOWN', Ref, _, _, Reason} ->
  224. erlang:error(Reason);
  225. Other ->
  226. erlang:error({received,Other})
  227. end),
  228. ?assertMatch(ok, t_call(P, die)),
  229. flush_down(Ref),
  230. ?assertMatch(ok, t_call(P1, die)).
  231. t_await_self([A|_]) ->
  232. Name = ?T_NAME,
  233. P = t_spawn(A, false), % don't buffer unknowns
  234. Ref = t_call(P, {apply, gproc, nb_wait, [Name]}),
  235. ?assertMatch(ok, t_call(P, {selective, true})),
  236. ?assertMatch(true, t_call(P, {apply, gproc, reg, [Name, some_value]})),
  237. ?assertMatch({registered, {Name, P, some_value}},
  238. t_call(P, {apply_fun, fun() ->
  239. receive
  240. {gproc, Ref, R, Wh} ->
  241. {R, Wh}
  242. after 10000 ->
  243. timeout
  244. end
  245. end})),
  246. ?assertMatch(ok, t_call(P, {selective, false})),
  247. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})).
  248. t_await_reg_exists([A,B|_]) ->
  249. Name = ?T_NAME,
  250. P = t_spawn(A),
  251. Ref = erlang:monitor(process, P),
  252. P1 = t_spawn_reg(B, Name),
  253. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  254. ?assert(P1 == receive
  255. {P, Ref, Res} ->
  256. element(1, Res);
  257. {'DOWN', Ref, _, _, Reason} ->
  258. erlang:error(Reason);
  259. Other ->
  260. erlang:error({received,Other})
  261. end),
  262. ?assertMatch(ok, t_call(P, die)),
  263. ?assertMatch(ok, t_call(P1, die)).
  264. t_give_away([A,B|_] = Ns) ->
  265. Na = ?T_NAME,
  266. Nb = ?T_NAME,
  267. Pa = t_spawn_reg(A, Na),
  268. Pb = t_spawn_reg(B, Nb),
  269. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  270. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  271. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  272. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  273. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  274. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  275. ?assertMatch(ok, t_call(Pa, die)),
  276. ?assertMatch(ok, t_call(Pb, die)).
  277. t_sync(Ns) ->
  278. %% Don't really know how to test this...
  279. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  280. || N <- Ns].
  281. t_monitor([A,B|_]) ->
  282. Na = ?T_NAME,
  283. Pa = t_spawn_reg(A, Na),
  284. Pb = t_spawn(B, _Selective = true),
  285. Ref = t_call(Pb, {apply, gproc, monitor, [Na]}),
  286. ?assert(is_reference(Ref)),
  287. ?assertMatch(ok, t_call(Pa, die)),
  288. ?assertMatch({gproc,unreg,Ref,Na}, got_msg(Pb, gproc)),
  289. Pc = t_spawn_reg(A, Na),
  290. Ref1 = t_call(Pb, {apply, gproc, monitor, [Na]}),
  291. ?assertMatch(true, t_call(Pc, {apply, gproc, unreg, [Na]})),
  292. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pb, gproc)).
  293. t_subscribe([A,B|_] = Ns) ->
  294. Na = ?T_NAME,
  295. Pb = t_spawn(B, _Selective = true),
  296. ?assertEqual(ok, t_call(Pb, {apply, gproc_monitor, subscribe, [Na]})),
  297. ?assertMatch({gproc_monitor, Na, undefined}, got_msg(Pb, gproc_monitor)),
  298. Pa = t_spawn_reg(A, Na),
  299. ?assertMatch({gproc_monitor, Na, Pa}, got_msg(Pb, gproc_monitor)),
  300. Pc = t_spawn(A),
  301. t_call(Pa, {apply, gproc, give_away, [Na, Pc]}),
  302. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pc)),
  303. ?assertEqual({gproc_monitor,Na,{migrated,Pc}}, got_msg(Pb, gproc_monitor)),
  304. ?assertEqual(ok, t_call(Pc, die)),
  305. ?assertEqual({gproc_monitor,Na,undefined}, got_msg(Pb, gproc_monitor)),
  306. ok.
  307. got_msg(Pb, Tag) ->
  308. t_call(Pb,
  309. {apply_fun,
  310. fun() ->
  311. receive
  312. M when element(1, M) == Tag ->
  313. M
  314. after 1000 ->
  315. erlang:error({timeout, got_msg, [Pb, Tag]})
  316. end
  317. end}).
  318. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  319. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  320. %% the other candidate doesn't respond too quickly.
  321. t_sync_cand_dies([A,B|_]) ->
  322. Leader = rpc:call(A, gproc_dist, get_leader, []),
  323. Other = case Leader of
  324. A -> B;
  325. B -> A
  326. end,
  327. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  328. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  329. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  330. %% The overall timeout for gproc_dist:sync() is 5 seconds. Here, we should
  331. %% still be waiting.
  332. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  333. exit(P, kill),
  334. %% The leader should detect that the other candidate died and respond
  335. %% immediately. Therefore, we should have our answer well within 1 sec.
  336. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  337. t_fail_node([A,B|_] = Ns) ->
  338. Na = ?T_NAME,
  339. Nb = ?T_NAME,
  340. Pa = t_spawn_reg(A, Na),
  341. Pb = t_spawn_reg(B, Nb),
  342. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  343. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  344. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  345. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  346. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  347. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  348. ?assertMatch(ok, t_call(Pa, die)),
  349. ?assertMatch(ok, t_call(Pb, die)).
  350. t_sleep() ->
  351. timer:sleep(500).
  352. t_lookup_everywhere(Key, Nodes, Exp) ->
  353. t_lookup_everywhere(Key, Nodes, Exp, 3).
  354. t_lookup_everywhere(Key, _, Exp, 0) ->
  355. {lookup_failed, Key, Exp};
  356. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  357. Expected = [{N, Exp} || N <- Nodes],
  358. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  359. if Expected =/= Found ->
  360. ?debugFmt("lookup ~p failed~n"
  361. "(Expected: ~p;~n"
  362. " Found : ~p), retrying...~n",
  363. [Key, Expected, Found]),
  364. t_sleep(),
  365. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  366. true ->
  367. ok
  368. end.
  369. t_read_everywhere(Key, Pid, Nodes, Exp) ->
  370. t_read_everywhere(Key, Pid, Nodes, Exp, 3).
  371. t_read_everywhere(Key, _, _, Exp, 0) ->
  372. {read_failed, Key, Exp};
  373. t_read_everywhere(Key, Pid, Nodes, Exp, I) ->
  374. Expected = [{N, Exp} || N <- Nodes],
  375. Found = [{N, read_result(rpc:call(N, gproc, get_value, [Key, Pid]))}
  376. || N <- Nodes],
  377. if Expected =/= Found ->
  378. ?debugFmt("read ~p failed~n"
  379. "(Expected: ~p;~n"
  380. " Found : ~p), retrying...~n",
  381. [{Key, Pid}, Expected, Found]),
  382. t_sleep(),
  383. t_read_everywhere(Key, Pid, Nodes, Exp, I-1);
  384. true ->
  385. ok
  386. end.
  387. read_result({badrpc, {'EXIT', {badarg, _}}}) -> badarg;
  388. read_result(R) -> R.
  389. t_spawn(Node) ->
  390. t_spawn(Node, false).
  391. t_spawn(Node, Selective) when is_boolean(Selective) ->
  392. Me = self(),
  393. P = spawn(Node, fun() ->
  394. Me ! {self(), ok},
  395. t_loop(Selective)
  396. end),
  397. receive
  398. {P, ok} -> P
  399. after 1000 ->
  400. erlang:error({timeout, t_spawn, [Node, Selective]})
  401. end.
  402. t_spawn_reg(Node, Name) ->
  403. t_spawn_reg(Node, Name, default_value(Name)).
  404. t_spawn_reg(Node, Name, Value) ->
  405. Me = self(),
  406. P = spawn(Node, fun() ->
  407. ?assertMatch(true, gproc:reg(Name, Value)),
  408. Me ! {self(), ok},
  409. t_loop()
  410. end),
  411. receive
  412. {P, ok} -> P
  413. after 1000 ->
  414. erlang:error({timeout, t_spawn_reg, [Node, Name, Value]})
  415. end.
  416. t_spawn_reg_shared(Node, Name, Value) ->
  417. Me = self(),
  418. P = spawn(Node, fun() ->
  419. ?assertMatch(true, gproc:reg_shared(Name, Value)),
  420. Me ! {self(), ok},
  421. t_loop()
  422. end),
  423. receive
  424. {P, ok} -> P
  425. after 1000 ->
  426. erlang:error({timeout, t_spawn_reg_shared, [Node,Name,Value]})
  427. end.
  428. default_value({c,_,_}) -> 0;
  429. default_value(_) -> undefined.
  430. t_spawn_mreg(Node, KVL) ->
  431. Me = self(),
  432. P = spawn(Node, fun() ->
  433. ?assertMatch(true, gproc:mreg(n, g, KVL)),
  434. Me ! {self(), ok},
  435. t_loop()
  436. end),
  437. receive
  438. {P, ok} -> P
  439. end.
  440. t_call(P, Req) ->
  441. Ref = erlang:monitor(process, P),
  442. P ! {self(), Ref, Req},
  443. receive
  444. {P, Ref, Res} ->
  445. erlang:demonitor(Ref, [flush]),
  446. Res;
  447. {'DOWN', Ref, _, _, Error} ->
  448. erlang:error({'DOWN', P, Error})
  449. after 1000 ->
  450. erlang:error({timeout,t_call,[P,Req]})
  451. end.
  452. t_loop() ->
  453. t_loop(false).
  454. t_loop(Selective) when is_boolean(Selective) ->
  455. receive
  456. {From, Ref, die} ->
  457. From ! {self(), Ref, ok};
  458. {From, Ref, {selective, Bool}} when is_boolean(Bool) ->
  459. From ! {self(), Ref, ok},
  460. t_loop(Bool);
  461. {From, Ref, {apply, M, F, A}} ->
  462. From ! {self(), Ref, apply(M, F, A)},
  463. t_loop(Selective);
  464. {From, Ref, {apply_fun, F}} ->
  465. From ! {self(), Ref, F()},
  466. t_loop(Selective);
  467. Other when not Selective ->
  468. ?debugFmt("got unknown msg: ~p~n", [Other]),
  469. exit({unknown_msg, Other})
  470. end.
  471. start_slaves(Ns) ->
  472. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  473. _ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
  474. Nodes.
  475. start_slave(Name) ->
  476. case node() of
  477. nonode@nohost ->
  478. os:cmd("epmd -daemon"),
  479. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  480. _ ->
  481. ok
  482. end,
  483. {Pa, Pz} = paths(),
  484. Paths = "-pa ./ -pz ../ebin" ++
  485. lists:flatten([[" -pa " ++ Path || Path <- Pa],
  486. [" -pz " ++ Path || Path <- Pz]]),
  487. {ok, Node} = slave:start(host(), Name, Paths),
  488. Node.
  489. paths() ->
  490. Path = code:get_path(),
  491. {ok, [[Root]]} = init:get_argument(root),
  492. {Pas, Rest} = lists:splitwith(fun(P) ->
  493. not lists:prefix(Root, P)
  494. end, Path),
  495. {_, Pzs} = lists:splitwith(fun(P) ->
  496. lists:prefix(Root, P)
  497. end, Rest),
  498. {Pas, Pzs}.
  499. host() ->
  500. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  501. list_to_atom(Host).
  502. -endif.