gproc_eqc.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. %%% File : gproc_eqc.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).
  8. -include("eqc.hrl").
  9. -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. }).
  43. %% external API
  44. start_test() ->
  45. eqc:module({numtests, 500}, ?MODULE).
  46. run() ->
  47. run(500).
  48. run(Num) ->
  49. eqc:quickcheck(eqc:numtests(Num, prop_gproc())).
  50. %% Command generator, S is the state
  51. command(S) ->
  52. oneof(
  53. %% spawn
  54. [ {call,?MODULE,spawn, []} ]
  55. %% where
  56. ++ [ {call,?MODULE,where, [key()]} ]
  57. %% kill
  58. ++ [ oneof([
  59. %% kill
  60. {call,?MODULE,kill, [elements(S#state.pids)]}
  61. %% register
  62. , {call,?MODULE,reg, [elements(S#state.pids), key(), value()]}
  63. %% unregister
  64. , {call,?MODULE,unreg, [elements(S#state.pids), key()]}
  65. %% many register
  66. %%, {call,?MODULE,mreg, [elements(S#state.pids), class(), scope()
  67. %% , list({name(), value()})]}
  68. %% set_value
  69. , {call,?MODULE,set_value, [elements(S#state.pids), key(), value()]}
  70. %% update_counter
  71. , {call,?MODULE,update_counter, [elements(S#state.pids), key(), value()]}
  72. %% get_value
  73. , {call,?MODULE,get_value, [elements(S#state.pids), key()]}
  74. %% lookup_pid
  75. , {call,?MODULE,lookup_pid, [key()]}
  76. %% lookup_pids
  77. , {call,?MODULE,lookup_pids, [key()]}
  78. ])
  79. || S#state.pids/=[] ]
  80. ).
  81. %% generator class
  82. class() -> elements([n,p,c,a]).
  83. %% generator scope
  84. scope() -> l.
  85. %% generator name
  86. name() -> elements([x,y,z,w]).
  87. %% generator key
  88. key() -> #key{class=class(), scope=scope(), name=name()}.
  89. %% generator value
  90. value() -> frequency([{8, int()}, {1, undefined}, {1, make_ref()}]).
  91. %% helpers
  92. is_register_ok(_S,_Pid,#key{class=Class},Value)
  93. when Class == c, not is_integer(Value) ->
  94. false;
  95. is_register_ok(S,Pid,Key,_Value) ->
  96. [] == [ Pid1 || #reg{pid=Pid1,key=Key1}
  97. <- S#state.regs, is_register_eq(Pid,Key,Pid1,Key1) ].
  98. is_register_eq(_PidA,#key{class=Class}=KeyA,_PidB,KeyB)
  99. when Class == n; Class ==a ->
  100. KeyA==KeyB;
  101. is_register_eq(PidA,KeyA,PidB,KeyB) ->
  102. PidA==PidB andalso KeyA==KeyB.
  103. is_unregister_ok(S,Pid,Key) ->
  104. [] /= [ Pid1 || #reg{pid=Pid1,key=Key1}
  105. <- S#state.regs, is_unregister_eq(Pid,Key,Pid1,Key1) ].
  106. is_unregister_eq(PidA,KeyA,PidB,KeyB) ->
  107. KeyA==KeyB andalso PidA==PidB.
  108. is_registered_and_alive(S,Pid,Key) ->
  109. is_unregister_ok(S,Pid,Key)
  110. andalso lists:member(Pid,S#state.pids).
  111. %% Initialize the state
  112. initial_state() ->
  113. #state{}.
  114. %% Next state transformation, S is the current state
  115. %% spawn
  116. next_state(S,V,{call,_,spawn,_}) ->
  117. S#state{pids=[V|S#state.pids]};
  118. %% kill
  119. next_state(S,_V,{call,_,kill,[Pid]}) ->
  120. S#state{pids=S#state.pids -- [Pid]
  121. , killed=[Pid|S#state.killed]
  122. , regs=[ X || #reg{pid=Pid1}=X <- S#state.regs, Pid/=Pid1 ]
  123. };
  124. %% reg
  125. next_state(S,_V,{call,_,reg,[Pid,Key,Value]}) ->
  126. case is_register_ok(S,Pid,Key,Value) of
  127. false ->
  128. S;
  129. true ->
  130. case Key of
  131. #key{class=a,name=Name} ->
  132. %% initialize aggr counter
  133. FunC = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == c andalso Name==Name1) end,
  134. {Regs, _Others} = lists:partition(FunC, S#state.regs),
  135. InitialValue = lists:sum([ V || #reg{value=V} <- Regs ]),
  136. S#state{regs=[#reg{pid=Pid,key=Key,value=InitialValue}|S#state.regs]};
  137. #key{class=c,name=Name} ->
  138. S1 = S#state{regs=[#reg{pid=Pid,key=Key,value=Value}|S#state.regs]},
  139. %% update aggr counter
  140. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  141. case lists:partition(FunA, S1#state.regs) of
  142. {[Reg], Others} ->
  143. S1#state{regs=[Reg#reg{value=Reg#reg.value+Value}|Others]};
  144. {[], _Others} ->
  145. S1
  146. end;
  147. _ ->
  148. S#state{regs=[#reg{pid=Pid,key=Key,value=Value}|S#state.regs]}
  149. end
  150. end;
  151. %% unreg
  152. next_state(S,_V,{call,_,unreg,[Pid,Key]}) ->
  153. case is_unregister_ok(S,Pid,Key) of
  154. false ->
  155. S;
  156. true ->
  157. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  158. case lists:partition(FunC, S#state.regs) of
  159. {[#reg{value=Value}], Others} ->
  160. S1 = S#state{regs=Others},
  161. case Key of
  162. #key{class=c,name=Name} ->
  163. %% update aggr counter
  164. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  165. case lists:partition(FunA, S1#state.regs) of
  166. {[], _Others1} ->
  167. S1;
  168. {[Reg], Others1} ->
  169. S1#state{regs=[Reg#reg{value=Reg#reg.value-Value}|Others1]}
  170. end;
  171. _ ->
  172. S1
  173. end
  174. end
  175. end;
  176. %% set_value
  177. next_state(S,_V,{call,_,set_value,[Pid,Key,Value]}) ->
  178. case is_registered_and_alive(S,Pid,Key) of
  179. false ->
  180. S;
  181. true ->
  182. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  183. case lists:partition(FunC, S#state.regs) of
  184. {[#reg{value=OldValue}=OldReg], Others} ->
  185. S1 = S#state{regs=[OldReg#reg{value=Value}|Others]},
  186. case Key of
  187. #key{class=c,name=Name} ->
  188. %% aggr counter update
  189. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  190. case lists:partition(FunA, S1#state.regs) of
  191. {[], _Others1} ->
  192. S1;
  193. {[Reg], Others1} ->
  194. S1#state{regs=[Reg#reg{value=Reg#reg.value-OldValue+Value}|Others1]}
  195. end;
  196. _ ->
  197. S1
  198. end
  199. end
  200. end;
  201. %% update_counter
  202. next_state(S,_V,{call,_,update_counter,[Pid,#key{class=Class}=Key,Incr]})
  203. when Class == c ->
  204. case is_registered_and_alive(S,Pid,Key) of
  205. false ->
  206. S;
  207. true ->
  208. FunC = fun(#reg{pid=Pid1,key=Key1}) -> (Pid==Pid1 andalso Key==Key1) end,
  209. case lists:partition(FunC, S#state.regs) of
  210. {[#reg{value=OldValue}=OldReg], Others} ->
  211. S1 = S#state{regs=[OldReg#reg{value=OldValue+Incr}|Others]},
  212. case Key of
  213. #key{class=c,name=Name} ->
  214. %% aggr counter update
  215. FunA = fun(#reg{key=#key{class=Class1,name=Name1}}) -> (Class1 == a andalso Name==Name1) end,
  216. case lists:partition(FunA, S1#state.regs) of
  217. {[], _Others1} ->
  218. S1;
  219. {[Reg], Others1} ->
  220. S1#state{regs=[Reg#reg{value=Reg#reg.value+Incr}|Others1]}
  221. end;
  222. _ ->
  223. S1
  224. end
  225. end
  226. end;
  227. %% otherwise
  228. next_state(S,_V,{call,_,_,_}) ->
  229. S.
  230. %% Precondition, checked before command is added to the command
  231. %% sequence
  232. precondition(_S,{call,_,_,_}) ->
  233. true.
  234. %% Postcondition, checked after command has been evaluated. S is the
  235. %% state before next_state(S,_,<command>)
  236. %% spawn
  237. postcondition(_S,{call,_,spawn,_},_Res) ->
  238. true;
  239. %% kill
  240. postcondition(_S,{call,_,kill,_},_Res) ->
  241. true;
  242. %% where
  243. postcondition(S,{call,_,where,[#key{class=Class}=Key]},Res) ->
  244. if Class == n orelse Class == a ->
  245. case lists:keysearch(Key,#reg.key,S#state.regs) of
  246. {value, #reg{pid=Pid}} ->
  247. Res == Pid;
  248. false ->
  249. Res == undefined
  250. end;
  251. true ->
  252. case Res of
  253. {'EXIT', {badarg, _}} ->
  254. true;
  255. _ ->
  256. false
  257. end
  258. end;
  259. %% reg
  260. postcondition(S,{call,_,reg,[Pid,Key,Value]},Res) ->
  261. case Res of
  262. true ->
  263. is_register_ok(S,Pid,Key,Value);
  264. {'EXIT', {badarg, _}} ->
  265. is_unregister_ok(S,Pid,Key)
  266. orelse not is_register_ok(S,Pid,Key,Value)
  267. end;
  268. %% unreg
  269. postcondition(S,{call,_,unreg,[Pid,Key]},Res) ->
  270. case Res of
  271. true ->
  272. is_unregister_ok(S,Pid,Key);
  273. {'EXIT', {badarg, _}} ->
  274. not is_unregister_ok(S,Pid,Key)
  275. end;
  276. %% set_value
  277. postcondition(S,{call,_,set_value,[Pid,Key,_Value]},Res) ->
  278. case Res of
  279. true ->
  280. is_registered_and_alive(S,Pid,Key);
  281. {'EXIT', {badarg, _}} ->
  282. not is_registered_and_alive(S,Pid,Key)
  283. orelse (Key#key.class == c
  284. andalso is_registered_and_alive(S, Pid, Key))
  285. end;
  286. %% update_counter
  287. postcondition(S,{call,_,update_counter,[Pid,#key{class=Class}=Key,Incr]},Res)
  288. when Class == c ->
  289. case [ Value1 || #reg{pid=Pid1,key=Key1,value=Value1} <- S#state.regs
  290. , (Pid==Pid1 andalso Key==Key1) ] of
  291. [] ->
  292. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  293. [Value] ->
  294. Res == Value+Incr
  295. end;
  296. postcondition(_S,{call,_,update_counter,[_Pid,_Key,_Incr]},Res) ->
  297. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  298. %% get_value
  299. postcondition(S,{call,_,get_value,[Pid,Key]},Res) ->
  300. case [ Value1 || #reg{pid=Pid1,key=Key1,value=Value1} <- S#state.regs
  301. , (Pid==Pid1 andalso Key==Key1) ] of
  302. [] ->
  303. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  304. [Value] ->
  305. Res == Value
  306. end;
  307. %% lookup_pid
  308. postcondition(S,{call,_,lookup_pid,[#key{class=Class}=Key]},Res)
  309. when Class == n; Class == a ->
  310. case [ Pid1 || #reg{pid=Pid1,key=Key1} <- S#state.regs
  311. , Key==Key1 ] of
  312. [] ->
  313. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  314. [Pid] ->
  315. Res == Pid
  316. end;
  317. postcondition(_S,{call,_,lookup_pid,[_Key]},Res) ->
  318. case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  319. %% lookup_pids
  320. postcondition(S,{call,_,lookup_pids,[#key{class=Class}=Key]},Res)
  321. when Class == n; Class == a; Class == c; Class == p ->
  322. Pids = [ Pid1 || #reg{pid=Pid1,key=Key1} <- S#state.regs
  323. , Key==Key1 ],
  324. lists:sort(Res) == lists:sort(Pids);
  325. %% postcondition(_S,{call,_,lookup_pids,[_Key]},Res) ->
  326. %% case Res of {'EXIT', {badarg, _}} -> true; _ -> false end;
  327. %% otherwise
  328. postcondition(_S,{call,_,_,_},_Res) ->
  329. false.
  330. %% property
  331. prop_gproc() ->
  332. ?FORALL(Cmds,commands(?MODULE),
  333. ?TRAPEXIT(
  334. begin
  335. ok = stop_app(),
  336. ok = start_app(),
  337. {H,S,Res} = run_commands(?MODULE,Cmds),
  338. kill_all_pids({H,S}),
  339. %% whenfail
  340. ?WHENFAIL(
  341. begin
  342. io:format("~nHISTORY:"),
  343. if
  344. length(H) < 1 ->
  345. io:format(" none~n");
  346. true ->
  347. CmdsH = eqc_statem:zip(Cmds,H),
  348. [ begin
  349. {Cmd,{State,Reply}} = lists:nth(N,CmdsH),
  350. io:format("~n #~p:~n\tCmd: ~p~n\tReply: ~p~n\tState: ~p~n",
  351. [N,Cmd,Reply,State])
  352. end
  353. || N <- lists:seq(1,length(CmdsH)) ]
  354. end,
  355. io:format("~nRESULT:~n\t~p~n",[Res]),
  356. io:format("~nSTATE:~n\t~p~n",[S])
  357. end,
  358. Res == ok)
  359. end)).
  360. %% helpers
  361. start_app() ->
  362. application:start(sasl),
  363. case application:start(gproc) of
  364. {error, {already_started,_}} ->
  365. stop_app(),
  366. ok = application:start(gproc);
  367. ok ->
  368. ok
  369. end.
  370. stop_app() ->
  371. case application:stop(gproc) of
  372. {error, {not_started,_}} ->
  373. ok;
  374. ok ->
  375. ok
  376. end.
  377. %% If using the scheduler... This code needs to run in a separate
  378. %% module, so it can be compiled without instrumentation.
  379. kill_all_pids(Pid) when is_pid(Pid) ->
  380. case is_process_alive(Pid) of
  381. true ->
  382. exit(Pid,kill);
  383. false ->
  384. ok
  385. end;
  386. kill_all_pids(Tup) when is_tuple(Tup) ->
  387. kill_all_pids(tuple_to_list(Tup));
  388. kill_all_pids([H|T]) ->
  389. kill_all_pids(H),
  390. kill_all_pids(T);
  391. kill_all_pids(_) ->
  392. ok.
  393. %% spawn
  394. spawn() ->
  395. spawn(fun() -> loop() end).
  396. loop() ->
  397. receive
  398. {From, Ref, F} ->
  399. From ! {Ref, catch F()},
  400. loop();
  401. stop -> ok
  402. end.
  403. %% kill
  404. kill(Pid) ->
  405. exit(Pid,foo),
  406. timer:sleep(10).
  407. %% where
  408. where(#key{class=Class,scope=Scope,name=Name}) ->
  409. catch gproc:where({Class,Scope,Name}).
  410. %% reg
  411. reg(Pid,#key{class=Class,scope=Scope,name=Name},Value) ->
  412. do(Pid, fun() -> catch gproc:reg({Class,Scope,Name},Value) end).
  413. %% unreg
  414. unreg(Pid,#key{class=Class,scope=Scope,name=Name}) ->
  415. do(Pid, fun() -> catch gproc:unreg({Class,Scope,Name}) end).
  416. %% set_value
  417. set_value(Pid,#key{class=Class,scope=Scope,name=Name},Value) ->
  418. do(Pid, fun() -> catch gproc:set_value({Class,Scope,Name},Value) end).
  419. %% update_counter
  420. update_counter(Pid, #key{class=Class,scope=Scope,name=Name},Incr) ->
  421. do(Pid, fun() -> catch gproc:update_counter({Class,Scope,Name},Incr) end).
  422. %% get_value
  423. get_value(Pid,#key{class=Class,scope=Scope,name=Name}) ->
  424. do(Pid, fun() -> catch gproc:get_value({Class,Scope,Name}) end).
  425. %% lookup_pid
  426. lookup_pid(#key{class=Class,scope=Scope,name=Name}) ->
  427. catch gproc:lookup_pid({Class,Scope,Name}).
  428. %% lookup_pids
  429. lookup_pids(#key{class=Class,scope=Scope,name=Name}) ->
  430. catch gproc:lookup_pids({Class,Scope,Name}).
  431. %% do
  432. do(Pid, F) ->
  433. Ref = erlang:monitor(process, Pid),
  434. Pid ! {self(), Ref, F},
  435. receive
  436. {'DOWN', Ref, process, Pid, Reason} ->
  437. {'EXIT', {'DOWN', Reason}};
  438. {Ref, Result} ->
  439. erlang:demonitor(Ref),
  440. Result
  441. after 3000 ->
  442. {'EXIT', timeout}
  443. end.