-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_test.go
87 lines (70 loc) · 1.37 KB
/
rotate_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
package cronolog
import (
"log"
"os"
"path"
"path/filepath"
"testing"
"time"
)
var tmpdir = "./tmp"
func initTMPDir() {
os.MkdirAll(tmpdir, 0666)
}
func removeTMPDir() error {
return os.RemoveAll(tmpdir)
}
func countFiles(dir string) int {
total := 0
filepath.Walk(tmpdir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
total++
return nil
})
return total
}
func TestRotate(t *testing.T) {
initTMPDir()
defer removeTMPDir()
tmpFile := path.Join(tmpdir, "/t-%Y%m%d%H%M%S.log")
period := "1s"
rt, err := NewRotater(tmpFile, period, 0)
if err != nil {
t.Fatal(err)
}
defer rt.Close()
log.SetOutput(rt)
files := 3
for i := 0; i < files; i++ {
log.Println("rotate", i)
time.Sleep(time.Second)
}
n := countFiles(tmpdir)
if n != files {
t.Fatalf("should be gernate %d files, but got %d files", files, n)
}
}
func TestBacklogs(t *testing.T) {
initTMPDir()
defer removeTMPDir()
tmpFile := path.Join(tmpdir, "/backlog-%Y%m%d%H%M%S.log")
backlogs := 3
period := "1s"
r, err := NewRotater(tmpFile, period, backlogs)
if err != nil {
t.Fatal(err)
}
defer r.Close()
log.SetOutput(r)
times := backlogs * 2
for i := 0; i < times; i++ {
log.Println(i)
time.Sleep(time.Second)
}
n := countFiles(tmpdir)
if n != backlogs {
t.Fatalf("should be gernate %d files, but got %d files", backlogs, n)
}
}