Permalink
Browse files

Fix "tracing to a dedicated file should work, second try"

* Support new async behavior of the lager:dispatch_log/8. Syncing the
  test by the log file size growth after the event being logged.

* Add test/wait.erl module with the 'wait-for' primitive provided. It
  should be used for syncing tests with some specific-to-backend
  quality changes. Because the logging behaviour is now asynchronous
  and there is no warranty that the event will be logged by a backend
  after the log event provider process returns to its execution.

* Add filesize_checker/1 to lager_file_backend: a handy generator of
  the predicator that can be used in wait:wait/2,3. Those predicators
  checks log file size growth which is the lager_file_backend's
  specific quality, and allow to sync tests.
  • Loading branch information...
1 parent de380c8 commit 4542f789b378ed88c21ba3e7b5632a3d670a6a3e @aleksandr-vin committed Nov 29, 2012
Showing with 73 additions and 9 deletions.
  1. +28 −9 src/lager_file_backend.erl
  2. +45 −0 test/wait.erl
@@ -383,18 +383,37 @@ filesystem_test_() ->
?assertMatch([_, _, "[error]", _, "Test message\n"], re:split(Bin3, " ", [{return, list}, {parts, 5}]))
end
},
- {"tracing to a dedicated file should work",
- fun() ->
- file:delete("foo.log"),
- {ok, _} = lager:trace_file("foo.log", [{module, ?MODULE}]),
- lager:error("Test message"),
- {ok, Bin3} = file:read_file("foo.log"),
- ?assertMatch([_, _, "[error]", _, "Test message\n"], re:split(Bin3, " ", [{return, list}, {parts, 5}]))
- end
- }
+ {"tracing to a dedicated file should work, second try",
+ fun() ->
+ File = "foo.log",
+ file:delete(File),
+ {ok, _} = lager:trace_file(File, [{module, ?MODULE}]),
+ CheckerFun = filesize_checker(File),
+ lager:error("Test message"),
+ ok = wait:wait(CheckerFun, 5000, 100),
+ {ok, Bin3} = file:read_file(File),
+ ?assertMatch([_, _, "[error]", _, "Test message\n"], re:split(Bin3, " ", [{return, list}, {parts, 5}]))
+ end
+ }
]
}.
+%% @doc Return a zero arity lambda function which checks File size
+%% growth to the time when this lambda was created.
+-spec filesize_checker(File :: file:name()) -> CheckerFun when
+ CheckerFun :: fun(() -> true | false).
+filesize_checker(File) ->
+ Fun =
+ fun () ->
+ {ok, FileInfo} =
+ file:read_file_info(File),
+ FileInfo#file_info.size
+ end,
+ CurSize = Fun(),
+ fun () ->
+ CurSize < Fun()
+ end.
+
formatting_test_() ->
{foreach,
fun() ->
View
@@ -0,0 +1,45 @@
+%% @doc Provides 'wait-for' primitive. It should be used for syncing
+%% tests with some specific-to-backend quality changes. Because the
+%% logging behaviour is now asynchronous and there is no warranty that
+%% the event will be logged by a backend after the log event provider
+%% process returns to its execution.
+-module(wait).
+
+%% API
+-export([wait/2, wait/3]).
+
+%% private
+-export([waiter/3]).
+
+-type predicate() :: fun(() -> true | false).
+
+-spec wait(Pred :: predicate(),
+ Timeout :: timer:time()) -> ok | 'timed-out'.
+wait(Pred, Timeout) ->
+ wait(Pred, Timeout, 500).
+
+-spec wait(Pred :: predicate(),
+ Timeout :: timer:time(),
+ Interval :: integer()) -> ok | 'timed-out'.
+wait(Pred, Timeout, Interval) when is_integer(Timeout) andalso Timeout > 0 ->
+ Ref = make_ref(),
+ {ok, TRef} = timer:apply_interval(Interval, ?MODULE, waiter, [self(), Ref, Pred]),
+ Result =
+ receive
+ {waiter, Ref} ->
+ ok
+ after Timeout ->
+ 'timed-out'
+ end,
+ timer:cancel(TRef),
+ Result.
+
+-spec waiter(Pid :: pid(), Ref :: reference(), Pred :: predicate()) -> true | false.
+waiter(Pid, Ref, Pred) ->
+ case Pred() of
+ true ->
+ Pid ! {waiter, Ref},
+ true;
+ false ->
+ false
+ end.

0 comments on commit 4542f78

Please sign in to comment.