-
Notifications
You must be signed in to change notification settings - Fork 3
/
rolling_file_appender_test.go
161 lines (120 loc) · 3.72 KB
/
rolling_file_appender_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package logbuch
import (
"fmt"
"os"
"testing"
)
type testNameSchema struct {
name string
counter int
}
func (schema *testNameSchema) Name() string {
schema.counter++
return fmt.Sprintf("%d_log%s.txt", schema.counter, schema.name)
}
func TestNewRollingFileAppender(t *testing.T) {
if err := os.RemoveAll("out"); err != nil {
t.Fatal(err)
}
rfa, err := NewRollingFileAppender(-1, -1, -1, "out", nil)
if rfa != nil || err == nil || err.Error() != "filename schema must be specified" {
t.Fatalf("Filename schema must be passed, but was: %v", err)
}
rfa, err = NewRollingFileAppender(-1, -1, -1, "out", &testNameSchema{})
if err != nil {
t.Fatalf("Rolling file appender must have been created, but was: %v", err)
}
if rfa.files != defaultFiles || rfa.fileSize != defaultFileSize || rfa.maxBufferSize != defaultBufferSize {
t.Fatalf("Default values not as expected: %v", rfa)
}
if _, err := os.Stat("out/1_log.txt"); os.IsNotExist(err) {
t.Fatalf("First log file must exist, but was: %v", err)
}
if err := rfa.Close(); err != nil {
t.Fatal(err)
}
}
func TestRollingFileAppender_Write(t *testing.T) {
if err := os.RemoveAll("out"); err != nil {
t.Fatal(err)
}
rfa, err := NewRollingFileAppender(3, 5, 10, "out", &testNameSchema{})
if err != nil {
t.Fatalf("Appender must be created, but was: %v", err)
}
for i := 0; i < 5; i++ {
if n, err := rfa.Write([]byte(fmt.Sprintf("%d234\n", i))); err != nil || n != 5 {
t.Fatalf("Log output must have been written, but was: %v %v", err, n)
}
}
if err := rfa.Close(); err != nil {
t.Fatalf("Appender must be closed, but was: %v", err)
}
dir, err := os.ReadDir("out")
if err != nil {
t.Fatal(err)
}
if len(dir) != 3 {
t.Fatalf("Three files must have been created, but was: %v", len(dir))
}
if dir[0].Name() != "2_log.txt" || dir[1].Name() != "3_log.txt" || dir[2].Name() != "4_log.txt" {
t.Fatalf("Files must have appropriate names, but was: %v %v %v", dir[0].Name(), dir[1].Name(), dir[2].Name())
}
if _, err := os.Stat("out/1_log.txt"); !os.IsNotExist(err) {
t.Fatalf("First log file must not exist, but was: %v", err)
}
}
func TestRollingFileAppender_Flush(t *testing.T) {
if err := os.RemoveAll("out"); err != nil {
t.Fatal(err)
}
rfa, err := NewRollingFileAppender(3, 100, 60, "out", &testNameSchema{})
if err != nil {
t.Fatalf("Appender must be created, but was: %v", err)
}
if _, err := rfa.Write([]byte("This fits into the buffer!\n")); err != nil {
t.Fatalf("Log output must have been written, but was: %v", err)
}
content, err := os.ReadFile("out/1_log.txt")
if err != nil {
t.Fatal(err)
}
if string(content) != "" {
t.Fatalf("File must be empty, but was: %v", string(content))
}
if err := rfa.Flush(); err != nil {
t.Fatalf("Log must have been flushed, but was: %v", err)
}
content, err = os.ReadFile("out/1_log.txt")
if err != nil {
t.Fatal(err)
}
if string(content) != "This fits into the buffer!\n" {
t.Fatalf("The log must have been saved, but was: %v", string(content))
}
if err := rfa.Close(); err != nil {
t.Fatal(err)
}
}
func TestRollingFileAppender_Close(t *testing.T) {
if err := os.RemoveAll("out"); err != nil {
t.Fatal(err)
}
rfa, err := NewRollingFileAppender(3, 100, 60, "out", &testNameSchema{})
if err != nil {
t.Fatalf("Appender must be created, but was: %v", err)
}
if _, err := rfa.Write([]byte("This fits into the buffer!\n")); err != nil {
t.Fatalf("Log output must have been written, but was: %v", err)
}
if err := rfa.Close(); err != nil {
t.Fatal(err)
}
content, err := os.ReadFile("out/1_log.txt")
if err != nil {
t.Fatal(err)
}
if string(content) != "This fits into the buffer!\n" {
t.Fatalf("The log must have been saved, but was: %v", string(content))
}
}