-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_file_test.go
102 lines (81 loc) · 2.08 KB
/
rotate_file_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
package logh
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestNewRotateFile(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest")
require.NoError(t, err)
defer os.RemoveAll(path)
path += "/TestNewRotateFile"
file, err := NewRotateFile(path, "test", 10, nil)
require.NoError(t, err)
require.NotNil(t, file)
dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 1)
}
func TestRotateSize(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestRotateSize")
require.NoError(t, err)
defer os.RemoveAll(path)
file, err := NewRotateFile(path, "test", 100)
require.NoError(t, err)
b := []byte("test\n")
for range 100 {
require.NoError(t, err)
_, err = file.Write(b)
require.NoError(t, err)
}
dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 5)
}
func TestRotateInterval(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestRotateInterval")
require.NoError(t, err)
defer os.RemoveAll(path)
file, err := NewRotateFile(path, "test", 1024, WithCheckEveryN(1), WithRotateInterval(100*time.Millisecond))
require.NoError(t, err)
b := []byte("test\n")
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}
dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 1)
time.Sleep(100 * time.Millisecond)
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}
dir, err = os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 2)
}
func TestCreateFileAfterRemove(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestRotateInterval")
require.NoError(t, err)
defer os.RemoveAll(path)
file, err := NewRotateFile(path, "test", 1024*1024*1024, WithCheckEveryN(3))
require.NoError(t, err)
b := []byte("test\n")
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}
os.Remove(file.file.Name())
dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 0)
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}
dir, err = os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 1)
}