1// Copyright (c) 2012 - Cloud Instruments Co., Ltd. 2// 3// All rights reserved. 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are met: 7// 8// 1. Redistributions of source code must retain the above copyright notice, this 9// list of conditions and the following disclaimer. 10// 2. Redistributions in binary form must reproduce the above copyright notice, 11// this list of conditions and the following disclaimer in the documentation 12// and/or other materials provided with the distribution. 13// 14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 25package seelog 26 27import ( 28 "bufio" 29 "bytes" 30 "fmt" 31 "io" 32 "io/ioutil" 33 "strconv" 34 "testing" 35) 36 37func countSequencedRowsInFile(filePath string) (int64, error) { 38 bts, err := ioutil.ReadFile(filePath) 39 if err != nil { 40 return 0, err 41 } 42 43 bufReader := bufio.NewReader(bytes.NewBuffer(bts)) 44 45 var gotCounter int64 46 for { 47 line, _, bufErr := bufReader.ReadLine() 48 if bufErr != nil && bufErr != io.EOF { 49 return 0, bufErr 50 } 51 52 lineString := string(line) 53 if lineString == "" { 54 break 55 } 56 57 intVal, atoiErr := strconv.ParseInt(lineString, 10, 64) 58 if atoiErr != nil { 59 return 0, atoiErr 60 } 61 62 if intVal != gotCounter { 63 return 0, fmt.Errorf("wrong order: %d Expected: %d\n", intVal, gotCounter) 64 } 65 66 gotCounter++ 67 } 68 69 return gotCounter, nil 70} 71 72func Test_Adaptive(t *testing.T) { 73 fileName := "beh_test_adaptive.log" 74 count := 100 75 76 Current.Close() 77 78 if e := tryRemoveFile(fileName); e != nil { 79 t.Error(e) 80 return 81 } 82 defer func() { 83 if e := tryRemoveFile(fileName); e != nil { 84 t.Error(e) 85 } 86 }() 87 88 testConfig := ` 89<seelog type="adaptive" mininterval="1000" maxinterval="1000000" critmsgcount="100"> 90 <outputs formatid="msg"> 91 <file path="` + fileName + `"/> 92 </outputs> 93 <formats> 94 <format id="msg" format="%Msg%n"/> 95 </formats> 96</seelog>` 97 98 logger, _ := LoggerFromConfigAsString(testConfig) 99 100 err := ReplaceLogger(logger) 101 if err != nil { 102 t.Error(err) 103 return 104 } 105 106 for i := 0; i < count; i++ { 107 Trace(strconv.Itoa(i)) 108 } 109 110 Flush() 111 112 gotCount, err := countSequencedRowsInFile(fileName) 113 if err != nil { 114 t.Error(err) 115 return 116 } 117 118 if int64(count) != gotCount { 119 t.Errorf("wrong count of log messages. Expected: %v, got: %v.", count, gotCount) 120 return 121 } 122 123 Current.Close() 124} 125