-
Notifications
You must be signed in to change notification settings - Fork 5
/
logrotate_test.go
100 lines (91 loc) · 2.85 KB
/
logrotate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package logrotate
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func Test_SimpleWrite(t *testing.T) {
// no rotate
t.Run("no rotate", func(t *testing.T) {
rl, err := NewRoteteLog("./testdata/test.log.2006010215", WithCurLogLinkname("./testdata/test.log"))
if assert.Empty(t, err) {
defer rl.Close()
rl.Write([]byte("hello, world!"))
compareFileContent(t, rl.getLatestLogPath(time.Now()), "hello, world!")
compareFileContent(t, "./testdata/test.log", "hello, world!")
os.RemoveAll("./testdata/")
}
})
// no rotate, on link
t.Run("no rotate and link", func(t *testing.T) {
rl, err := NewRoteteLog("./testdata/test.log")
if assert.Empty(t, err) {
defer rl.Close()
rl.Write([]byte("hello, world!"))
content, err := ioutil.ReadFile(rl.getLatestLogPath(time.Now()))
if assert.Empty(t, err) {
assert.Equal(t, string(content), "hello, world!")
t.Log(string(content))
}
os.RemoveAll("./testdata/")
}
})
}
func Test_Rotate(t *testing.T) {
rl, err := NewRoteteLog("./testdata/test.log.2006010215", WithRotateTime(time.Hour), WithCurLogLinkname("./testdata/test.log"))
if assert.Empty(t, err) {
defer rl.Close()
rotate := make(chan time.Time, 1)
rl.rotate = rotate
rl.Write([]byte("hello, world\n"))
nextHour := time.Now().Add(time.Hour)
rotate <- nextHour
time.Sleep(time.Millisecond * 100)
rl.Write([]byte("hello, world2\n"))
compareFileContent(t, rl.getLatestLogPath(time.Now()), "hello, world\n")
compareFileContent(t, rl.getLatestLogPath(nextHour), "hello, world2\n")
compareFileContent(t, "./testdata/test.log", "hello, world2\n")
os.RemoveAll("./testdata/")
}
}
func Test_DeleteExpiredFile(t *testing.T) {
rl, err := NewRoteteLog("./testdata/test.log.2006010215", WithRotateTime(time.Hour), WithCurLogLinkname("./testdata/test.log"),
WithDeleteExpiredFile(time.Second, "test.log*"))
if assert.Empty(t, err) {
defer rl.Close()
for i := 0; i < 10; i++ {
os.OpenFile(fmt.Sprintf("./testdata/test.log.%d", i), os.O_CREATE, 0644)
}
matches, _ := filepath.Glob("./testdata/test.log*")
assert.Equal(t, 12, len(matches))
time.Sleep(time.Millisecond * 1200)
rl.rotateFile(time.Now())
time.Sleep(time.Millisecond * 10)
matches, _ = filepath.Glob("./testdata/test.log*")
assert.Equal(t, 1, len(matches))
}
os.RemoveAll("./testdata/")
}
func Test_Speed(t *testing.T) {
t.Skip()
rl, err := NewRoteteLog("./testdata/test.log", WithRotateTime(time.Hour))
if assert.Empty(t, err) {
bg := time.Now()
for i := 0; i < 1000000; i++ {
rl.Write([]byte("hello, world\n"))
}
t.Log(time.Since(bg))
os.Remove("./testdata/test.log")
}
}
func compareFileContent(t *testing.T, filename string, str string) {
content, err := ioutil.ReadFile(filename)
t.Log(string(content))
if assert.Empty(t, err) {
assert.Equal(t, str, string(content))
}
}