Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Circumventing race condition and removing io:format/2
  • Loading branch information
Gianfranco committed Jul 5, 2011
1 parent b2776ed commit 2f4c8da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/audit_collector.erl
Expand Up @@ -47,19 +47,15 @@ handle_call({add_tracing_on,{process,Options}},_,State) ->
erlang:trace(new,true,Flags),
{reply,ok,State};
handle_call({review,{process,Options}},_,State) ->
OPFlags = option_to_flag(Options),
io:format(user,"Audit Collector :: review, {process,OPFlags} ---> ~p~n",[OPFlags]),
io:format(user,"Audit Collector :: State log ---> ~p~n",[State#state.log]),
Reply = make_log(OPFlags,lists:reverse(State#state.log)),
io:format(user,"Reply == ~p~n",[Reply]),
OPFlags = option_to_flag(Options),
Reply = make_log(OPFlags,lists:reverse(State#state.log)),
{reply,Reply,State}.


handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(X, State) ->
io:format(user,"Audit Collector :: handle_info/2 -> ~p~n",[X]),
handle_info(X, State) ->
{noreply, State#state{log = [X|State#state.log]}}.

terminate(_Reason, _State) ->
Expand All @@ -78,7 +74,6 @@ make_log([],_) -> [];
make_log([send|R],History) ->
lists:foldl(
fun({trace_ts,P,send,Msg,To,_},Acc) when is_pid(P)->
io:format(user,"Foldl SEND ~p~n",[{trace_ts,P,send,Msg,To}]),
case Msg of
{io_request,P,To,_} ->
Acc;
Expand All @@ -90,7 +85,6 @@ io:format(user,"Foldl SEND ~p~n",[{trace_ts,P,send,Msg,To}]),
make_log(['receive'|R],History) ->
lists:foldl(
fun({trace_ts,P,'receive',Msg,_},Acc) ->
io:format(user,"Foldl RECEIVE ~p~n",[{trace_ts,P,'receive',Msg}]),
case Msg of
{io_reply,P2,_} when is_pid(P2)->
Acc;
Expand All @@ -99,4 +93,3 @@ io:format(user,"Foldl RECEIVE ~p~n",[{trace_ts,P,'receive',Msg}]),
end;
(_,Acc) -> Acc
end,[],History)++make_log(R,History).

5 changes: 3 additions & 2 deletions test/audit_collector_tests.erl
Expand Up @@ -4,7 +4,7 @@
audit_collector_test_() ->
{foreach,
fun() -> audit_collector:start_link() end,
fun(P) -> audit_collector:stop() end,
fun(_) -> audit_collector:stop() end,
[fun audit_collector_process/0
]}.

Expand All @@ -15,7 +15,8 @@ audit_collector_process() ->
Receiver = spawn_link(fun() -> receive die -> ok end end),
receive {i_sent_this,Sender} -> ok end,
Receiver ! die,
timer:sleep(10),
?assertMatch(
[{send,Sender,{i_sent_this,Sent}},
[{send,Sender,{i_sent_this,Sender}},
{'receive',Receiver,die}],
audit_collector:review(process,[send,'receive'])).

0 comments on commit 2f4c8da

Please sign in to comment.