1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 1996-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20-module(log_mf_h_SUITE). 21 22-include_lib("common_test/include/ct.hrl"). 23-include_lib("kernel/include/file.hrl"). 24 25-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 26 init_per_group/2,end_per_group/2, test/1]). 27 28suite() -> [{ct_hooks,[ts_install_cth]}]. 29 30all() -> 31 [test]. 32 33groups() -> 34 []. 35 36init_per_suite(Config) -> 37 Config. 38 39end_per_suite(_Config) -> 40 ok. 41 42init_per_group(_GroupName, Config) -> 43 Config. 44 45end_per_group(_GroupName, Config) -> 46 Config. 47 48 49 50%%----------------------------------------------------------------- 51%% This is actually very basic tests, maybe we could test some more 52%% in the future... 53%%----------------------------------------------------------------- 54 55test(Config) when is_list(Config) -> 56 {ok, Pid} = gen_event:start_link(), 57 PrivDir = proplists:get_value(priv_dir, Config), 58 Log1 = PrivDir ++ "/log1", 59 ok = file:make_dir(Log1), 60 Args1 = log_mf_h:init(Log1, 500, 3), 61 gen_event:add_handler(Pid, log_mf_h, Args1), 62 generate(Pid, 200), 63 {ok, Files} = file:list_dir(Log1), 64 true = lists:member("1", Files), 65 true = lists:member("index", Files), 66 false = lists:member("2", Files), 67 generate(Pid, 2500), 68 %% The documentation doesn't guarantee that syncing one request 69 %% causes all previous ones to be finished too, but that seems to 70 %% be the case. We need to be sure that the files exist when we 71 %% look for them with 'list_dir'. 72 gen_event:sync_notify(Pid, "end"), 73 {ok, Files2} = file:list_dir(Log1), 74 true = lists:member("1", Files2), 75 true = lists:member("2", Files2), 76 true = lists:member("3", Files2), 77 false = lists:member("4", Files2), 78 true = lists:member("index", Files2), 79 {ok, #file_info{size=Size1,type=regular}} = file:read_file_info(Log1 ++ "/1"), 80 if Size1 > 500 -> ct:fail({too_big, Size1}); 81 true -> ok end, 82 {ok, #file_info{size=Size2,type=regular}} = file:read_file_info(Log1 ++ "/2"), 83 if Size2 > 500 -> ct:fail({too_big, Size2}); 84 true -> ok end, 85 {ok, #file_info{size=Size3,type=regular}} = file:read_file_info(Log1 ++ "/3"), 86 if Size3 > 500 -> ct:fail({too_big, Size3}); 87 true -> ok end, 88 gen_event:delete_handler(Pid, log_mf_h, []), 89 {ok, Index} = read_index_file(Log1), 90 gen_event:add_handler(Pid, log_mf_h, Args1), 91 X = if Index == 3 -> 1; true -> Index + 1 end, 92 {ok, X} = read_index_file(Log1). 93 94 95generate(Pid, Bytes) when Bytes > 32 -> 96 gen_event:notify(Pid, make_list(32, [])), 97 generate(Pid, Bytes - 32); 98generate(_, _) -> ok. 99 100make_list(0, Res) -> Res; 101make_list(N, Res) -> make_list(N-1, [67 | Res]). 102 103 104read_index_file(Dir) -> 105 case file:open(Dir ++ "/index", [read,raw]) of 106 {ok, Fd} -> 107 case catch file:read(Fd, 1) of 108 {ok, [Index]} -> {ok, Index}; 109 _ -> error 110 end; 111 _ -> error 112 end. 113