Skip to content
Browse files

Added test for process creation checks, only works for second generat…

…ion though
  • Loading branch information...
1 parent 58c8ebe commit 50cefbdf8e9dc8402d49045b29cbb076bda7c6fa Gianfranco committed Jul 6, 2011
Showing with 31 additions and 10 deletions.
  1. +16 −8 src/audit_collector.erl
  2. +15 −2 test/audit_collector_tests.erl
View
24 src/audit_collector.erl
@@ -24,11 +24,11 @@ start_link() ->
stop() ->
gen_server:call(?MODULE,stop).
--spec(audit(process,[send|'receive']) -> ok).
+-spec(audit(process,[send|'receive'|start]) -> ok).
audit(process,Options) ->
gen_server:call(?MODULE,{add_tracing_on,{process,Options}}).
--spec(review(process,[send|'receive']) -> [tuple()]).
+-spec(review(process,[send|'receive'|start]) -> [tuple()]).
review(process,Options) ->
gen_server:call(?MODULE,{review,{process,Options}}).
@@ -42,13 +42,12 @@ init([]) ->
handle_call(stop,_, State) ->
{stop,normal,ok,State};
handle_call({add_tracing_on,{process,Options}},_,State) ->
- OPFlags = option_to_flag(Options),
+ OPFlags = options_to_trace_flag(Options),
Flags = [timestamp,{tracer,self()}|OPFlags],
erlang:trace(new,true,Flags),
{reply,ok,State};
handle_call({review,{process,Options}},_,State) ->
- OPFlags = option_to_flag(Options),
- Reply = make_log(OPFlags,lists:reverse(State#state.log)),
+ Reply = make_log(Options,lists:reverse(State#state.log)),
{reply,Reply,State}.
@@ -67,8 +66,11 @@ code_change(_OldVsn, State, _Extra) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
-option_to_flag(X) ->
- X.
+options_to_trace_flag([start|R]) ->
+ [procs,set_on_spawn|options_to_trace_flag(R)];
+options_to_trace_flag([X|R]) ->
+ [X|options_to_trace_flag(R)];
+options_to_trace_flag([]) -> [].
make_log([],_) -> [];
make_log([send|R],History) ->
@@ -92,4 +94,10 @@ make_log(['receive'|R],History) ->
Acc++[{'receive',P,Msg}]
end;
(_,Acc) -> Acc
- end,[],History)++make_log(R,History).
+ end,[],History)++make_log(R,History);
+make_log([start|R],History) ->
+ lists:foldl(
+ fun({trace_ts,P,spawn,P2,{_M,_F,_A},_},Acc) ->
+ Acc++[{started,P,P2}];
+ (_,Acc) -> Acc
+ end,[],History)++make_log(R,History).
View
17 test/audit_collector_tests.erl
@@ -5,8 +5,10 @@ audit_collector_test_() ->
{foreach,
fun() -> audit_collector:start_link() end,
fun(_) -> audit_collector:stop() end,
- [fun audit_collector_process_send/0,
- fun audit_collector_process_receive/0
+ [
+ fun audit_collector_process_send/0,
+ fun audit_collector_process_receive/0,
+ fun audit_collector_process_started/0
]}.
audit_collector_process_send() ->
@@ -28,3 +30,14 @@ audit_collector_process_receive() ->
[{'receive',Receiver,die}],
audit_collector:review(process,['receive'])).
+audit_collector_process_started() ->
+ audit_collector:audit(process,[start]),
+ Self = self(),
+ Started = spawn(fun() -> Startee = spawn(lists,reverse,[[1,2,3]]),
+ Self ! {started,Startee} end),
+ {started,Startee} = receive X -> X end,
+ timer:sleep(10),
+ ?assertMatch(
+ [{started,Started,Startee}],
+ audit_collector:review(process,[start])).
+

0 comments on commit 50cefbd

Please sign in to comment.
Something went wrong with that request. Please try again.