gproc_dist_tests.erl 18 KB

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