gproc_dist_tests.erl 18 KB

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