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