-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf_test.go
238 lines (206 loc) · 6.02 KB
/
buf_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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package logger_test
import (
"errors"
"time"
"github.com/juju/clock/testclock"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
corelogger "github.com/juju/juju/core/logger"
coretesting "github.com/juju/juju/testing"
)
type BufferedLoggerSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&BufferedLoggerSuite{})
func (s *BufferedLoggerSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
}
func (s *BufferedLoggerSuite) waitFlush(c *gc.C, mock *mockLogger) []corelogger.LogRecord {
select {
case records := <-mock.called:
return records
case <-time.After(coretesting.LongWait):
}
c.Fatal("timed out waiting for logs to be flushed")
panic("unreachable")
}
func (s *BufferedLoggerSuite) assertNoFlush(c *gc.C, mock *mockLogger, clock *testclock.Clock) {
err := clock.WaitAdvance(0, 0, 0) // There should be no active timers
c.Assert(err, jc.ErrorIsNil)
select {
case records := <-mock.called:
c.Fatalf("unexpected log records: %v", records)
case <-time.After(coretesting.ShortWait):
}
}
func (s *BufferedLoggerSuite) TestLogFlushes(c *gc.C) {
const bufsz = 3
mock := mockLogger{}
clock := testclock.NewClock(time.Time{})
b := corelogger.NewBufferedLogger(&mock, bufsz, time.Minute, clock)
in := []corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}, {
Entity: "not-a-tag",
Message: "bar",
}, {
Entity: "not-a-tag",
Message: "baz",
}}
err := b.Log(in[:2])
c.Assert(err, jc.ErrorIsNil)
mock.CheckNoCalls(c)
err = b.Log(in[2:])
c.Assert(err, jc.ErrorIsNil)
mock.CheckCalls(c, []testing.StubCall{
{"Log", []interface{}{in}},
})
err = clock.WaitAdvance(0, coretesting.LongWait, 0)
c.Assert(err, jc.ErrorIsNil)
s.assertNoFlush(c, &mock, clock)
}
func (s *BufferedLoggerSuite) TestLogFlushesMultiple(c *gc.C) {
const bufsz = 1
mock := mockLogger{}
clock := testclock.NewClock(time.Time{})
b := corelogger.NewBufferedLogger(&mock, bufsz, time.Minute, clock)
in := []corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}, {
Entity: "not-a-tag",
Message: "bar",
}, {
Entity: "not-a-tag",
Message: "baz",
}}
err := b.Log(in)
c.Assert(err, jc.ErrorIsNil)
mock.CheckCalls(c, []testing.StubCall{
{"Log", []interface{}{in[:1]}},
{"Log", []interface{}{in[1:2]}},
{"Log", []interface{}{in[2:]}},
})
}
func (s *BufferedLoggerSuite) TestTimerFlushes(c *gc.C) {
const bufsz = 10
const flushInterval = time.Minute
mock := mockLogger{called: make(chan []corelogger.LogRecord)}
clock := testclock.NewClock(time.Time{})
b := corelogger.NewBufferedLogger(&mock, bufsz, flushInterval, clock)
in := []corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}, {
Entity: "not-a-tag",
Message: "bar",
}}
err := b.Log(in[:1])
c.Assert(err, jc.ErrorIsNil)
mock.CheckNoCalls(c)
// Advance, but not far enough to trigger the flush.
clock.WaitAdvance(30*time.Second, coretesting.LongWait, 1)
mock.CheckNoCalls(c)
// Log again; the timer should not have been reset.
err = b.Log(in[1:])
c.Assert(err, jc.ErrorIsNil)
mock.CheckNoCalls(c)
// Advance to to the flush interval.
clock.Advance(30 * time.Second)
c.Assert(s.waitFlush(c, &mock), jc.DeepEquals, in)
mock.CheckCalls(c, []testing.StubCall{
{"Log", []interface{}{in}},
})
s.assertNoFlush(c, &mock, clock)
mock.ResetCalls()
// Logging again, the timer resets to the time at which
// the new log records are inserted.
err = b.Log(in)
c.Assert(err, jc.ErrorIsNil)
clock.WaitAdvance(59*time.Second, coretesting.LongWait, 1)
mock.CheckNoCalls(c)
clock.Advance(1 * time.Second)
c.Assert(s.waitFlush(c, &mock), jc.DeepEquals, in)
mock.CheckCalls(c, []testing.StubCall{
{"Log", []interface{}{in}},
})
s.assertNoFlush(c, &mock, clock)
}
func (s *BufferedLoggerSuite) TestLogOverCapacity(c *gc.C) {
const bufsz = 2
const flushInterval = time.Minute
mock := mockLogger{called: make(chan []corelogger.LogRecord, 1)}
clock := testclock.NewClock(time.Time{})
// The buffer has a capacity of 2, so writing 3 logs will
// cause 2 to be flushed, with 1 remaining in the buffer
// until the timer triggers.
b := corelogger.NewBufferedLogger(&mock, bufsz, flushInterval, clock)
in := []corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}, {
Entity: "not-a-tag",
Message: "bar",
}, {
Entity: "not-a-tag",
Message: "baz",
}}
err := b.Log(in)
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.waitFlush(c, &mock), jc.DeepEquals, in[:bufsz])
clock.WaitAdvance(time.Minute, coretesting.LongWait, 1)
c.Assert(s.waitFlush(c, &mock), jc.DeepEquals, in[bufsz:])
mock.CheckCalls(c, []testing.StubCall{
{"Log", []interface{}{in[:bufsz]}},
{"Log", []interface{}{in[bufsz:]}},
})
}
func (s *BufferedLoggerSuite) TestFlushNothing(c *gc.C) {
mock := mockLogger{}
clock := testclock.NewClock(time.Time{})
b := corelogger.NewBufferedLogger(&mock, 1, time.Minute, clock)
err := b.Flush()
c.Assert(err, jc.ErrorIsNil)
mock.CheckNoCalls(c)
}
func (s *BufferedLoggerSuite) TestFlushReportsError(c *gc.C) {
mock := mockLogger{}
clock := testclock.NewClock(time.Time{})
mock.SetErrors(errors.New("nope"))
b := corelogger.NewBufferedLogger(&mock, 2, time.Minute, clock)
err := b.Log([]corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}})
c.Assert(err, jc.ErrorIsNil)
err = b.Flush()
c.Assert(err, gc.ErrorMatches, "nope")
}
func (s *BufferedLoggerSuite) TestLogReportsError(c *gc.C) {
mock := mockLogger{}
clock := testclock.NewClock(time.Time{})
mock.SetErrors(errors.New("nope"))
b := corelogger.NewBufferedLogger(&mock, 1, time.Minute, clock)
err := b.Log([]corelogger.LogRecord{{
Entity: "not-a-tag",
Message: "foo",
}})
c.Assert(err, gc.ErrorMatches, "nope")
}
type mockLogger struct {
testing.Stub
called chan []corelogger.LogRecord
}
func (m *mockLogger) Log(in []corelogger.LogRecord) error {
incopy := make([]corelogger.LogRecord, len(in))
copy(incopy, in)
m.MethodCall(m, "Log", incopy)
if m.called != nil {
m.called <- incopy
}
return m.NextErr()
}