Permalink
Browse files

Add a test for async_threshold

  • Loading branch information...
1 parent 5b3fae6 commit acb8cf596fe43eb0afe0f0c2966b41bf61eeead2 @Vagabond Vagabond committed Mar 14, 2013
Showing with 54 additions and 3 deletions.
  1. +2 −2 src/lager_app.erl
  2. +1 −1 src/lager_backend_throttle.erl
  3. +51 −0 test/lager_test_backend.erl
View
@@ -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,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
@@ -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.

0 comments on commit acb8cf5

Please sign in to comment.