EUnit tests
This commit is contained in:
parent
90314e4080
commit
de8d2cc3c4
9
Makefile
9
Makefile
|
@ -2,12 +2,21 @@ REBAR=rebar3
|
|||
|
||||
all: release
|
||||
|
||||
prod:
|
||||
${REBAR} as prod tar
|
||||
|
||||
release:
|
||||
${REBAR} release
|
||||
|
||||
compile:
|
||||
${REBAR} compile
|
||||
|
||||
code:
|
||||
${REBAR} dialyzer
|
||||
|
||||
test:
|
||||
${REBAR} eunit
|
||||
|
||||
doc:
|
||||
${REBAR} edoc
|
||||
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
|
||||
-behaviour(gen_fsm).
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-endif.
|
||||
|
||||
%% API
|
||||
-export([start_link/2]).
|
||||
|
||||
|
@ -19,8 +23,6 @@
|
|||
-export([state_off/2, state_on/2]).
|
||||
|
||||
-define(SERVER, ?MODULE).
|
||||
-define(DEFAULT_TIMER_TIME, 1000).
|
||||
-define(DEFAULT_SAFE_TIMER_TIME, 30000).
|
||||
|
||||
-record(log, {file, error_regex}).
|
||||
-record(state_off, {log}).
|
||||
|
@ -80,7 +82,7 @@ init([File, ErrorRegex]) ->
|
|||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
state_off({log_line, Text}, State = #state_off{log = Log}) ->
|
||||
case isError(Text, Log#log.error_regex) of
|
||||
case is_error(Text, Log#log.error_regex) of
|
||||
true ->
|
||||
Timeout = gathering_time(),
|
||||
MaxTimeout = max_gathering_time(),
|
||||
|
@ -110,7 +112,7 @@ state_off({log_line, Text}, State = #state_off{log = Log}) ->
|
|||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until, max_until = MaxUntil}) ->
|
||||
case isError(Text, Log#log.error_regex) of
|
||||
case is_error(Text, Log#log.error_regex) of
|
||||
true ->
|
||||
Timeout = gathering_time(),
|
||||
Now = to_milliseconds(os:timestamp()),
|
||||
|
@ -212,7 +214,7 @@ code_change(_OldVsn, StateName, State, _Extra) ->
|
|||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%%===================================================================
|
||||
isError(Text, ErrorRegex) ->
|
||||
is_error(Text, ErrorRegex) ->
|
||||
case re:run(Text, ErrorRegex) of
|
||||
{match, _} ->
|
||||
true;
|
||||
|
@ -221,12 +223,26 @@ isError(Text, ErrorRegex) ->
|
|||
end.
|
||||
|
||||
gathering_time() ->
|
||||
{ok, ProcessingConfig} = application:get_env(log_monitor, general),
|
||||
proplists:get_value(gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).
|
||||
{ok, GeneralConfig} = application:get_env(log_monitor, general),
|
||||
proplists:get_value(gathering_time, GeneralConfig).
|
||||
|
||||
max_gathering_time() ->
|
||||
{ok, ProcessingConfig} = application:get_env(log_monitor, general),
|
||||
proplists:get_value(max_gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).
|
||||
{ok, GeneralConfig} = application:get_env(log_monitor, general),
|
||||
proplists:get_value(max_gathering_time, GeneralConfig).
|
||||
|
||||
to_milliseconds({Me, S, Mu}) ->
|
||||
(Me * 1000 * 1000 * 1000) + (S * 1000) + (Mu div 1000).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Tests
|
||||
%%%===================================================================
|
||||
-ifdef(TEST).
|
||||
|
||||
start_test() ->
|
||||
{ok, _Pid} = start_link("/tmp/test.log", "ERROR").
|
||||
|
||||
is_error_test() ->
|
||||
?assert(not is_error("2017-01-01 MyApp - WARN: abc", "ERROR")),
|
||||
?assert(is_error("2017-01-01 MyApp - ERROR: abc", "ERROR")).
|
||||
|
||||
-endif.
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
-behaviour(gen_server).
|
||||
|
||||
-include_lib("mnesia_tables.hrl").
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-endif.
|
||||
|
||||
%% API
|
||||
-export([start_link/0]).
|
||||
|
@ -219,5 +222,27 @@ email_subject(Text, File) ->
|
|||
re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]).
|
||||
|
||||
file_name_from_path(File) ->
|
||||
{match, [_, Basename]} = re:run(File, "^.*\/(.*)\\..*$", [{capture, all, list}]),
|
||||
{match, [_, _, Basename]} = re:run(File, "^(.*\/)?(.*)\\..*$", [{capture, all, list}]),
|
||||
Basename.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Tests
|
||||
%%%===================================================================
|
||||
-ifdef(TEST).
|
||||
|
||||
start_test() ->
|
||||
{ok, _Pid} = start_link().
|
||||
|
||||
email_subject_test() ->
|
||||
?assertEqual("Error", email_subject("Error", "/var/log/myApp.log")),
|
||||
?assertEqual(
|
||||
"[myApp] Error: /var/log/myApp.log",
|
||||
email_subject("[%F] Error: %f", "/var/log/myApp.log")
|
||||
).
|
||||
|
||||
file_name_from_path_test() ->
|
||||
?assertEqual("myApp", file_name_from_path("myApp.log")),
|
||||
?assertEqual("myApp", file_name_from_path("./myApp.log")),
|
||||
?assertEqual("myApp", file_name_from_path("/var/log/myApp.log")).
|
||||
|
||||
-endif.
|
||||
|
|
|
@ -8,6 +8,10 @@
|
|||
%%%-------------------------------------------------------------------
|
||||
-module(utils).
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-endif.
|
||||
|
||||
%% API
|
||||
-export([flatten/1]).
|
||||
|
||||
|
@ -31,3 +35,15 @@ flatten([],Acc) -> Acc;
|
|||
flatten([[]|T],Acc) -> flatten(T, Acc);
|
||||
flatten([[_|_]=H|T],Acc) -> flatten(T, flatten(H,Acc));
|
||||
flatten([H|T],Acc) -> flatten(T,Acc++[H]) .
|
||||
|
||||
%%%===================================================================
|
||||
%%% Tests
|
||||
%%%===================================================================
|
||||
-ifdef(TEST).
|
||||
|
||||
flatten_test() ->
|
||||
?assertEqual([], flatten([])),
|
||||
?assertEqual([1,2,3], flatten([1,2,3])),
|
||||
?assertEqual([1,2,3], flatten([[1],[2,3]])).
|
||||
|
||||
-endif.
|
||||
|
|
Loading…
Reference in New Issue
Block a user