Permalink
Browse files

Add some more record printing tests and fix a bug

  • Loading branch information...
1 parent 9bd0a41 commit f566318bdc1753eaf2e6ea0e0d812eb9f620953f @Vagabond Vagabond committed Dec 12, 2012
Showing with 47 additions and 1 deletion.
  1. +1 −1 src/lager.erl
  2. +46 −0 test/lager_test_backend.erl
View
@@ -282,7 +282,7 @@ pr(Record, Module) when is_tuple(Record), is_atom(element(1, Record)) ->
(_) ->
false
end, Records) of
- false ->
+ [] ->
Record;
[{RecordName, RecordFields}|_] ->
{'$lager_record', RecordName,
@@ -55,6 +55,10 @@ handle_call(print_state, State) ->
spawn(fun() -> lager:info("State ~p", [lager:pr(State, ?MODULE)]) end),
timer:sleep(100),
{ok, ok, State};
+handle_call(print_bad_state, State) ->
+ spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, ?MODULE)]) end),
+ timer:sleep(100),
+ {ok, ok, State};
handle_call(_Request, State) ->
{ok, ok, State}.
@@ -96,6 +100,9 @@ flush() ->
print_state() ->
gen_event:call(lager_event, ?MODULE, print_state).
+print_bad_state() ->
+ gen_event:call(lager_event, ?MODULE, print_bad_state).
+
has_line_numbers() ->
%% are we R15 or greater
Rel = erlang:system_info(otp_release),
@@ -236,6 +243,45 @@ lager_test_() ->
?assertEqual("State #state{level=6,buffer=[],ignored=[]}", lists:flatten(Message)),
ok
end
+ },
+ {"record printing fails gracefully",
+ fun() ->
+ print_bad_state(),
+ {Level, _Time, Message, _Metadata} = pop(),
+ ?assertMatch(Level, lager_util:level_to_num(info)),
+ ?assertEqual("State {state,1}", lists:flatten(Message)),
+ ok
+ end
+ },
+ {"record printing fails gracefully when no lager_record attribute",
+ fun() ->
+ spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, lager)]) end),
+ timer:sleep(100),
+ {Level, _Time, Message, _Metadata} = pop(),
+ ?assertMatch(Level, lager_util:level_to_num(info)),
+ ?assertEqual("State {state,1}", lists:flatten(Message)),
+ ok
+ end
+ },
+ {"record printing fails gracefully when input is not a tuple",
+ fun() ->
+ spawn(fun() -> lager:info("State ~p", [lager:pr(ok, lager)]) end),
+ timer:sleep(100),
+ {Level, _Time, Message, _Metadata} = pop(),
+ ?assertMatch(Level, lager_util:level_to_num(info)),
+ ?assertEqual("State ok", lists:flatten(Message)),
+ ok
+ end
+ },
+ {"record printing fails gracefully when module is invalid",
+ fun() ->
+ spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, not_a_module)]) end),
+ timer:sleep(100),
+ {Level, _Time, Message, _Metadata} = pop(),
+ ?assertMatch(Level, lager_util:level_to_num(info)),
+ ?assertEqual("State {state,1}", lists:flatten(Message)),
+ ok
+ end
}
]
}.

0 comments on commit f566318

Please sign in to comment.