Removed a lot of useless code and tuples, because this is a simpler case than eunit

This commit is contained in:
Joe DeVivo 2012-09-07 08:43:56 -07:00
parent afea5866d4
commit 00693399c3
3 changed files with 57 additions and 74 deletions

View File

@ -1,101 +1,83 @@
-module(riak_test_group_leader).
-export([new_group_leader/1, group_leader_process/1]).
-export([new_group_leader/1, group_leader_loop/1]).
% @doc stolen without shame from eunit_proc.erl
% @doc spawns the new group leader
new_group_leader(Runner) ->
spawn_link(?MODULE, group_leader_process, [Runner]).
spawn_link(?MODULE, group_leader_loop, [Runner]).
% @doc stolen without shame from eunit_proc.erl
group_leader_process(Runner) ->
group_leader_loop(Runner, infinity, []).
% @doc stolen without shame from eunit_proc.erl
group_leader_loop(Runner, Wait, Buf) ->
% @doc listens for io_requests, and pipes them into lager
group_leader_loop(Runner) ->
receive
{io_request, From, ReplyAs, Req} ->
lager:notice("io_request"),
P = process_flag(priority, normal),
%% run this part under normal priority always
Buf1 = io_request(From, ReplyAs, Req, Buf),
io_request(From, ReplyAs, Req),
process_flag(priority, P),
group_leader_loop(Runner, Wait, Buf1);
stop ->
lager:notice("stop"),
%% quitting time: make a minimal pause, go low on priority,
%% set receive-timeout to zero and schedule out again
receive after 2 -> ok end,
process_flag(priority, low),
group_leader_loop(Runner, 0, Buf);
group_leader_loop(Runner);
_ ->
%% discard any other messages
group_leader_loop(Runner, Wait, Buf)
after Wait ->
lager:notice("after after"),
%% no more messages and nothing to wait for; we ought to
%% have collected all immediately pending output now
process_flag(priority, normal),
Runner ! {self(), buffer_to_binary(Buf)}
group_leader_loop(Runner)
end.
% @doc stolen without shame from eunit_proc.erl
buffer_to_binary([B]) when is_binary(B) -> B; % avoid unnecessary copying
buffer_to_binary(Buf) -> list_to_binary(lists:reverse(Buf)).
%% Implementation of buffering I/O for group leader processes. (Note that
%% each batch of characters is just pushed on the buffer, so it needs to
%% be reversed when it is flushed.)
io_request(From, ReplyAs, Req, Buf) ->
{Reply, Buf1} = io_request(Req, Buf),
%% Processes an io_request and sends a reply
io_request(From, ReplyAs, Req) ->
Reply = io_request(Req),
io_reply(From, ReplyAs, Reply),
Buf1.
ok.
%% sends a reply back to the sending process
io_reply(From, ReplyAs, Reply) ->
From ! {io_reply, ReplyAs, Reply}.
io_request({put_chars, Chars}, _Buf) ->
%% If we're processing io:put_chars, Chars shows up as binary
io_request({put_chars, Chars}) when is_binary(Chars) ->
io_request({put_chars, binary_to_list(Chars)});
io_request({put_chars, Chars}) ->
log_chars(Chars),
{ok, []};
io_request({put_chars, M, F, As}, Buf) ->
ok;
io_request({put_chars, M, F, As}) ->
try apply(M, F, As) of
Chars ->
log_chars(Chars),
{ok, []}
ok
catch
C:T -> {{error, {C,T,erlang:get_stacktrace()}}, Buf}
C:T -> {error, {C,T,erlang:get_stacktrace()}}
end;
io_request({put_chars, _Enc, Chars}, Buf) ->
io_request({put_chars, Chars}, Buf);
io_request({put_chars, _Enc, Mod, Func, Args}, Buf) ->
io_request({put_chars, Mod, Func, Args}, Buf);
io_request({get_chars, _Enc, _Prompt, _N}, Buf) ->
{eof, Buf};
io_request({get_chars, _Prompt, _N}, Buf) ->
{eof, Buf};
io_request({get_line, _Prompt}, Buf) ->
{eof, Buf};
io_request({get_line, _Enc, _Prompt}, Buf) ->
{eof, Buf};
io_request({get_until, _Prompt, _M, _F, _As}, Buf) ->
{eof, Buf};
io_request({setopts, _Opts}, Buf) ->
{ok, Buf};
io_request(getopts, Buf) ->
{error, {error, enotsup}, Buf};
io_request({get_geometry,columns}, Buf) ->
{error, {error, enotsup}, Buf};
io_request({get_geometry,rows}, Buf) ->
{error, {error, enotsup}, Buf};
io_request({requests, Reqs}, Buf) ->
io_requests(Reqs, {ok, Buf});
io_request(_, Buf) ->
{{error, request}, Buf}.
io_request({put_chars, _Enc, Chars}) ->
io_request({put_chars, Chars});
io_request({put_chars, _Enc, Mod, Func, Args}) ->
io_request({put_chars, Mod, Func, Args});
%% The rest of these functions just handle expected messages from
%% the io module. They're mostly i, but we only care about o.
io_request({get_chars, _Enc, _Prompt, _N}) ->
eof;
io_request({get_chars, _Prompt, _N}) ->
eof;
io_request({get_line, _Prompt}) ->
eof;
io_request({get_line, _Enc, _Prompt}) ->
eof;
io_request({get_until, _Prompt, _M, _F, _As}) ->
eof;
io_request({setopts, _Opts}) ->
ok;
io_request(getopts) ->
{error, enotsup};
io_request({get_geometry,columns}) ->
{error, enotsup};
io_request({get_geometry,rows}) ->
{error, enotsup};
io_request({requests, Reqs}) ->
io_requests(Reqs, ok);
io_request(_) ->
{error, request}.
io_requests([R | Rs], {ok, Buf}) ->
io_requests(Rs, io_request(R, Buf));
io_requests([R | Rs], ok) ->
io_requests(Rs, io_request(R));
io_requests(_, Result) ->
Result.
%% If we get multiple lines, we'll split them up for lager to maximize the prettiness.
log_chars(Chars) ->
[lager:debug("~s", [Line]) || Line <- string:tokens(lists:flatten(Chars), "\n")].

View File

@ -44,12 +44,11 @@ stop_lager_backend() ->
gen_event:delete_handler(lager_event, lager_file_backend, []),
gen_event:delete_handler(lager_event, riak_test_lager_backend, []).
%% @doc does some group_leader swapping, in the style of EUnit.
%% does some group_leader swapping, in the style of EUnit.
execute(TestModule) ->
process_flag(trap_exit, true),
Runner = self(),
GroupLeader = group_leader(),
NewGroupLeader = riak_test_group_leader:new_group_leader(Runner),
NewGroupLeader = riak_test_group_leader:new_group_leader(self()),
group_leader(NewGroupLeader, self()),
_Pid = spawn_link(TestModule, confirm, []),

View File

@ -3,5 +3,7 @@
-export([confirm/0]).
confirm() ->
io:format("This should be a lager message!"),
io:put_chars("This is an io:put_chars/1 call"),
io:format("This is an io:format/1 call"),
io:format("This is an io:format/~w call", [2]),
lager:info("This is a lager message").