forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_syncer_test.go
217 lines (181 loc) · 7.23 KB
/
write_syncer_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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package zapcore
import (
"bytes"
"errors"
"testing"
"time"
"io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"go.uber.org/zap/internal/ztest"
)
type writeSyncSpy struct {
io.Writer
ztest.Syncer
}
type errorWriter struct{}
func (*errorWriter) Write([]byte) (int, error) { return 0, errors.New("unimplemented") }
func requireWriteWorks(t testing.TB, ws WriteSyncer) {
n, err := ws.Write([]byte("foo"))
require.NoError(t, err, "Unexpected error writing to WriteSyncer.")
require.Equal(t, 3, n, "Wrote an unexpected number of bytes.")
}
func TestAddSyncWriteSyncer(t *testing.T) {
buf := &bytes.Buffer{}
concrete := &writeSyncSpy{Writer: buf}
ws := AddSync(concrete)
requireWriteWorks(t, ws)
require.NoError(t, ws.Sync(), "Unexpected error syncing a WriteSyncer.")
require.True(t, concrete.Called(), "Expected to dispatch to concrete type's Sync method.")
concrete.SetError(errors.New("fail"))
assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Sync method.")
}
func TestAddSyncWriter(t *testing.T) {
// If we pass a plain io.Writer, make sure that we still get a WriteSyncer
// with a no-op Sync.
buf := &bytes.Buffer{}
ws := AddSync(buf)
requireWriteWorks(t, ws)
assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
}
func TestBufferWriter(t *testing.T) {
defer goleak.VerifyNone(t)
// If we pass a plain io.Writer, make sure that we still get a WriteSyncer
// with a no-op Sync.
t.Run("sync", func(t *testing.T) {
buf := &bytes.Buffer{}
ws, close := Buffer(AddSync(buf), 0, 0)
defer close()
requireWriteWorks(t, ws)
assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.")
assert.Equal(t, "foo", buf.String(), "Unexpected log string")
})
t.Run("close", func(t *testing.T) {
buf := &bytes.Buffer{}
ws, close := Buffer(AddSync(buf), 0, 0)
requireWriteWorks(t, ws)
assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.")
close()
assert.Equal(t, "foo", buf.String(), "Unexpected log string")
})
t.Run("wrap twice", func(t *testing.T) {
buf := &bytes.Buffer{}
bufsync, close1 := Buffer(AddSync(buf), 0, 0)
ws, close2 := Buffer(bufsync, 0, 0)
requireWriteWorks(t, ws)
assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
close2()
close1()
assert.Equal(t, "foo", buf.String(), "Unexpected log string")
})
t.Run("small buffer", func(t *testing.T) {
buf := &bytes.Buffer{}
ws, close := Buffer(AddSync(buf), 5, 0)
defer close()
requireWriteWorks(t, ws)
assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.")
requireWriteWorks(t, ws)
assert.Equal(t, "foo", buf.String(), "Unexpected log string")
})
t.Run("flush error", func(t *testing.T) {
ws, close := Buffer(AddSync(&errorWriter{}), 4, time.Nanosecond)
n, err := ws.Write([]byte("foo"))
require.NoError(t, err, "Unexpected error writing to WriteSyncer.")
require.Equal(t, 3, n, "Wrote an unexpected number of bytes.")
ws.Write([]byte("foo"))
assert.Error(t, close(), "Expected close to fail.")
})
t.Run("flush timer", func(t *testing.T) {
buf := &bytes.Buffer{}
ws, close := Buffer(AddSync(buf), 6, time.Microsecond)
defer close()
requireWriteWorks(t, ws)
ztest.Sleep(10 * time.Millisecond)
bws := ws.(*bufferWriterSyncer)
bws.Lock()
assert.Equal(t, "foo", buf.String(), "Unexpected log string")
bws.Unlock()
// flush twice to validate loop logic
requireWriteWorks(t, ws)
ztest.Sleep(10 * time.Millisecond)
bws.Lock()
assert.Equal(t, "foofoo", buf.String(), "Unexpected log string")
bws.Unlock()
})
}
func TestNewMultiWriteSyncerWorksForSingleWriter(t *testing.T) {
w := &ztest.Buffer{}
ws := NewMultiWriteSyncer(w)
assert.Equal(t, w, ws, "Expected NewMultiWriteSyncer to return the same WriteSyncer object for a single argument.")
ws.Sync()
assert.True(t, w.Called(), "Expected Sync to be called on the created WriteSyncer")
}
func TestMultiWriteSyncerWritesBoth(t *testing.T) {
first := &bytes.Buffer{}
second := &bytes.Buffer{}
ws := NewMultiWriteSyncer(AddSync(first), AddSync(second))
msg := []byte("dumbledore")
n, err := ws.Write(msg)
require.NoError(t, err, "Expected successful buffer write")
assert.Equal(t, len(msg), n)
assert.Equal(t, msg, first.Bytes())
assert.Equal(t, msg, second.Bytes())
}
func TestMultiWriteSyncerFailsWrite(t *testing.T) {
ws := NewMultiWriteSyncer(AddSync(&ztest.FailWriter{}))
_, err := ws.Write([]byte("test"))
assert.Error(t, err, "Write error should propagate")
}
func TestMultiWriteSyncerFailsShortWrite(t *testing.T) {
ws := NewMultiWriteSyncer(AddSync(&ztest.ShortWriter{}))
n, err := ws.Write([]byte("test"))
assert.NoError(t, err, "Expected fake-success from short write")
assert.Equal(t, 3, n, "Expected byte count to return from underlying writer")
}
func TestWritestoAllSyncs_EvenIfFirstErrors(t *testing.T) {
failer := &ztest.FailWriter{}
second := &bytes.Buffer{}
ws := NewMultiWriteSyncer(AddSync(failer), AddSync(second))
_, err := ws.Write([]byte("fail"))
assert.Error(t, err, "Expected error from call to a writer that failed")
assert.Equal(t, []byte("fail"), second.Bytes(), "Expected second sink to be written after first error")
}
func TestMultiWriteSyncerSync_PropagatesErrors(t *testing.T) {
badsink := &ztest.Buffer{}
badsink.SetError(errors.New("sink is full"))
ws := NewMultiWriteSyncer(&ztest.Discarder{}, badsink)
assert.Error(t, ws.Sync(), "Expected sync error to propagate")
}
func TestMultiWriteSyncerSync_NoErrorsOnDiscard(t *testing.T) {
ws := NewMultiWriteSyncer(&ztest.Discarder{})
assert.NoError(t, ws.Sync(), "Expected error-free sync to /dev/null")
}
func TestMultiWriteSyncerSync_AllCalled(t *testing.T) {
failed, second := &ztest.Buffer{}, &ztest.Buffer{}
failed.SetError(errors.New("disposal broken"))
ws := NewMultiWriteSyncer(failed, second)
assert.Error(t, ws.Sync(), "Expected first sink to fail")
assert.True(t, failed.Called(), "Expected first sink to have Sync method called.")
assert.True(t, second.Called(), "Expected call to Sync even with first failure.")
}