-
Notifications
You must be signed in to change notification settings - Fork 3
/
appender_test.go
174 lines (131 loc) · 9.46 KB
/
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
162
163
164
165
166
167
168
169
170
171
172
173
174
package logf
import (
"encoding/base64"
"errors"
"os"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDiscardAppender(t *testing.T) {
a := NewDiscardAppender()
assert.NoError(t, a.Append(Entry{}))
assert.NoError(t, a.Flush())
assert.NoError(t, a.Sync())
}
type testWriterNoSync struct {
writeData []byte
}
func (w *testWriterNoSync) Write(p []byte) (n int, err error) {
if len(p) == 0 {
panic("TestBuffer.Writer buffer can't be empty!")
}
w.writeData = p
return 0, nil
}
type testWriter struct {
writeData []byte
syncCounter int
e error
}
func (w *testWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 {
panic("TestBuffer.Writer buffer can't be empty!")
}
w.writeData = p
return 0, nil
}
func (w *testWriter) Sync() error {
w.syncCounter++
return w.e
}
func checkWriterData(t *testing.T, w *testWriter, writeData string, syncCounter int) {
assert.Equal(t, writeData, string(w.writeData))
assert.Equal(t, syncCounter, w.syncCounter)
w.syncCounter = 0
w.writeData = nil
}
type failingEncoder struct {
e error
}
func (e *failingEncoder) Encode(*Buffer, Entry) error {
return e.e
}
type failingWriter struct {
e error
}
func (w *failingWriter) Write(p []byte) (n int, err error) {
return 0, w.e
}
func (w *failingWriter) Sync() error {
return w.e
}
func TestWriteAppenderErrors(t *testing.T) {
goldenErr := errors.New("test error")
w := testWriter{}
a := NewWriteAppender(&w, &failingEncoder{goldenErr})
// Sync should be called once in constructor to check sync possibility.
checkWriterData(t, &w, "", 1)
assert.EqualError(t, a.Append(Entry{}), goldenErr.Error())
checkWriterData(t, &w, "", 0)
a = NewWriteAppender(&failingWriter{goldenErr}, NewJSONEncoder.Default())
assert.NoError(t, a.Append(Entry{}))
assert.EqualError(t, a.Flush(), goldenErr.Error())
assert.EqualError(t, a.Sync(), goldenErr.Error())
}
func TestWriteAppenderNoSync(t *testing.T) {
w := testWriterNoSync{}
a := NewWriteAppender(&w, NewJSONEncoder.Default())
assert.NoError(t, a.Sync())
}
func TestWriteAppenderInvalidSync(t *testing.T) {
// Check for EINVAL - a known error if Writer is bound to a
// special File (e.g., a pipe or socket) which does not support
// synchronization.
w := testWriter{e: &os.PathError{Err: syscall.EINVAL}}
a := NewWriteAppender(&w, NewJSONEncoder.Default())
// Sync should be called once in constructor to check sync possibility.
checkWriterData(t, &w, "", 1)
assert.NoError(t, a.Sync())
// Sync is ignoring now.
checkWriterData(t, &w, "", 0)
}
func TestWriteAppender(t *testing.T) {
w := testWriter{}
a := NewWriteAppender(&w, NewJSONEncoder.Default())
// Sync should be called once in constructor to check sync possibility.
checkWriterData(t, &w, "", 1)
assert.NoError(t, a.Sync())
// Expected no buffer because of no data and one sync call.
checkWriterData(t, &w, "", 1)
// Test Append auto flush.
bigEntry := Entry{
Text: base64.StdEncoding.EncodeToString(make([]byte, PageSize)),
}
assert.NoError(t, a.Append(bigEntry))
// Passed entry is enough to force Appender to call Flush automatically.
// Expected specified buffer and no sync calls.
checkWriterData(t, &w, `{"level":"error","ts":"0001-01-01T00:00:00Z","msg":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="}`+"\n", 0)
// Test Flush.
assert.NoError(t, a.Append(Entry{}))
// Passed entry size less than Appender buffer size. So no buffer is
// expected and no sync calls.
checkWriterData(t, &w, "", 0)
assert.NoError(t, a.Flush())
// Flush forced Appender to commit it's buffer. Expected specified
// buffer and no sync calls.
checkWriterData(t, &w, `{"level":"error","ts":"0001-01-01T00:00:00Z","msg":""}`+"\n", 0)
assert.NoError(t, a.Flush())
// Second flush should do nothing.
checkWriterData(t, &w, "", 0)
// Test Sync.
assert.NoError(t, a.Append(Entry{}))
// Passed entry size less than Appender buffer size. So no buffer is
// expected and no sync calls.
checkWriterData(t, &w, "", 0)
assert.NoError(t, a.Flush())
assert.NoError(t, a.Sync())
// Sync forced Appender to commit it's buffer. Expected specified
// buffer and one sync call.
checkWriterData(t, &w, `{"level":"error","ts":"0001-01-01T00:00:00Z","msg":""}`+"\n", 1)
}