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