xmtop/logging/logging_test.go

54 lines
848 B
Go
Raw Normal View History

2020-02-16 04:27:31 +08:00
package logging
import (
"io/ioutil"
//"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
2020-04-24 03:07:08 +08:00
"github.com/xxxserxxx/gotop/v4"
2020-02-16 04:27:31 +08:00
)
func TestLogging(t *testing.T) {
tdn := "testdir"
path, err := filepath.Abs(tdn)
defer os.RemoveAll(path)
c := gotop.Config{
MaxLogSize: 300,
}
wc, err := New(c)
assert.NoError(t, err)
if err != nil {
return
}
defer wc.Close()
ds := make([]byte, 100)
2020-04-28 09:33:41 +08:00
for i := range ds {
2020-02-16 04:27:31 +08:00
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))
}
}