xmtop/logging/logging_test.go
2020-02-15 14:27:31 -06:00

56 lines
896 B
Go

package logging
import (
"io/ioutil"
//"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xxxserxxx/gotop"
)
func TestLogging(t *testing.T) {
tdn := "testdir"
path, err := filepath.Abs(tdn)
defer os.RemoveAll(path)
c := gotop.Config{
MaxLogSize: 300,
LogDir: path,
LogFile: "errors.log",
}
wc, err := New(c)
assert.NoError(t, err)
if err != nil {
return
}
defer wc.Close()
ds := make([]byte, 100)
for i, _ := range ds {
ds[i] = 'x'
}
// Base case -- empty log file
td, err := ioutil.ReadDir(path)
assert.NoError(t, err)
if err != nil {
return
}
assert.Equal(t, 1, len(td))
for i := 1; i < 6; i++ {
wc.Write(ds)
wc.Write(ds)
wc.Write(ds)
wc.Write([]byte{'\n'}) // max... + 1
td, err = ioutil.ReadDir(path)
assert.NoError(t, err)
k := i
if k > 4 {
k = 4
}
assert.Equal(t, k, len(td))
}
}