瀏覽代碼

Add a test for async_threshold

pull/117/head
Andrew Thompson 12 年之前
父節點
當前提交
acb8cf596f
共有 3 個檔案被更改,包括 54 行新增3 行删除
  1. +2
    -2
      src/lager_app.erl
  2. +1
    -1
      src/lager_backend_throttle.erl
  3. +51
    -0
      test/lager_test_backend.erl

+ 2
- 2
src/lager_app.erl 查看文件

@ -41,8 +41,8 @@ start(_StartType, _StartArgs) ->
ok;
{ok, Threshold} when is_integer(Threshold), Threshold >= 0 ->
_ = supervisor:start_child(lager_handler_watcher_sup, [lager_event, lager_backend_throttle, Threshold]);
{ok, BadVal} ->
error_logger:error_msg("Invalid value for 'async_threshold': ~p~n", [BadVal]),
{ok, BadThreshold} ->
error_logger:error_msg("Invalid value for 'async_threshold': ~p~n", [BadThreshold]),
throw({error, bad_config})
end,

+ 1
- 1
src/lager_backend_throttle.erl 查看文件

@ -1,4 +1,4 @@
%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
%% Copyright (c) 2011-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file

+ 51
- 0
test/lager_test_backend.erl 查看文件

@ -917,6 +917,57 @@ safe_format_test() ->
?assertEqual("FORMAT ERROR: \"~p ~p ~p\" [foo,bar]", lists:flatten(lager:safe_format("~p ~p ~p", [foo, bar], 1024))),
ok.
async_threshold_test_() ->
{foreach,
fun() ->
error_logger:tty(false),
application:load(lager),
application:set_env(lager, error_logger_redirect, false),
application:set_env(lager, async_threshold, 10),
application:set_env(lager, handlers, [{?MODULE, info}]),
application:start(lager)
end,
fun(_) ->
application:unset_env(lager, async_threshold),
application:stop(lager),
error_logger:tty(true)
end,
[
{"async threshold works",
fun() ->
%% we start out async
?assertEqual(true, lager_config:get(async)),
%% put a ton of things in the queue
Workers = [spawn_monitor(fun() -> [lager:info("hello world") || _ <- lists:seq(1, 1000)] end) || _ <- lists:seq(1, 10)],
%% serialize on mailbox
_ = gen_event:which_handlers(lager_event),
%% there should be a ton of outstanding messages now, so async is false
?assertEqual(false, lager_config:get(async)),
%% wait for all the workers to return, meaning that all the messages have been logged (since we're in sync mode)
collect_workers(Workers),
%% serialize ont the mailbox again
_ = gen_event:which_handlers(lager_event),
%% just in case...
timer:sleep(100),
%% async is true again now that the mailbox has drained
?assertEqual(true, lager_config:get(async)),
ok
end
}
]
}.
collect_workers([]) ->
ok;
collect_workers(Workers) ->
receive
{'DOWN', Ref, _, _, _} ->
collect_workers(lists:keydelete(Ref, 2, Workers))
end.
-endif.

Loading…
取消
儲存