gproc_eqc_tests.erl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. %%% File : gproc_eqc_tests.erl
  2. %%% Author : <norton@alum.mit.edu>
  3. %%% : <Ulf.Wiger@erlang-consulting.com>
  4. %%% : <John.Hughes@quviq.com>
  5. %%% Description : QuickCheck test model for gproc
  6. %%% Created : 11 Dec 2008 by <John Hughes@JTABLET2007>
  7. -module(gproc_eqc_tests).
  8. -ifdef(EQC).
  9. -include_lib("eqc/include/eqc.hrl").
  10. -include_lib("eqc/include/eqc_statem.hrl").
  11. -compile(export_all).
  12. %%
  13. %% QUESTIONS:
  14. %%
  15. %% - does set_value for Class==a make sense?
  16. %% - shouldn't mreg return {true, keys()} rather than the {true,
  17. %% objects()} upon success?
  18. %%
  19. %% TODO:
  20. %%
  21. %% - implement mreg
  22. %% - implement send
  23. %% - implement info
  24. %% - implement select
  25. %% - implement first/next/prev/last
  26. %% - implement table
  27. %%
  28. %% records
  29. -record(key,
  30. {class %% class()
  31. , scope %% scope()
  32. , name %% name()
  33. }).
  34. -record(reg,
  35. {pid %% pid()
  36. , key %% key()
  37. , value %% int()
  38. }).
  39. -record(state,
  40. {pids=[] %% [pid()]
  41. , killed=[] %% [pid()]
  42. , regs=[] %% [reg()]
  43. , waiters=[] %% [{key(),pid()}]
  44. }).
  45. %% external API
  46. good_number_of_tests() ->
  47. 3000.
  48. %% hook to run from eunit. Note: a small number of tests.
  49. %% I recommend running at least 3000 to get to interesting stuff.
  50. %%
  51. gproc_test_() ->
  52. {timeout, 60, [fun() -> run(100) end]}.
  53. %% When run from eunit, we need to set the group leader so that EQC
  54. %% reporting (the dots) are made visible - that is, if that's what we want.
  55. verbose_run(N) ->
  56. erlang:group_leader(whereis(user), self()),
  57. run(N).
  58. %% 3000 tests seems like a large number, but this seems to be needed
  59. %% to reach enough variation in the tests.
  60. all_tests() ->
  61. eqc:module({numtests, good_number_of_tests()}, ?MODULE).
  62. run() ->
  63. run(good_number_of_tests()).
  64. run(Num) ->
  65. error_logger:delete_report_handler(error_logger_tty_h),
  66. eqc:quickcheck(eqc:numtests(Num, prop_gproc())).
  67. %% Command generator, S is the state
  68. command(S) ->
  69. oneof(
  70. %% spawn
  71. [ {call,?MODULE,spawn, []} ]
  72. %% where
  73. ++ [ {call,?MODULE,where, [key()]} ]
  74. ++ [ {call,?MODULE,await_new, [name_key()]} ]
  75. %% kill
  76. ++ [ oneof([
  77. %% kill
  78. {call,?MODULE,kill, [elements(S#state.pids)]}
  79. %% register
  80. , {call,?MODULE,reg, ?LET(Key, key(),[elements(S#state.pids), Key, reg_value(Key)])}
  81. %% unregister
  82. , {call,?MODULE,unreg, [elements(S#state.pids), key()]}
  83. %% many register
  84. , {call, ?MODULE, mreg, ?LET({Pid,Class,Scope}, {elements(S#state.pids),class(),scope()}, [Pid, Class, Scope, mreg_values(S, Class, Scope)])}
  85. %%, {call,?MODULE,mreg, [elements(S#state.pids), class(), scope()
  86. %% , list({name(), value()})]}
  87. %% set_value
  88. , {call,?MODULE,set_value, ?LET(Key, key(),[elements(S#state.pids), Key, reg_value(Key)])}
  89. %% update_counter
  90. , {call,?MODULE,update_counter, [elements(S#state.pids), key(), value()]}
  91. %% get_value
  92. , {call,?MODULE,get_value, [elements(S#state.pids), key()]}
  93. %% lookup_pid
  94. , {call,?MODULE,lookup_pid, [key()]}
  95. %% lookup_pids
  96. , {call,?MODULE,lookup_pids, [key()]}
  97. ])
  98. || S#state.pids/=[] ]
  99. ++ [
  100. %% await on existing value
  101. {call,?MODULE,await_existing, [elements(S#state.regs)]}
  102. || S#state.regs/=[] ]
  103. ).
  104. %% generator class
  105. class() -> elements([n,p,c,a]).
  106. %% generator scope
  107. scope() -> l.
  108. %% generator name
  109. name() -> elements(names()).
  110. names() -> [x,y,z,w].
  111. %% generator key
  112. key() -> key(class(), scope(), name()).
  113. key(Class, Scope, Name) ->
  114. #key{class=Class, scope=Scope, name=Name}.
  115. name_key() ->
  116. key(n, scope(), name()).
  117. %% generator value
  118. value() -> frequency([{8, int()}, {1, undefined}, {1, make_ref()}]).
  119. %% value for reg and set_value
  120. %% 'a' and 'c' should only have integers as values (reg: value is ignored for 'a')
  121. reg_value(#key{class=C}) when C == a; C == c -> int();
  122. reg_value(_) -> value().
  123. mreg_values(_S, Class, Scope) ->
  124. ?LET(Names, subset(names()),
  125. [?LET(K, key(Class, Scope, N), {K, reg_value(K)}) || N <- Names]).
  126. %% Snipped from the TrapExit QuickCheck tutorials
  127. %% http://trapexit.org/SubSetGenerator
  128. subset(Generators) ->
  129. ?LET(Keep,[ {bool(),G} || G<-Generators],
  130. [ G || {true,G}<-Keep]).
  131. %% helpers
  132. is_register_ok(_S,_Pid,#key{class=c},Value) when not is_integer(Value) ->
  133. false;
  134. is_register_ok(_S,_Pid,#key{class=a},Value) ->
  135. Value == undefined;
  136. is_register_ok(S,Pid,Key,_Value) ->
  137. [] == [ Pid1 || #reg{pid=Pid1,key=Key1}
  138. <- S#state.regs, is_register_eq(Pid,Key,Pid1,Key1) ].
  139. is_mreg_ok(S, Pid, List) ->
  140. lists:all(fun({Key, Value}) ->
  141. is_register_ok(S, Pid, Key, Value)
  142. end, List).
  143. is_register_eq(_PidA,#key{class=Class}=KeyA,_PidB,KeyB)
  144. when Class == n; Class ==a ->
  145. KeyA==KeyB;
  146. is_register_eq(PidA,KeyA,PidB,KeyB) ->
  147. PidA==PidB andalso KeyA==KeyB.
  148. is_unregister_ok(S,Pid,Key) ->
  149. [] /= [ Pid1 || #reg{pid=Pid1,key=Key1}
  150. <- S#state.regs, is_unregister_eq(Pid,Key,Pid1,Key1) ].
  151. is_unregister_eq(PidA,KeyA,PidB,KeyB) ->
  152. KeyA==KeyB andalso PidA==PidB.
  153. is_registered_and_alive(S,Pid,Key) ->
  154. is_unregister_ok(S,Pid,Key)
  155. andalso lists:member(Pid,S#state.pids).
  156. %% Initialize the state
  157. initial_state() ->
  158. #state{}.
  159. %% Next state transformation, S is the current state
  160. %% spawn
  161. next_state(S,V,{call,_,spawn,_}) ->
  162. S#state{pids=[V|S#state.pids]};
  163. %% kill
  164. next_state(S,_V,{call,_,kill,[Pid]}) ->
  165. S#state{pids=S#state.pids -- [Pid]
  166. , killed=[Pid|S#state.killed]
  167. , regs=[ X || #reg{pid=Pid1}=X <- S#state.regs, Pid/=Pid1 ]
  168. };
  169. %% reg
  170. next_state(S,_V,{call,_,reg,[Pid,Key,Value]}) ->
  171. case is_register_ok(S,Pid,Key,Value) of
  172. false ->
  173. S;
  174. true ->
  175. update_state_reg(S, Pid, Key, Value)
  176. end;
  177. next_state(S,_V,{call,_,mreg,[Pid, _Class, _Scope, List]}) ->
  178. case is_mreg_ok(S, Pid, List) of
  179. false ->
  180. S;
  181. true ->
  182. lists:foldl(
  183. fun({Key, Value}, Acc) ->
  184. update_state_reg(Acc, Pid, Key, Value)
  185. end, S, List)
  186. end;
  187. %% unreg
  188. next_state(S,_V,{call,_,unreg,[Pid,Key]}) ->
  189. case is_unregister_ok(S,Pid,Key) of
  190. false ->
  191. S;
  192. true ->
  193. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  194. case lists:partition(FunC, S#state.regs) of
  195. {[#reg{value=Value}], Others} ->
  196. S1 = S#state{regs=Others},
  197. case Key of
  198. #key{class=c,name=Name} ->
  199. %% update aggr counter
  200. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  201. case lists:partition(FunA, S1#state.regs) of
  202. {[], _Others1} ->
  203. S1;
  204. {[Reg], Others1} ->
  205. S1#state{regs=[Reg#reg{value=Reg#reg.value-Value}|Others1]}
  206. end;
  207. _ ->
  208. S1
  209. end
  210. end
  211. end;
  212. %% set_value
  213. next_state(S,_V,{call,_,set_value,[Pid,Key,Value]}) ->
  214. case is_registered_and_alive(S,Pid,Key) of
  215. false ->
  216. S;
  217. true ->
  218. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  219. case lists:partition(FunC, S#state.regs) of
  220. {[#reg{value=OldValue}=OldReg], Others} ->
  221. S1 = S#state{regs=[OldReg#reg{value=Value}|Others]},
  222. case Key of
  223. #key{class=c,name=Name} ->
  224. %% aggr counter update
  225. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  226. case lists:partition(FunA, S1#state.regs) of
  227. {[], _Others1} ->
  228. S1;
  229. {[Reg], Others1} ->
  230. S1#state{regs=[Reg#reg{value=Reg#reg.value-OldValue+Value}|Others1]}
  231. end;
  232. _ ->
  233. S1
  234. end
  235. end
  236. end;
  237. %% update_counter
  238. next_state(S,_V,{call,_,update_counter,[Pid,#key{class=Class}=Key,Incr]})
  239. when Class == c, is_integer(Incr) ->
  240. case is_registered_and_alive(S,Pid,Key) of
  241. false ->
  242. S;
  243. true ->
  244. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  245. case lists:partition(FunC, S#state.regs) of
  246. {[#reg{value=OldValue}=OldReg], Others} ->
  247. S1 = S#state{regs=[OldReg#reg{value=OldValue+Incr}|Others]},
  248. case Key of
  249. #key{class=c,name=Name} ->
  250. %% aggr counter update
  251. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  252. case lists:partition(FunA, S1#state.regs) of
  253. {[], _Others1} ->
  254. S1;
  255. {[Reg], Others1} ->
  256. S1#state{regs=[Reg#reg{value=Reg#reg.value+Incr}|Others1]}
  257. end;
  258. _ ->
  259. S1
  260. end
  261. end
  262. end;
  263. next_state(S,V,{call,_,await_new,[Key]}) ->
  264. S#state{waiters = [{Key,V}|S#state.waiters]};
  265. %% otherwise
  266. next_state(S,_V,{call,_,_,_}) ->
  267. S.
  268. update_state_reg(S, Pid, Key, Value) ->
  269. case Key of
  270. #key{class=a,name=Name} ->
  271. %% initialize aggr counter
  272. FunC = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == c andalso Name==Name1) end,
  273. {Regs, _Others} = lists:partition(FunC, S#state.regs),
  274. InitialValue = lists:sum([ V || #reg{value=V} <- Regs ]),
  275. S#state{regs=[#reg{pid=Pid,key=Key,value=InitialValue}|S#state.regs]};
  276. #key{class=c,name=Name} ->
  277. S1 = S#state{regs=[#reg{pid=Pid,key=Key,value=Value}|S#state.regs]},
  278. %% update aggr counter
  279. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  280. case lists:partition(FunA, S1#state.regs) of
  281. {[Reg], Others} ->
  282. S1#state{regs=[Reg#reg{value=Reg#reg.value+Value}|Others]};
  283. {[], _Others} ->
  284. S1
  285. end;
  286. _ ->
  287. S#state{regs=[#reg{pid=Pid,key=Key,value=Value}|S#state.regs],
  288. waiters = [W || {K,_} = W <- S#state.waiters,
  289. K =/= Key]}
  290. end.
  291. %% Precondition, checked before command is added to the command
  292. %% sequence
  293. precondition(S, {call,_,reg, [Pid, _Key, _Value]}) ->
  294. lists:member(Pid, S#state.pids);
  295. precondition(S, {call,_,unreg, [Pid, _Key]}) ->
  296. lists:member(Pid, S#state.pids);
  297. precondition(S, {call,_,await_new,[#key{class=C}=Key]}) ->
  298. C == n andalso
  299. not lists:keymember(Key,#reg.key,S#state.regs);
  300. precondition(S, {call,_,mreg,[Pid, Class, _Scope, List]}) ->
  301. %% TODO: lift this restriction to generate all classes mreg can handle
  302. Class == n andalso
  303. lists:member(Pid, S#state.pids) andalso
  304. lists:all(fun({#key{class=C},_}) -> C == n end, List);
  305. precondition(S, {call,_,await_existing,[#reg{key=#key{class=C}=Key}]}) ->
  306. C == n andalso
  307. lists:keymember(Key, #reg.key, S#state.regs);
  308. precondition(S,{call,_,get_value,[Pid,_]}) ->
  309. lists:member(Pid,S#state.pids);
  310. precondition(_S,{call,_,_,_}) ->
  311. true.
  312. %% Postcondition, checked after command has been evaluated. S is the
  313. %% state before next_state(S,_,<command>)
  314. %% spawn
  315. postcondition(_S,{call,_,spawn,_},_Res) ->
  316. true;
  317. %% kill
  318. postcondition(_S,{call,_,kill,_},_Res) ->
  319. true;
  320. %% where
  321. postcondition(S,{call,_,where,[#key{class=Class}=Key]},Res) ->
  322. if Class == n orelse Class == a ->
  323. case lists:keysearch(Key,#reg.key,S#state.regs) of
  324. {value, #reg{pid=Pid}} ->
  325. Res == Pid;
  326. false ->
  327. Res == undefined
  328. end;
  329. true ->
  330. case Res of
  331. {'EXIT', {badarg, _}} ->
  332. true;
  333. _ ->
  334. false
  335. end
  336. end;
  337. %% reg
  338. postcondition(S,{call,_,reg,[Pid,Key,Value]},Res) ->
  339. case Res of
  340. true ->
  341. is_register_ok(S,Pid,Key,Value) andalso
  342. check_waiters(Pid, Key, Value, S#state.waiters);
  343. {'EXIT', {badarg, _}} ->
  344. is_unregister_ok(S,Pid,Key)
  345. orelse not is_register_ok(S,Pid,Key,Value)
  346. end;
  347. postcondition(S,{call,_,mreg,[Pid,_Class,_Scope,List]},Res) ->
  348. case Res of
  349. true ->
  350. is_mreg_ok(S,Pid,List)
  351. andalso lists:all(fun({K,V}) ->
  352. check_waiters(Pid,K,V, S#state.waiters)
  353. end, List);
  354. {'EXIT', {badarg,_}} ->
  355. not is_mreg_ok(S,Pid,List)
  356. end;
  357. %% unreg
  358. postcondition(S,{call,_,unreg,[Pid,Key]},Res) ->
  359. case Res of
  360. true ->
  361. is_unregister_ok(S,Pid,Key);
  362. {'EXIT', {badarg, _}} ->
  363. not is_unregister_ok(S,Pid,Key)
  364. end;
  365. %% set_value
  366. postcondition(S,{call,_,set_value,[Pid,Key,_Value]},Res) ->
  367. case Res of
  368. true ->
  369. is_registered_and_alive(S,Pid,Key);
  370. {'EXIT', {badarg, _}} ->
  371. not is_registered_and_alive(S,Pid,Key)
  372. orelse (Key#key.class == c
  373. andalso is_registered_and_alive(S, Pid, Key))
  374. end;
  375. %% update_counter
  376. postcondition(S,{call,_,update_counter,[Pid,#key{class=Class}=Key,Incr]},Res)
  377. when Class == c, is_integer(Incr) ->
  378. case [ Value1 || #reg{pid=Pid1,key=Key1,value=Value1} <- S#state.regs
  379. , (Pid==Pid1 andalso Key==Key1) ] of
  380. [] ->
  381. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  382. [Value] when is_integer(Value) ->
  383. Res == Value+Incr;
  384. [_] ->
  385. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end
  386. end;
  387. postcondition(_S,{call,_,update_counter,[_Pid,_Key,_Incr]},Res) ->
  388. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  389. %% get_value
  390. postcondition(S,{call,_,get_value,[Pid,Key]},Res) ->
  391. case [ Value1 || #reg{pid=Pid1,key=Key1,value=Value1} <- S#state.regs
  392. , (Pid==Pid1 andalso Key==Key1) ] of
  393. [] ->
  394. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  395. [Value] ->
  396. Res == Value
  397. end;
  398. %% lookup_pid
  399. postcondition(S,{call,_,lookup_pid,[#key{class=Class}=Key]},Res)
  400. when Class == n; Class == a ->
  401. case [ Pid1 || #reg{pid=Pid1,key=Key1} <- S#state.regs
  402. , Key==Key1 ] of
  403. [] ->
  404. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  405. [Pid] ->
  406. Res == Pid
  407. end;
  408. postcondition(_S,{call,_,lookup_pid,[_Key]},Res) ->
  409. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  410. %% lookup_pids
  411. postcondition(S,{call,_,lookup_pids,[#key{class=Class}=Key]},Res)
  412. when Class == n; Class == a; Class == c; Class == p ->
  413. Pids = [ Pid1 || #reg{pid=Pid1,key=Key1} <- S#state.regs
  414. , Key==Key1 ],
  415. lists:sort(Res) == lists:sort(Pids);
  416. postcondition(_S, {call,_,await_new,[#key{}]}, Pid) ->
  417. is_pid(Pid);
  418. postcondition(S,{call,_,await_existing,[#reg{key=Key}]}, {P1,V1}) ->
  419. case lists:keyfind(Key, #reg.key, S#state.regs) of
  420. #reg{pid=P1, value = V1} -> true;
  421. _ -> false
  422. end;
  423. %% postcondition(_S,{call,_,lookup_pids,[_Key]},Res) ->
  424. %% case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  425. %% otherwise
  426. postcondition(_S,{call,_,_,_},_Res) ->
  427. false.
  428. %%% Spec fixes
  429. %%%
  430. %%% - added precondition for set_value must be integer (could be changed
  431. %%% to neg. test)
  432. %%% - updated postcondition for update_counter to check for non-integers
  433. %%%
  434. %%% It still crashes on lists:sum in next_state... Maybe we should change
  435. %%% the generators instead!
  436. prop_gproc() ->
  437. ?FORALL(Cmds,commands(?MODULE),
  438. ?TRAPEXIT(
  439. begin
  440. ok = stop_app(),
  441. ok = start_app(),
  442. {H,S,Res} = run_commands(?MODULE,Cmds),
  443. kill_all_pids({H,S}),
  444. %% whenfail
  445. ?WHENFAIL(
  446. begin
  447. io:format("~nHISTORY:"),
  448. if
  449. length(H) < 1 ->
  450. io:format(" none~n");
  451. true ->
  452. CmdsH = eqc_statem:zip(Cmds,H),
  453. [ begin
  454. {Cmd,{State,Reply}} = lists:nth(N,CmdsH),
  455. io:format("~n #~p:~n\tCmd: ~p~n\tReply: ~p~n\tState: ~p~n",
  456. [N,Cmd,Reply,State])
  457. end
  458. || N <- lists:seq(1,length(CmdsH)) ]
  459. end,
  460. io:format("~nRESULT:~n\t~p~n",[Res]),
  461. io:format("~nSTATE:~n\t~p~n",[S])
  462. end,
  463. Res == ok)
  464. end)).
  465. %% helpers
  466. start_app() ->
  467. case application:start(gproc) of
  468. {error, {already_started,_}} ->
  469. stop_app(),
  470. ok = application:start(gproc);
  471. ok ->
  472. ok
  473. end.
  474. stop_app() ->
  475. case application:stop(gproc) of
  476. {error, {not_started,_}} ->
  477. ok;
  478. ok ->
  479. ok
  480. end.
  481. %% If using the scheduler... This code needs to run in a separate
  482. %% module, so it can be compiled without instrumentation.
  483. kill_all_pids(Pid) when is_pid(Pid) ->
  484. case is_process_alive(Pid) of
  485. true ->
  486. exit(Pid,kill);
  487. false ->
  488. ok
  489. end;
  490. kill_all_pids(Tup) when is_tuple(Tup) ->
  491. kill_all_pids(tuple_to_list(Tup));
  492. kill_all_pids([H|T]) ->
  493. kill_all_pids(H),
  494. kill_all_pids(T);
  495. kill_all_pids(_) ->
  496. ok.
  497. %% spawn
  498. spawn() ->
  499. spawn(fun() -> loop() end).
  500. loop() ->
  501. receive
  502. {From, Ref, F} ->
  503. From ! {Ref, catch F()},
  504. loop();
  505. stop -> ok
  506. end.
  507. %% kill
  508. kill(Pid) ->
  509. exit(Pid,foo),
  510. timer:sleep(10).
  511. %% where
  512. where(#key{class=Class,scope=Scope,name=Name}) ->
  513. catch gproc:where({Class,Scope,Name}).
  514. %% reg
  515. reg(Pid,#key{class=Class,scope=Scope,name=Name},Value) ->
  516. do(Pid, fun() -> catch gproc:reg({Class,Scope,Name},Value) end).
  517. mreg(Pid, Class, Scope, List) ->
  518. do(Pid, fun() -> catch gproc:mreg(Class,Scope,[{Name,Value} || {#key{name = Name}, Value} <- List]) end).
  519. %% unreg
  520. unreg(Pid,#key{class=Class,scope=Scope,name=Name}) ->
  521. do(Pid, fun() -> catch gproc:unreg({Class,Scope,Name}) end).
  522. %% set_value
  523. set_value(Pid,#key{class=Class,scope=Scope,name=Name},Value) ->
  524. do(Pid, fun() -> catch gproc:set_value({Class,Scope,Name},Value) end).
  525. %% update_counter
  526. update_counter(Pid, #key{class=Class,scope=Scope,name=Name},Incr) ->
  527. do(Pid, fun() -> catch gproc:update_counter({Class,Scope,Name},Incr) end).
  528. %% get_value
  529. get_value(Pid,#key{class=Class,scope=Scope,name=Name}) ->
  530. do(Pid, fun() -> catch gproc:get_value({Class,Scope,Name}) end).
  531. %% lookup_pid
  532. lookup_pid(#key{class=Class,scope=Scope,name=Name}) ->
  533. catch gproc:lookup_pid({Class,Scope,Name}).
  534. %% lookup_pids
  535. lookup_pids(#key{class=Class,scope=Scope,name=Name}) ->
  536. catch gproc:lookup_pids({Class,Scope,Name}).
  537. %% do
  538. do(Pid, F) ->
  539. Ref = erlang:monitor(process, Pid),
  540. Pid ! {self(), Ref, F},
  541. receive
  542. {'DOWN', Ref, process, Pid, Reason} ->
  543. {'EXIT', {'DOWN', Reason}};
  544. {Ref, Result} ->
  545. erlang:demonitor(Ref),
  546. Result
  547. after 3000 ->
  548. {'EXIT', timeout}
  549. end.
  550. await_existing(#reg{key = #key{class=Class,scope=Scope,name=Name}}) ->
  551. %% short timeout, this call is expected to work
  552. gproc:await({Class,Scope,Name}, 10000).
  553. await_new(#key{class=Class,scope=Scope,name=Name}) ->
  554. spawn(
  555. fun() ->
  556. Res = (catch gproc:await({Class,Scope,Name})),
  557. receive
  558. {From, send_result} ->
  559. From ! {result, Res},
  560. timer:sleep(1000)
  561. end
  562. end).
  563. check_waiters(Pid, Key, Value, Waiters) ->
  564. case [W || {K, W} <- Waiters,
  565. K == Key] of
  566. [] ->
  567. true;
  568. WPids ->
  569. lists:all(fun(WPid) ->
  570. check_waiter(WPid, Pid, Key, Value)
  571. end, WPids)
  572. end.
  573. check_waiter(WPid, Pid, _Key, Value) ->
  574. MRef = erlang:monitor(process, WPid),
  575. WPid ! {self(), send_result},
  576. receive
  577. {result, Res} ->
  578. {Pid,Value} == Res;
  579. {'DOWN', MRef, _, _, R} ->
  580. erlang:error(R)
  581. after 1000 ->
  582. erlang:error(timeout)
  583. end.
  584. -endif.