-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrotator_test.go
More file actions
90 lines (63 loc) · 1.47 KB
/
rotator_test.go
File metadata and controls
90 lines (63 loc) · 1.47 KB
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
package rotator
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRotatorInterfaceByDailyRotator(t *testing.T) {
path := "test_daily.log"
stat, _ := os.Lstat(path)
if stat != nil {
os.Remove(path)
}
var r Rotator
// assign NewDailyRotator
r = NewDailyRotator(path)
// 1. Close method
defer r.Close()
// 2. Write method
r.Write(bytes.NewBufferString("SAMPLE LOG").Bytes())
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))
// 3. WriteString method
r.WriteString("\nNEXT LOG")
r.WriteString("\nLAST LOG")
b = make([]byte, 28)
file.ReadAt(b, 0)
assert.Equal(t, "SAMPLE LOG\nNEXT LOG\nLAST LOG", string(b))
}
func TestRotatorInterfaceBySizeRotator(t *testing.T) {
path := "test_size.log"
stat, _ := os.Lstat(path)
if stat != nil {
os.Remove(path)
}
var r Rotator
// assign NewSizeRotator
r = NewSizeRotator(path)
// 1. Close method
defer r.Close()
// 2. Write method
r.Write(bytes.NewBufferString("SAMPLE LOG").Bytes())
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))
// 3. WriteString method
r.WriteString("|NEXT LOG")
r.WriteString("|LAST LOG")
b = make([]byte, 28)
file.ReadAt(b, 0)
assert.Equal(t, "SAMPLE LOG|NEXT LOG|LAST LOG", string(b))
}