gproc_eqc_tests.erl 21 KB

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