-
Notifications
You must be signed in to change notification settings - Fork 255
/
logkit_test.go
84 lines (79 loc) · 1.95 KB
/
logkit_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/qiniu/log"
"github.com/stretchr/testify/assert"
)
func Test_getValidPath(t *testing.T) {
t.Parallel()
confs := []string{
"test1",
"./test1",
"../logkit/test1",
"./test2",
}
paths := getValidPath(confs)
if len(paths) != 2 {
t.Errorf("get paths error exp 2 but got %v %v", len(paths), paths)
}
for _, v := range paths {
os.Remove(v)
}
}
func Test_RotateClean(t *testing.T) {
t.Parallel()
dirp := "Test_RotateClean"
os.MkdirAll(dirp, 0755)
defer os.RemoveAll(dirp)
ch1 := make(chan struct{}, 0)
go loopCleanLogkitLog(dirp, "logkit.log-*", 3, "10ms", ch1)
ch2 := make(chan struct{}, 0)
go loopRotateLogs(filepath.Join(dirp, "logkit.log"), 10, 10*time.Nanosecond, ch2)
exitchan := make(chan struct{}, 0)
go func() {
i := 0
for {
select {
case <-exitchan:
return
default:
//通过这一行打印日志到文件,生成日志文件,误删
log.Info("test output log ", i)
}
i++
}
}()
time.Sleep(2 * time.Second)
exitchan <- struct{}{}
ch2 <- struct{}{}
time.Sleep(time.Millisecond)
ch1 <- struct{}{}
nn, _ := ioutil.ReadDir(dirp)
assert.Equal(t, 3, len(nn))
}
func Test_cleanlogkitlog(t *testing.T) {
t.Parallel()
dirp := "Test_cleanlogkitlog"
os.MkdirAll(dirp, 0755)
defer os.RemoveAll(dirp)
ioutil.WriteFile(filepath.Join(dirp, "first.log"), []byte("first"), 0666)
time.Sleep(10 * time.Millisecond)
ioutil.WriteFile(filepath.Join(dirp, "second.log"), []byte("second"), 0666)
time.Sleep(10 * time.Millisecond)
ioutil.WriteFile(filepath.Join(dirp, "third.log"), []byte("third"), 0666)
time.Sleep(10 * time.Millisecond)
ioutil.WriteFile(filepath.Join(dirp, "forth.log"), []byte("forth"), 0666)
time.Sleep(10 * time.Millisecond)
cleanLogkitLog(dirp, "*.log", 3)
nn, _ := ioutil.ReadDir(dirp)
assert.Equal(t, 3, len(nn))
for _, v := range nn {
if v.Name() == "first.log" {
t.Fatal("should not have first")
}
}
}