2020-05-20 11:47:42 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-06-30 18:09:26 +03:00
|
|
|
"strconv"
|
2020-05-20 11:47:42 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-06-30 18:09:26 +03:00
|
|
|
"time"
|
2024-01-30 19:16:01 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-20 11:47:42 +08:00
|
|
|
)
|
|
|
|
|
2023-06-30 18:09:26 +03:00
|
|
|
func TestWriterHourly(t *testing.T) {
|
2024-01-30 19:16:01 +08:00
|
|
|
w := NewAsyncFileWriter("./hello.log", 100, 1, 1)
|
2020-05-20 11:47:42 +08:00
|
|
|
w.Start()
|
|
|
|
w.Write([]byte("hello\n"))
|
|
|
|
w.Write([]byte("world\n"))
|
|
|
|
w.Stop()
|
2023-06-30 18:09:26 +03:00
|
|
|
files, _ := os.ReadDir("./")
|
2020-05-20 11:47:42 +08:00
|
|
|
for _, f := range files {
|
|
|
|
fn := f.Name()
|
|
|
|
if strings.HasPrefix(fn, "hello") {
|
|
|
|
t.Log(fn)
|
2023-06-30 18:09:26 +03:00
|
|
|
content, _ := os.ReadFile(fn)
|
2020-05-20 11:47:42 +08:00
|
|
|
t.Log(content)
|
|
|
|
os.Remove(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 18:09:26 +03:00
|
|
|
|
|
|
|
func TestGetNextRotationHour(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
now time.Time
|
2023-12-14 10:02:26 +08:00
|
|
|
delta uint
|
2023-06-30 18:09:26 +03:00
|
|
|
expectedHour int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
now: time.Date(1980, 1, 6, 15, 34, 0, 0, time.UTC),
|
|
|
|
delta: 3,
|
|
|
|
expectedHour: 18,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
now: time.Date(1980, 1, 6, 23, 59, 0, 0, time.UTC),
|
|
|
|
delta: 1,
|
|
|
|
expectedHour: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
now: time.Date(1980, 1, 6, 22, 15, 0, 0, time.UTC),
|
|
|
|
delta: 2,
|
|
|
|
expectedHour: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
now: time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC),
|
|
|
|
delta: 1,
|
|
|
|
expectedHour: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-14 10:02:26 +08:00
|
|
|
test := func(now time.Time, delta uint, expectedHour int) func(*testing.T) {
|
2023-06-30 18:09:26 +03:00
|
|
|
return func(t *testing.T) {
|
|
|
|
got := getNextRotationHour(now, delta)
|
|
|
|
if got != expectedHour {
|
|
|
|
t.Fatalf("Expected %d, found: %d\n", expectedHour, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tcs {
|
|
|
|
t.Run("TestGetNextRotationHour_"+strconv.Itoa(i), test(tc.now, tc.delta, tc.expectedHour))
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 19:16:01 +08:00
|
|
|
|
|
|
|
func TestClearBackups(t *testing.T) {
|
|
|
|
dir := "./test"
|
|
|
|
os.Mkdir(dir, 0700)
|
|
|
|
w := NewAsyncFileWriter("./test/bsc.log", 100, 1, 1)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
fakeCurrentTime := time.Now()
|
|
|
|
name := ""
|
|
|
|
data := []byte("data")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
name = w.filePath + "." + fakeCurrentTime.Format(backupTimeFormat)
|
|
|
|
_ = os.WriteFile(name, data, 0700)
|
|
|
|
fakeCurrentTime = fakeCurrentTime.Add(-time.Hour * 1)
|
|
|
|
}
|
|
|
|
oldFile := w.getExpiredFile(w.filePath, w.maxBackups, w.rotateHours)
|
|
|
|
w.removeExpiredFile()
|
|
|
|
_, err := os.Stat(oldFile)
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
}
|