-
Notifications
You must be signed in to change notification settings - Fork 8
/
size_rotator_test.go
134 lines (98 loc) · 2.52 KB
/
size_rotator_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package rotator
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)
const (
path = "test_size.log"
)
func cleanup() {
os.Remove(path)
os.Remove(path + ".1")
os.Remove(path + ".2")
os.Remove(path + ".3")
}
func TestSizeNormalOutput(t *testing.T) {
cleanup()
defer cleanup()
rotator := NewSizeRotator(path)
defer rotator.Close()
rotator.WriteString("SAMPLE LOG")
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
defer file.Close()
b := make([]byte, 10)
file.Read(b)
assert.Equal(t, "SAMPLE LOG", string(b))
rotator.WriteString("|NEXT LOG")
rotator.WriteString("|LAST LOG")
b = make([]byte, 28)
file.ReadAt(b, 0)
assert.Equal(t, "SAMPLE LOG|NEXT LOG|LAST LOG", string(b))
}
func TestSizeRotation(t *testing.T) {
cleanup()
defer cleanup()
rotator := NewSizeRotator(path)
rotator.RotationSize = 10
defer rotator.Close()
rotator.WriteString("0123456789")
// it should not be rotated
stat, _ := os.Lstat(path + ".1")
assert.Nil(t, stat)
// it should be rotated
rotator.WriteString("0123456789")
stat, _ = os.Lstat(path)
assert.NotNil(t, stat)
assert.Equal(t, stat.Size(), 10)
stat, _ = os.Lstat(path + ".1")
assert.NotNil(t, stat)
assert.Equal(t, stat.Size(), 10)
}
func TestSizeAppendExist(t *testing.T) {
cleanup()
defer cleanup()
file, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
file.WriteString("01234") // size should be 5
file.Close()
rotator := NewSizeRotator(path)
rotator.RotationSize = 10
_, err := rotator.WriteString("56789012")
assert.Nil(t, err)
stat, _ := os.Lstat(path)
assert.NotNil(t, stat)
assert.Equal(t, 8, stat.Size())
stat, _ = os.Lstat(path + ".1")
assert.NotNil(t, stat)
assert.Equal(t, 5, stat.Size())
}
func TestSizeMaxRotation(t *testing.T) {
cleanup()
defer cleanup()
rotator := NewSizeRotator(path)
rotator.RotationSize = 10
rotator.MaxRotation = 3
defer rotator.Close()
rotator.WriteString("0123456789")
stat, _ := os.Lstat(path + ".1")
assert.Nil(t, stat)
rotator.WriteString("0123456789")
rotator.WriteString("0123456789")
rotator.WriteString("0123456789")
stat, _ = os.Lstat(path + ".1")
assert.NotNil(t, stat)
assert.Equal(t, stat.Size(), 10)
stat, _ = os.Lstat(path + ".2")
assert.NotNil(t, stat)
assert.Equal(t, stat.Size(), 10)
stat, _ = os.Lstat(path + ".3")
assert.NotNil(t, stat)
assert.Equal(t, stat.Size(), 10)
// it should fail rotation
_, err := rotator.WriteString("0123456789")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "rotation count has been exceeded")
}