-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrel_test.go
258 lines (190 loc) · 5.93 KB
/
barrel_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package barrel
import (
"bytes"
"errors"
"io"
"testing"
"github.com/matryer/is"
)
//go:generate moq -fmt goimports -out ./barrel_test_mock_test.go . Trigger Rotator
func TestRollingWriter_Write(t *testing.T) {
t.Parallel()
t.Run("closed writer", func(t *testing.T) {
t.Parallel()
r := is.New(t)
writer := RollingWriter{}
err := writer.Close()
r.NoErr(err) // should not be any error
_, err = writer.Write([]byte("hello"))
r.True(errors.Is(err, ErrClosed)) // error should wrap ErrClosed
})
t.Run("trigger errors", func(t *testing.T) {
t.Parallel()
r := is.New(t)
writer := RollingWriter{Trigger: faultyTrigger(errTrigger)}
_, err := writer.Write([]byte("hello"))
r.True(errors.Is(err, errTrigger)) // error should wrap underlying Trigger error
})
t.Run("trigger returns false", func(t *testing.T) {
t.Parallel()
t.Run("underlying writer errors", func(t *testing.T) {
t.Parallel()
r := is.New(t)
writer := RollingWriter{Writer: faultyWriter{Err: errWrite}, Trigger: fixedTrigger(false)}
data := []byte("hello")
_, err := writer.Write(data)
r.True(errors.Is(err, errWrite)) // error should wrap underlying io.Writer error
})
t.Run("underlying writer succeeds", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf bytes.Buffer
writer := RollingWriter{Writer: &buf, Trigger: fixedTrigger(false)}
data := []byte("hello")
n, err := writer.Write(data)
r.NoErr(err) //should not be any error
r.True(n == len(data)) // all bytes should be written
r.True(n == len(buf.Bytes())) // all bytes should be written to underlying io.Writer
r.True(bytes.Equal(buf.Bytes(), data)) // bytes written io.Writer should be same as given bytes
})
})
t.Run("trigger returns true", func(t *testing.T) {
t.Parallel()
t.Run("rotator errors", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf1 bytes.Buffer
writer := RollingWriter{Writer: &buf1, Trigger: fixedTrigger(true), Rotator: faultyRotator(errRotate)}
data := []byte("hello")
n, err := writer.Write(data)
r.True(errors.Is(err, errRotate)) // error should wrap underlying Rotator error
r.True(n == 0) // no bytes should be written
r.True(len(buf1.Bytes()) == 0) // original io.Writer should not receive any data
})
t.Run("rotator succeeds", func(t *testing.T) {
t.Parallel()
t.Run("rotated writer errors", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf1 bytes.Buffer
writer := RollingWriter{Writer: &buf1, Trigger: fixedTrigger(true), Rotator: fixedRotator(faultyWriter{Err: errWrite})}
data := []byte("hello")
n, err := writer.Write(data)
r.True(errors.Is(err, errWrite)) // error should wrap underlying Rotator error
r.True(n == 0) // no bytes should be written
r.True(len(buf1.Bytes()) == 0) // original io.Writer should not receive any data
})
t.Run("rotated writer succeeds", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf1, buf2 bytes.Buffer
writer := RollingWriter{Writer: &buf1, Trigger: fixedTrigger(true), Rotator: fixedRotator(&buf2)}
data := []byte("hello")
n, err := writer.Write(data)
r.NoErr(err) // should not be any error
r.True(len(buf1.Bytes()) == 0) // original io.Writer should not receive any data
r.True(n == len(data)) // all bytes should be written
r.True(n == len(buf2.Bytes())) // bytes should be written to rotated io.Writer
r.True(bytes.Equal(buf2.Bytes(), data)) // bytes written to rotated io.Writer should be same as given bytes
})
})
})
}
func TestRollingWriter_Close(t *testing.T) {
t.Parallel()
t.Run("closed writer", func(t *testing.T) {
t.Parallel()
r := is.New(t)
writer := RollingWriter{}
err := writer.Close()
r.NoErr(err) // should not be any error
err = writer.Close()
r.True(errors.Is(err, ErrClosed)) // error should wrap ErrClosed
})
t.Run("writer not io closer", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf bytes.Buffer
writer := RollingWriter{Writer: &buf}
err := writer.Close()
r.NoErr(err) // should not be any error
})
t.Run("writer is io closer", func(t *testing.T) {
t.Parallel()
t.Run("writer close errors", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf bytes.Buffer
writer := RollingWriter{Writer: writeCloser{Writer: &buf, Closer: faultyCloser{Err: errClose}}}
err := writer.Close()
r.True(errors.Is(err, errClose)) // error should wrap underlying io.WriteCloser error
})
t.Run("writer close succeeds", func(t *testing.T) {
t.Parallel()
r := is.New(t)
var buf bytes.Buffer
writer := RollingWriter{Writer: writeCloser{Writer: &buf, Closer: noopCloser{}}}
err := writer.Close()
r.NoErr(err) // should not be any error
})
})
}
func fixedTrigger(value bool) Trigger {
return &TriggerMock{
TriggerFunc: func(_ io.Writer, _ []byte) (bool, error) {
return value, nil
},
}
}
func faultyTrigger(err error) Trigger {
return &TriggerMock{
TriggerFunc: func(_ io.Writer, _ []byte) (bool, error) {
return false, err
},
}
}
func fixedRotator(value io.Writer) Rotator {
return &RotatorMock{
RotateFunc: func(_ io.Writer) (io.Writer, error) {
return value, nil
},
}
}
func faultyRotator(err error) Rotator {
return &RotatorMock{
RotateFunc: func(w io.Writer) (io.Writer, error) {
return w, err
},
}
}
type writeCloser struct {
io.Writer
io.Closer
}
type faultyWriter struct {
Err error
}
func (w faultyWriter) Write(_ []byte) (int, error) {
return 0, w.Err
}
type testErr string
func (e testErr) Error() string {
return string(e)
}
type faultyCloser struct {
Err error
}
func (c faultyCloser) Close() error {
return c.Err
}
type noopCloser struct {
}
func (c noopCloser) Close() error {
return nil
}
const (
errTrigger testErr = "err trigger"
errRotate testErr = "err rotate"
errWrite testErr = "err write"
errClose testErr = "err close"
)