gproc_dist_tests.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% --------------------------------------------------
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. %% --------------------------------------------------
  17. %%
  18. %% @author Ulf Wiger <ulf@wiger.net>
  19. %%
  20. -module(gproc_dist_tests).
  21. -ifdef(TEST).
  22. -include_lib("eunit/include/eunit.hrl").
  23. -export([t_spawn/1, t_spawn_reg/2]).
  24. -define(f(E), fun() -> ?debugVal(E) end).
  25. dist_test_() ->
  26. {timeout, 120,
  27. [
  28. %% {setup,
  29. %% fun dist_setup/0,
  30. %% fun dist_cleanup/1,
  31. %% fun(skip) -> [];
  32. %% (Ns) when is_list(Ns) ->
  33. %% {inorder, basic_tests(Ns)}
  34. %% end
  35. %% },
  36. {foreach,
  37. fun dist_setup/0,
  38. fun dist_cleanup/1,
  39. [
  40. fun(Ns) ->
  41. [{inorder, basic_tests(Ns)}]
  42. end,
  43. fun(Ns) ->
  44. tests(Ns, [?f(t_sync_cand_dies(Ns))])
  45. end,
  46. fun(Ns) ->
  47. tests(Ns, [?f(t_fail_node(Ns))])
  48. end,
  49. fun(Ns) ->
  50. tests(Ns, [{timeout, 15, ?f(t_master_dies(Ns))}])
  51. end
  52. ]}
  53. ]}.
  54. tests(skip, _) ->
  55. [];
  56. tests(_, L) ->
  57. L.
  58. basic_tests(skip) ->
  59. [];
  60. basic_tests(Ns) ->
  61. [
  62. ?f(t_simple_reg(Ns)),
  63. ?f(t_simple_reg_other(Ns)),
  64. ?f(t_simple_ensure(Ns)),
  65. ?f(t_simple_ensure_other(Ns)),
  66. ?f(t_simple_reg_or_locate(Ns)),
  67. ?f(t_simple_counter(Ns)),
  68. ?f(t_aggr_counter(Ns)),
  69. ?f(t_awaited_aggr_counter(Ns)),
  70. ?f(t_simple_resource_count(Ns)),
  71. ?f(t_awaited_resource_count(Ns)),
  72. ?f(t_resource_count_on_zero(Ns)),
  73. ?f(t_update_counters(Ns)),
  74. ?f(t_shared_counter(Ns)),
  75. ?f(t_prop(Ns)),
  76. ?f(t_mreg(Ns)),
  77. ?f(t_await_reg(Ns)),
  78. ?f(t_await_self(Ns)),
  79. ?f(t_await_reg_exists(Ns)),
  80. ?f(t_give_away(Ns)),
  81. ?f(t_sync(Ns)),
  82. ?f(t_monitor(Ns)),
  83. ?f(t_standby_monitor(Ns)),
  84. ?f(t_standby_monitor_unreg(Ns)),
  85. ?f(t_follow_monitor(Ns)),
  86. ?f(t_monitor_demonitor(Ns)),
  87. ?f(t_subscribe(Ns))
  88. ].
  89. dist_setup() ->
  90. case run_dist_tests() of
  91. true ->
  92. Ns = start_slaves([dist_test_n1, dist_test_n2, dist_test_n3]),
  93. ?assertMatch({[ok,ok,ok],[]},
  94. rpc:multicall(Ns, application, set_env,
  95. [gproc, gproc_dist, Ns])),
  96. ?assertMatch({[ok,ok,ok],[]},
  97. rpc:multicall(
  98. Ns, application, start, [gproc])),
  99. Ns;
  100. false ->
  101. skip
  102. end.
  103. dist_cleanup(skip) ->
  104. ok;
  105. dist_cleanup(Ns) ->
  106. [slave:stop(N) || N <- Ns],
  107. ok.
  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, os:timestamp()}}).
  121. -define(T_KVL, [{foo, "foo"}, {bar, "bar"}]).
  122. -define(T_COUNTER, {c, g, {?MODULE, ?LINE}}).
  123. -define(T_RESOURCE, {r, g, {?MODULE, ?LINE}}).
  124. -define(T_PROP, {p, g, ?MODULE}).
  125. t_simple_reg([H|_] = Ns) ->
  126. Name = ?T_NAME,
  127. P = t_spawn_reg(H, Name),
  128. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  129. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  130. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  131. ?assertMatch(ok, t_call(P, die)).
  132. t_simple_reg_other([A, B|_] = Ns) ->
  133. Name = ?T_NAME,
  134. P1 = t_spawn(A),
  135. P2 = t_spawn(B),
  136. ?assertMatch(true, t_call(P1, {apply, gproc, reg_other, [Name, P2]})),
  137. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P2)),
  138. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_other, [Name, P2]})),
  139. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  140. ?assertMatch(ok, t_call(P1, die)),
  141. ?assertMatch(ok, t_call(P2, die)).
  142. t_simple_ensure([H|_] = Ns) ->
  143. Name = ?T_NAME,
  144. P = t_spawn_reg(H, Name),
  145. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P)),
  146. ?assertMatch(
  147. updated, t_call(
  148. P, {apply, gproc, ensure_reg, [Name, new_val, [{a,1}]]})),
  149. ?assertMatch(
  150. [{a,1}], t_call(
  151. P, {apply, gproc, get_attributes, [Name]})),
  152. ?assertMatch(ok, t_read_everywhere(Name, P, Ns, new_val)),
  153. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})),
  154. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  155. ?assertMatch(ok, t_call(P, die)).
  156. t_simple_ensure_other([A, B|_] = Ns) ->
  157. Name = ?T_NAME,
  158. P1 = t_spawn(A),
  159. P2 = t_spawn(B),
  160. ?assertMatch(true, t_call(P1, {apply, gproc, reg_other, [Name, P2]})),
  161. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P2)),
  162. ?assertMatch(
  163. updated, t_call(
  164. P1, {apply, gproc, ensure_reg_other, [Name, P2, new_val]})),
  165. ?assertMatch(ok, t_read_everywhere(Name, P2, Ns, new_val)),
  166. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_other, [Name, P2]})),
  167. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, undefined)),
  168. ?assertMatch(ok, t_call(P1, die)),
  169. ?assertMatch(ok, t_call(P2, die)).
  170. t_simple_reg_or_locate([A,B|_] = _Ns) ->
  171. Name = ?T_NAME,
  172. P1 = t_spawn(A),
  173. Ref = erlang:monitor(process, P1),
  174. ?assertMatch({P1, the_value},
  175. t_call(P1, {apply, gproc, reg_or_locate, [Name, the_value]})),
  176. P2 = t_spawn(B),
  177. Ref2 = erlang:monitor(process, P2),
  178. ?assertMatch({P1, the_value},
  179. t_call(P2, {apply, gproc, reg_or_locate, [Name, other_value]})),
  180. ?assertMatch(ok, t_call(P1, die)),
  181. ?assertMatch(ok, t_call(P2, die)),
  182. flush_down(Ref),
  183. flush_down(Ref2).
  184. flush_down(Ref) ->
  185. receive
  186. {'DOWN', Ref, _, _, _} ->
  187. ok
  188. after 1000 ->
  189. erlang:error({timeout, [flush_down, Ref]})
  190. end.
  191. t_simple_counter([H|_] = Ns) ->
  192. Ctr = ?T_COUNTER,
  193. P = t_spawn_reg(H, Ctr, 3),
  194. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
  195. ?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
  196. ?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
  197. ?assertMatch(ok, t_call(P, die)).
  198. t_shared_counter([H|_] = Ns) ->
  199. Ctr = ?T_COUNTER,
  200. P = t_spawn_reg_shared(H, Ctr, 3),
  201. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 3)),
  202. ?assertMatch(5, t_call(P, {apply, gproc, update_shared_counter, [Ctr, 2]})),
  203. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  204. ?assertMatch(ok, t_call(P, die)),
  205. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)),
  206. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, 5)), % twice
  207. P1 = t_spawn(H),
  208. ?assertMatch(true, t_call(P1, {apply, gproc, unreg_shared, [Ctr]})),
  209. ?assertMatch(ok, t_read_everywhere(Ctr, shared, Ns, badarg)).
  210. t_aggr_counter([H1,H2|_] = Ns) ->
  211. {c,g,Nm} = Ctr = ?T_COUNTER,
  212. Aggr = {a,g,Nm},
  213. Pc1 = t_spawn_reg(H1, Ctr, 3),
  214. Pa = t_spawn_reg(H2, Aggr),
  215. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 3)),
  216. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  217. Pc2 = t_spawn_reg(H2, Ctr, 3),
  218. ?assertMatch(ok, t_read_everywhere(Ctr, Pc2, Ns, 3)),
  219. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 6)),
  220. ?assertMatch(5, t_call(Pc1, {apply, gproc, update_counter, [Ctr, 2]})),
  221. ?assertMatch(ok, t_read_everywhere(Ctr, Pc1, Ns, 5)),
  222. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 8)),
  223. ?assertMatch(ok, t_call(Pc1, die)),
  224. ?assertMatch(ok, t_read_everywhere(Aggr, Pa, Ns, 3)),
  225. ?assertMatch(ok, t_call(Pc2, die)),
  226. ?assertMatch(ok, t_call(Pa, die)).
  227. t_awaited_aggr_counter([H1,H2|_] = Ns) ->
  228. {c,g,Nm} = Ctr = ?T_COUNTER,
  229. Aggr = {a,g,Nm},
  230. Pc1 = t_spawn_reg(H1, Ctr, 3),
  231. P = t_spawn(H2),
  232. Ref = erlang:monitor(process, P),
  233. P ! {self(), Ref, {apply, gproc, await, [Aggr]}},
  234. t_sleep(),
  235. P1 = t_spawn_reg(H2, Aggr),
  236. ?assert(P1 == receive
  237. {P, Ref, Res} ->
  238. element(1, Res);
  239. {'DOWN', Ref, _, _, Reason} ->
  240. erlang:error(Reason);
  241. Other ->
  242. erlang:error({received, Other})
  243. end),
  244. ?assertMatch(ok, t_read_everywhere(Aggr, P1, Ns, 3)),
  245. ?assertMatch(ok, t_call(Pc1, die)),
  246. ?assertMatch(ok, t_call(P, die)),
  247. flush_down(Ref),
  248. ?assertMatch(ok, t_call(P1, die)).
  249. t_simple_resource_count([H1,H2|_] = Ns) ->
  250. {r,g,Nm} = R = ?T_RESOURCE,
  251. RC = {rc,g,Nm},
  252. Pr1 = t_spawn_reg(H1, R, 3),
  253. Prc = t_spawn_reg(H2, RC),
  254. ?assertMatch(ok, t_read_everywhere(R, Pr1, Ns, 3)),
  255. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  256. Pr2 = t_spawn_reg(H2, R, 4),
  257. ?assertMatch(ok, t_read_everywhere(R, Pr2, Ns, 4)),
  258. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 2)),
  259. ?assertMatch(ok, t_call(Pr1, die)),
  260. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  261. ?assertMatch(ok, t_call(Pr2, die)),
  262. ?assertMatch(ok, t_call(Prc, die)).
  263. t_awaited_resource_count([H1,H2|_] = Ns) ->
  264. {r,g,Nm} = R = ?T_RESOURCE,
  265. RC = {rc,g,Nm},
  266. Pr1 = t_spawn_reg(H1, R, 3),
  267. P = t_spawn(H2),
  268. Ref = erlang:monitor(process, P),
  269. P ! {self(), Ref, {apply, gproc, await, [RC]}},
  270. t_sleep(),
  271. P1 = t_spawn_reg(H2, RC),
  272. ?assert(P1 == receive
  273. {P, Ref, Res} ->
  274. element(1, Res);
  275. {'DOWN', Ref, _, _, Reason} ->
  276. erlang:error(Reason);
  277. Other ->
  278. erlang:error({received, Other})
  279. end),
  280. ?assertMatch(ok, t_read_everywhere(RC, P1, Ns, 1)),
  281. ?assertMatch(ok, t_call(Pr1, die)),
  282. ?assertMatch(ok, t_call(P, die)),
  283. flush_down(Ref),
  284. ?assertMatch(ok, t_call(P1, die)).
  285. t_resource_count_on_zero([H1,H2|_] = Ns) ->
  286. {r,g,Nm} = R = ?T_RESOURCE,
  287. Prop = ?T_PROP,
  288. RC = {rc,g,Nm},
  289. Pr1 = t_spawn_reg(H1, R, 3),
  290. Pp = t_spawn_reg(H2, Prop),
  291. ?assertMatch(ok, t_call(Pp, {selective, true})),
  292. Prc = t_spawn_reg(H2, RC, undefined, [{on_zero, [{send, Prop}]}]),
  293. ?assertMatch(ok, t_read_everywhere(R, Pr1, Ns, 3)),
  294. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 1)),
  295. ?assertMatch(ok, t_call(Pr1, die)),
  296. ?assertMatch(ok, t_read_everywhere(RC, Prc, Ns, 0)),
  297. ?assertMatch({gproc, resource_on_zero, g, Nm, Prc},
  298. t_call(Pp, {apply_fun, fun() ->
  299. receive
  300. {gproc, _, _, _, _} = M ->
  301. M
  302. after 10000 ->
  303. timeout
  304. end
  305. end})),
  306. ?assertMatch(ok, t_call(Pp, {selective, false})),
  307. ?assertMatch(ok, t_call(Pp, die)),
  308. ?assertMatch(ok, t_call(Prc, die)).
  309. t_update_counters([H1,H2|_] = Ns) ->
  310. {c,g,N1} = C1 = ?T_COUNTER,
  311. A1 = {a,g,N1},
  312. C2 = ?T_COUNTER,
  313. P1 = t_spawn_reg(H1, C1, 2),
  314. P12 = t_spawn_reg(H2, C1, 2),
  315. P2 = t_spawn_reg(H2, C2, 1),
  316. Pa1 = t_spawn_reg(H2, A1),
  317. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 2)),
  318. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 2)),
  319. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 1)),
  320. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4)),
  321. ?assertMatch([{C1,P1, 3},
  322. {C1,P12,4},
  323. {C2,P2, 0}], t_call(P1, {apply, gproc, update_counters,
  324. [g, [{C1,P1,1},{C1,P12,2},{C2,P2,{-2,0,0}}]]})),
  325. ?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 3)),
  326. ?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 4)),
  327. ?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 0)),
  328. ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7)),
  329. ?assertMatch(ok, t_call(P1, die)),
  330. ?assertMatch(ok, t_call(P12, die)),
  331. ?assertMatch(ok, t_call(P2, die)).
  332. t_prop([H1,H2|_] = Ns) ->
  333. {p, g, _} = P = ?T_PROP,
  334. P1 = t_spawn_reg(H1, P, 1),
  335. P2 = t_spawn_reg(H2, P, 2),
  336. ?assertMatch(ok, t_read_everywhere(P, P1, Ns, 1)),
  337. ?assertMatch(ok, t_read_everywhere(P, P2, Ns, 2)),
  338. ?assertMatch(ok, t_call(P1, die)),
  339. ?assertMatch(ok, t_read_everywhere(P, P1, Ns, badarg)),
  340. ?assertMatch(ok, t_call(P2, die)).
  341. t_mreg([H|_] = Ns) ->
  342. Kvl = ?T_KVL,
  343. Keys = [K || {K,_} <- Kvl],
  344. P = t_spawn_mreg(H, Kvl),
  345. [?assertMatch(ok, t_lookup_everywhere({n,g,K}, Ns, P)) || K <- Keys],
  346. ?assertMatch(true, t_call(P, {apply, gproc, munreg, [n, g, Keys]})),
  347. [?assertMatch(ok, t_lookup_everywhere({n,g,K},Ns,undefined)) || K <- Keys],
  348. ?assertMatch(ok, t_call(P, die)).
  349. t_await_reg([A,B|_] = Ns) ->
  350. Name = ?T_NAME,
  351. P = t_spawn(A),
  352. Ref = erlang:monitor(process, P),
  353. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  354. t_sleep(),
  355. P1 = t_spawn_reg(B, Name),
  356. ?assert(P1 == receive
  357. {P, Ref, Res} ->
  358. element(1, Res);
  359. {'DOWN', Ref, _, _, Reason} ->
  360. erlang:error(Reason);
  361. Other ->
  362. erlang:error({received,Other})
  363. end),
  364. ?assertMatch(ok, t_call(P, die)),
  365. flush_down(Ref),
  366. ?assertMatch(ok, t_lookup_everywhere(Name, Ns, P1)),
  367. ?assertMatch(ok, t_call(P1, die)).
  368. t_await_self([A|_]) ->
  369. Name = ?T_NAME,
  370. P = t_spawn(A, false), % don't buffer unknowns
  371. Ref = t_call(P, {apply, gproc, nb_wait, [Name]}),
  372. ?assertMatch(ok, t_call(P, {selective, true})),
  373. ?assertMatch(true, t_call(P, {apply, gproc, reg, [Name, some_value]})),
  374. ?assertMatch({registered, {Name, P, some_value}},
  375. t_call(P, {apply_fun, fun() ->
  376. receive
  377. {gproc, Ref, R, Wh} ->
  378. {R, Wh}
  379. after 10000 ->
  380. timeout
  381. end
  382. end})),
  383. ?assertMatch(ok, t_call(P, {selective, false})),
  384. ?assertMatch(true, t_call(P, {apply, gproc, unreg, [Name]})).
  385. t_await_reg_exists([A,B|_]) ->
  386. Name = ?T_NAME,
  387. P = t_spawn(A),
  388. Ref = erlang:monitor(process, P),
  389. P1 = t_spawn_reg(B, Name),
  390. P ! {self(), Ref, {apply, gproc, await, [Name]}},
  391. ?assert(P1 == receive
  392. {P, Ref, Res} ->
  393. element(1, Res);
  394. {'DOWN', Ref, _, _, Reason} ->
  395. erlang:error(Reason);
  396. Other ->
  397. erlang:error({received,Other})
  398. end),
  399. ?assertMatch(ok, t_call(P, die)),
  400. ?assertMatch(ok, t_call(P1, die)).
  401. t_give_away([A,B|_] = Ns) ->
  402. Na = ?T_NAME,
  403. Nb = ?T_NAME,
  404. Pa = t_spawn_reg(A, Na),
  405. Pb = t_spawn_reg(B, Nb),
  406. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  407. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  408. ?assertMatch(Pb, t_call(Pa, {apply, gproc, give_away, [Na, Nb]})),
  409. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  410. ?assertMatch(Pa, t_call(Pb, {apply, gproc, give_away, [Na, Pa]})),
  411. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  412. ?assertMatch(ok, t_call(Pa, die)),
  413. ?assertMatch(ok, t_call(Pb, die)).
  414. t_sync(Ns) ->
  415. %% Don't really know how to test this...
  416. [?assertMatch(true, rpc:call(N, gproc_dist, sync, []))
  417. || N <- Ns].
  418. t_monitor([A,B|_]) ->
  419. Na = ?T_NAME,
  420. Pa = t_spawn_reg(A, Na),
  421. Pb = t_spawn(B, _Selective = true),
  422. Ref = t_call(Pb, {apply, gproc, monitor, [Na]}),
  423. ?assert(is_reference(Ref)),
  424. ?assertMatch(ok, t_call(Pa, die)),
  425. ?assertMatch({gproc,unreg,Ref,Na}, got_msg(Pb, gproc)),
  426. Pc = t_spawn_reg(A, Na),
  427. Ref1 = t_call(Pb, {apply, gproc, monitor, [Na]}),
  428. ?assertMatch(true, t_call(Pc, {apply, gproc, unreg, [Na]})),
  429. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pb, gproc)).
  430. t_standby_monitor([A,B|_] = Ns) ->
  431. Na = ?T_NAME,
  432. Pa = t_spawn_reg(A, Na),
  433. Pb = t_spawn(B, _Selective = true),
  434. Ref = t_call(Pb, {apply, gproc, monitor, [Na, standby]}),
  435. ?assert(is_reference(Ref)),
  436. ?assertMatch(ok, t_call(Pa, die)),
  437. ?assertMatch({gproc,{failover,Pb},Ref,Na}, got_msg(Pb, gproc)),
  438. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pb)),
  439. Pc = t_spawn(A, true),
  440. Ref1 = t_call(Pc, {apply, gproc, monitor, [Na, standby]}),
  441. ?assertMatch(true, t_call(Pb, {apply, gproc, unreg, [Na]})),
  442. ?assertMatch({gproc,unreg,Ref1,Na}, got_msg(Pc, gproc)),
  443. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)).
  444. t_standby_monitor_unreg([A|_] = Ns) ->
  445. Na = ?T_NAME,
  446. Pa = t_spawn(A, _Selective = true),
  447. Ref = t_call(Pa, {apply, gproc, monitor, [Na, standby]}),
  448. ?assert(is_reference(Ref)),
  449. ?assertMatch({gproc,{failover,Pa},Ref,Na}, got_msg(Pa, gproc)),
  450. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  451. ?assertMatch(ok, t_call(Pa, die)),
  452. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)).
  453. t_follow_monitor([A,B|_]) ->
  454. Na = ?T_NAME,
  455. Pa = t_spawn(A, _Selective = true),
  456. Ref = t_call(Pa, {apply, gproc, monitor, [Na, follow]}),
  457. Msg1 = {gproc,unreg,Ref,Na},
  458. {Msg1, Msg1} = {got_msg(Pa), Msg1},
  459. Pb = t_spawn_reg(B, Na),
  460. Msg2 = {gproc,registered,Ref,Na},
  461. {Msg2, Msg2} = {got_msg(Pa), Msg2},
  462. ok = t_call(Pb, die),
  463. ok = t_call(Pa, die).
  464. t_monitor_demonitor([A,B|_]) ->
  465. Na = ?T_NAME,
  466. Pa = t_spawn(A, Selective = true),
  467. Pa2 = t_spawn(A, Selective),
  468. Pb = t_spawn(B, Selective),
  469. Pb2 = t_spawn(B, Selective),
  470. RefA = t_call(Pa, {apply, gproc, monitor, [Na, follow]}),
  471. RefA2 = t_call(Pa2, {apply, gproc, monitor, [Na, follow]}),
  472. RefB = t_call(Pb, {apply, gproc, monitor, [Na, follow]}),
  473. RefB2 = t_call(Pb2, {apply, gproc, monitor, [Na, follow]}),
  474. Msg1 = {gproc, unreg, RefA, Na},
  475. {Msg1, Msg1} = {got_msg(Pa), Msg1},
  476. Msg2 = {gproc, unreg, RefA2, Na},
  477. {Msg2, Msg2} = {got_msg(Pa2), Msg2},
  478. Msg3 = {gproc, unreg, RefB, Na},
  479. {Msg3, Msg3} = {got_msg(Pb), Msg3},
  480. Msg4 = {gproc, unreg, RefB2, Na},
  481. {Msg4, Msg4} = {got_msg(Pb2), Msg4},
  482. ok = t_call(Pa, {apply, gproc, demonitor, [Na, RefA]}),
  483. ok = t_call(Pb, {apply, gproc, demonitor, [Na, RefB]}),
  484. Pr = t_spawn_reg(B, Na),
  485. Msg5 = {gproc, registered, RefA2, Na},
  486. {Msg5, Msg5} = {got_msg(Pa2), Msg5},
  487. Msg6 = {gproc, registered, RefB2, Na},
  488. {Msg6, Msg6} = {got_msg(Pb2), Msg6},
  489. ok = no_msg(Pa, 500),
  490. ok = no_msg(Pb, 500),
  491. [ ok = t_call(P, die) || P <- [Pa, Pa2, Pb, Pb2, Pr]],
  492. ok.
  493. t_subscribe([A,B|_] = Ns) ->
  494. Na = ?T_NAME,
  495. Pb = t_spawn(B, _Selective = true),
  496. ?assertEqual(ok, t_call(Pb, {apply, gproc_monitor, subscribe, [Na]})),
  497. ?assertMatch({gproc_monitor, Na, undefined}, got_msg(Pb, gproc_monitor)),
  498. Pa = t_spawn_reg(A, Na),
  499. ?assertMatch({gproc_monitor, Na, Pa}, got_msg(Pb, gproc_monitor)),
  500. Pc = t_spawn(A),
  501. t_call(Pa, {apply, gproc, give_away, [Na, Pc]}),
  502. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pc)),
  503. ?assertEqual({gproc_monitor,Na,{migrated,Pc}}, got_msg(Pb, gproc_monitor)),
  504. ?assertEqual(ok, t_call(Pc, die)),
  505. ?assertEqual({gproc_monitor,Na,undefined}, got_msg(Pb, gproc_monitor)),
  506. ok.
  507. %% got_msg(Pb, Tag) ->
  508. %% t_call(Pb,
  509. %% {apply_fun,
  510. %% fun() ->
  511. %% receive
  512. %% M when element(1, M) == Tag ->
  513. %% M
  514. %% after 1000 ->
  515. %% erlang:error({timeout, got_msg, [Pb, Tag]})
  516. %% end
  517. %% end}).
  518. %% Verify that the gproc_dist:sync() call returns true even if a candidate dies
  519. %% while the sync is underway. This test makes use of sys:suspend() to ensure that
  520. %% the other candidate doesn't respond too quickly.
  521. t_sync_cand_dies([A,B,C]) ->
  522. Leader = rpc:call(A, gproc_dist, get_leader, []),
  523. Other = case Leader of
  524. A -> B;
  525. B -> A;
  526. C -> A
  527. end,
  528. ?assertMatch(ok, rpc:call(Other, sys, suspend, [gproc_dist])),
  529. P = rpc:call(Other, erlang, whereis, [gproc_dist]),
  530. Key = rpc:async_call(Leader, gproc_dist, sync, []),
  531. %% The overall timeout for gproc_dist:sync() is 10 seconds. Here, we should
  532. %% still be waiting.
  533. ?assertMatch(timeout, rpc:nb_yield(Key, 1000)),
  534. exit(P, kill),
  535. %% The leader should detect that the other candidate died and respond
  536. %% immediately. Therefore, we should have our answer well within 1 sec.
  537. ?assertMatch({value, true}, rpc:nb_yield(Key, 1000)).
  538. %% Verify that the registry updates consistently if a non-leader node
  539. %% dies.
  540. t_fail_node(Ns) ->
  541. Leader = rpc:call(hd(Ns), gproc_dist, get_leader, []),
  542. [A,B] = Ns -- [Leader],
  543. Na = ?T_NAME,
  544. Nb = ?T_NAME,
  545. Pa = t_spawn_reg(A, Na),
  546. Pb = t_spawn_reg(B, Nb),
  547. ?assertMatch(ok, rpc:call(A, application, stop, [gproc])),
  548. ?assertMatch(ok, t_lookup_everywhere(Na, Ns -- [A], undefined)),
  549. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns -- [A], Pb)),
  550. ?assertMatch(ok, rpc:call(A, application, start, [gproc])),
  551. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, undefined)),
  552. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  553. ?assertMatch(ok, t_call(Pa, die)),
  554. ?assertMatch(ok, t_call(Pb, die)).
  555. t_master_dies([A,B,C] = Ns) ->
  556. Na = ?T_NAME,
  557. Nb = ?T_NAME,
  558. Nc = ?T_NAME,
  559. Pa = t_spawn_reg(A, Na),
  560. Pb = t_spawn_reg(B, Nb),
  561. Pc = t_spawn_reg(C, Nc),
  562. L = rpc:call(A, gproc_dist, get_leader, []),
  563. ?assertMatch(ok, t_lookup_everywhere(Na, Ns, Pa)),
  564. ?assertMatch(ok, t_lookup_everywhere(Nb, Ns, Pb)),
  565. ?assertMatch(ok, t_lookup_everywhere(Nc, Ns, Pc)),
  566. {Nl, Pl} = case L of
  567. A -> {Na, Pa};
  568. B -> {Nb, Pb};
  569. C -> {Nc, Pc}
  570. end,
  571. ?assertMatch(true, rpc:call(A, gproc_dist, sync, [])),
  572. ?assertMatch(ok, rpc:call(L, application, stop, [gproc])),
  573. Names = [{Na,Pa}, {Nb,Pb}, {Nc,Pc}] -- [{Nl, Pl}],
  574. RestNs = Ns -- [L],
  575. %% ?assertMatch(true, rpc:call(hd(RestNs), gproc_dist, sync, [])),
  576. ?assertMatch(true, try_sync(hd(RestNs), RestNs)),
  577. ?assertMatch(ok, t_lookup_everywhere(Nl, RestNs, undefined)),
  578. [?assertMatch(ok, t_lookup_everywhere(Nx, RestNs, Px))
  579. || {Nx, Px} <- Names],
  580. ok.
  581. try_sync(N, Ns) ->
  582. case rpc:call(N, gproc_dist, sync, []) of
  583. {badrpc, _} = Err ->
  584. ?debugFmt(
  585. "Error in gproc_dist:sync() (~p):~n"
  586. " ~p~n"
  587. "Status = ~p~n",
  588. [Err, N,
  589. {Ns, rpc:multicall([N|Ns], sys, get_status, [gproc_dist])}]),
  590. Err;
  591. true ->
  592. true
  593. end.
  594. t_sleep() ->
  595. timer:sleep(500).
  596. t_lookup_everywhere(Key, Nodes, Exp) ->
  597. true = rpc:call(hd(Nodes), gproc_dist, sync, []),
  598. t_lookup_everywhere(Key, Nodes, Exp, 3).
  599. t_lookup_everywhere(Key, _, Exp, 0) ->
  600. {lookup_failed, Key, Exp};
  601. t_lookup_everywhere(Key, Nodes, Exp, I) ->
  602. Expected = [{N, Exp} || N <- Nodes],
  603. Found = [{N,rpc:call(N, gproc, where, [Key])} || N <- Nodes],
  604. if Expected =/= Found ->
  605. ?debugFmt("lookup ~p failed~n"
  606. "(Expected: ~p;~n"
  607. " Found : ~p), retrying...~n",
  608. [Key, Expected, Found]),
  609. t_sleep(),
  610. t_lookup_everywhere(Key, Nodes, Exp, I-1);
  611. true ->
  612. ok
  613. end.
  614. t_read_everywhere(Key, Pid, Nodes, Exp) ->
  615. true = rpc:call(hd(Nodes), gproc_dist, sync, []),
  616. t_read_everywhere(Key, Pid, Nodes, Exp, 3).
  617. t_read_everywhere(Key, _, _, Exp, 0) ->
  618. {read_failed, Key, Exp};
  619. t_read_everywhere(Key, Pid, Nodes, Exp, I) ->
  620. Expected = [{N, Exp} || N <- Nodes],
  621. Found = [{N, read_result(rpc:call(N, gproc, get_value, [Key, Pid]))}
  622. || N <- Nodes],
  623. if Expected =/= Found ->
  624. ?debugFmt("read ~p failed~n"
  625. "(Expected: ~p;~n"
  626. " Found : ~p), retrying...~n",
  627. [{Key, Pid}, Expected, Found]),
  628. t_sleep(),
  629. t_read_everywhere(Key, Pid, Nodes, Exp, I-1);
  630. true ->
  631. ok
  632. end.
  633. read_result({badrpc, {'EXIT', {badarg, _}}}) -> badarg;
  634. read_result(R) -> R.
  635. t_spawn(Node) -> gproc_test_lib:t_spawn(Node).
  636. t_spawn(Node, Selective) -> gproc_test_lib:t_spawn(Node, Selective).
  637. t_spawn_mreg(Node, KVL) -> gproc_test_lib:t_spawn_mreg(Node, KVL).
  638. t_spawn_reg(Node, N) -> gproc_test_lib:t_spawn_reg(Node, N).
  639. t_spawn_reg(Node, N, V) -> gproc_test_lib:t_spawn_reg(Node, N, V).
  640. t_spawn_reg(Node, N, V, As) -> gproc_test_lib:t_spawn_reg(Node, N, V, As).
  641. t_spawn_reg_shared(Node, N, V) -> gproc_test_lib:t_spawn_reg_shared(Node, N, V).
  642. got_msg(P) -> gproc_test_lib:got_msg(P).
  643. got_msg(P, Tag) -> gproc_test_lib:got_msg(P, Tag).
  644. no_msg(P, Timeout) -> gproc_test_lib:no_msg(P, Timeout).
  645. t_call(P, Req) ->
  646. gproc_test_lib:t_call(P, Req).
  647. start_slaves(Ns) ->
  648. [H|T] = Nodes = [start_slave(N) || N <- Ns],
  649. _ = [rpc:call(H, net_adm, ping, [N]) || N <- T],
  650. Nodes.
  651. start_slave(Name) ->
  652. case node() of
  653. nonode@nohost ->
  654. os:cmd("epmd -daemon"),
  655. {ok, _} = net_kernel:start([gproc_master, shortnames]);
  656. _ ->
  657. ok
  658. end,
  659. {Pa, Pz} = paths(),
  660. Paths = "-pa ./ -pz ../ebin" ++
  661. lists:flatten([[" -pa " ++ Path || Path <- Pa],
  662. [" -pz " ++ Path || Path <- Pz]]),
  663. {ok, Node} = slave:start(host(), Name, Paths),
  664. Node.
  665. paths() ->
  666. Path = code:get_path(),
  667. {ok, [[Root]]} = init:get_argument(root),
  668. {Pas, Rest} = lists:splitwith(fun(P) ->
  669. not lists:prefix(Root, P)
  670. end, Path),
  671. {_, Pzs} = lists:splitwith(fun(P) ->
  672. lists:prefix(Root, P)
  673. end, Rest),
  674. {Pas, Pzs}.
  675. host() ->
  676. [_Name, Host] = re:split(atom_to_list(node()), "@", [{return, list}]),
  677. list_to_atom(Host).
  678. -endif.