gproc_dist_tests.erl 26 KB

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