From b2ddcb26e22dd5245cb94d958c71651376a96f29 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 16 Aug 2019 15:11:38 +0800 Subject: [PATCH] Testcase logf (#2800) * Add test cases for logger formatter * Remove the unregister API --- src/emqx_cm.erl | 19 +------- test/emqx_logger_formatter_SUITE.erl | 73 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 test/emqx_logger_formatter_SUITE.erl diff --git a/src/emqx_cm.erl b/src/emqx_cm.erl index a4800e839..d23507d7c 100644 --- a/src/emqx_cm.erl +++ b/src/emqx_cm.erl @@ -28,8 +28,6 @@ -export([start_link/0]). -export([ register_channel/1 - , unregister_channel/1 - , unregister_channel/2 ]). -export([ get_chan_attrs/1 @@ -94,6 +92,7 @@ start_link() -> %%-------------------------------------------------------------------- %% @doc Register a channel. +%% Channel will be unregistered automatically when the channel process dies -spec(register_channel(emqx_types:client_id()) -> ok). register_channel(ClientId) when is_binary(ClientId) -> register_channel(ClientId, self()). @@ -106,17 +105,6 @@ register_channel(ClientId, ChanPid) -> ok = emqx_cm_registry:register_channel(Chan), cast({registered, Chan}). -%% @doc Unregister a channel. --spec(unregister_channel(emqx_types:client_id()) -> ok). -unregister_channel(ClientId) when is_binary(ClientId) -> - unregister_channel(ClientId, self()). - --spec(unregister_channel(emqx_types:client_id(), chan_pid()) -> ok). -unregister_channel(ClientId, ChanPid) -> - Chan = {ClientId, ChanPid}, - true = do_unregister_channel(Chan), - cast({unregistered, Chan}). - %% @private do_unregister_channel(Chan) -> ok = emqx_cm_registry:unregister_channel(Chan), @@ -285,10 +273,6 @@ handle_cast({registered, {ClientId, ChanPid}}, State = #{chan_pmon := PMon}) -> PMon1 = emqx_pmon:monitor(ChanPid, ClientId, PMon), {noreply, State#{chan_pmon := PMon1}}; -handle_cast({unregistered, {_ClientId, ChanPid}}, State = #{chan_pmon := PMon}) -> - PMon1 = emqx_pmon:demonitor(ChanPid, PMon), - {noreply, State#{chan_pmon := PMon1}}; - handle_cast(Msg, State) -> ?LOG(error, "Unexpected cast: ~p", [Msg]), {noreply, State}. @@ -325,4 +309,3 @@ update_stats({Tab, Stat, MaxStat}) -> undefined -> ok; Size -> emqx_stats:setstat(Stat, MaxStat, Size) end. - diff --git a/test/emqx_logger_formatter_SUITE.erl b/test/emqx_logger_formatter_SUITE.erl new file mode 100644 index 000000000..ea8180e9e --- /dev/null +++ b/test/emqx_logger_formatter_SUITE.erl @@ -0,0 +1,73 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2019 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqx_logger_formatter_SUITE). + +-compile(export_all). +-compile(nowarn_export_all). + +-include_lib("eunit/include/eunit.hrl"). + +all() -> emqx_ct:all(?MODULE). + +init_per_suite(Config) -> + Config. + +end_per_suite(_Config) -> + ok. + +t_chars_limit(_Config) -> + CharsLimit = 50, + LogFilename = "./t_chars_limit.log", + Formatter = {emqx_logger_formatter, + #{chars_limit => CharsLimit}}, + #{level := OrgLevel} = logger:get_primary_config(), + Config = + #{level => info, + config => #{ + type => halt, + file => LogFilename}, + formatter => Formatter}, + logger:add_handler(t_handler, logger_disk_log_h, Config), + logger:set_primary_config(level, info), + + logger:info("hello"), + logger:info(lists:duplicate(10, "hello")), + logger_disk_log_h:filesync(t_handler), + + ct:pal("content : ~p", [file:read_file(LogFilename)]), + [FirstLine, SecondLine] = readlines(LogFilename), + + ?assertMatch([_Date, _Time, _Level, "hello\n"], string:split(FirstLine, " ", all)), + ?assert(length(SecondLine) =< 50), + + logger:set_primary_config(level, OrgLevel). + + +readlines(FileName) -> + {ok, Device} = file:open(FileName, [read]), + try get_all_lines(Device) + after file:close(Device) + end. + +get_all_lines(Device) -> + get_all_lines(Device, []). +get_all_lines(Device, All) -> + case io:get_line(Device, "") of + eof -> + lists:reverse(All); + Line -> get_all_lines(Device, [Line | All]) + end. \ No newline at end of file