-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufferedlogwriter_test.go
210 lines (177 loc) · 5.2 KB
/
bufferedlogwriter_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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENSE file for details.
package logsender_test
import (
"fmt"
"strconv"
"time"
"github.com/juju/loggo"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/worker/logsender"
"github.com/juju/juju/worker/logsender/logsendertest"
)
const maxLen = 6
type bufferedLogWriterSuite struct {
coretesting.BaseSuite
writer *logsender.BufferedLogWriter
shouldClose bool
}
var _ = gc.Suite(&bufferedLogWriterSuite{})
func (s *bufferedLogWriterSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
s.writer = logsender.NewBufferedLogWriter(maxLen)
s.shouldClose = true
}
func (s *bufferedLogWriterSuite) TearDownTest(c *gc.C) {
if s.shouldClose {
s.writer.Close()
}
s.BaseSuite.TearDownTest(c)
}
func (s *bufferedLogWriterSuite) TestOne(c *gc.C) {
s.writeAndReceive(c)
}
func (s *bufferedLogWriterSuite) TestMultiple(c *gc.C) {
for i := 0; i < 10; i++ {
s.writeAndReceive(c)
}
}
func (s *bufferedLogWriterSuite) TestBuffering(c *gc.C) {
// Write several log message before attempting to read them out.
const numMessages = 5
now := time.Now()
for i := 0; i < numMessages; i++ {
s.writer.Write(
loggo.Entry{
Level: loggo.Level(i),
Module: fmt.Sprintf("module%d", i),
Filename: fmt.Sprintf("filename%d", i),
Line: i,
Timestamp: now.Add(time.Duration(i)),
Message: fmt.Sprintf("message%d", i),
})
}
for i := 0; i < numMessages; i++ {
c.Assert(*s.receiveOne(c), gc.DeepEquals, logsender.LogRecord{
Time: now.Add(time.Duration(i)),
Module: fmt.Sprintf("module%d", i),
Location: fmt.Sprintf("filename%d:%d", i, i),
Level: loggo.Level(i),
Message: fmt.Sprintf("message%d", i),
})
}
}
func (s *bufferedLogWriterSuite) TestLimiting(c *gc.C) {
write := func(msgNum int) {
s.writer.Write(
loggo.Entry{
Level: loggo.INFO,
Module: "module",
Filename: "filename",
Line: 42,
Timestamp: time.Now(),
Message: fmt.Sprintf("log%d", msgNum),
})
}
expect := func(msgNum, dropped int) {
rec := s.receiveOne(c)
c.Assert(rec.Message, gc.Equals, fmt.Sprintf("log%d", msgNum))
c.Assert(rec.DroppedAfter, gc.Equals, dropped)
}
// Write more logs than the buffer allows.
for i := 0; i < maxLen+3; i++ {
write(i)
}
// Read some logs from the writer.
// Even though logs have been dropped, log 0 is still seen
// first. This is useful because means the time range for dropped
// logs can be observed.
expect(0, 2) // logs 1 and 2 dropped here
expect(3, 0)
expect(4, 0)
// Now write more logs, again exceeding the limit.
for i := maxLen + 3; i < maxLen+3+maxLen; i++ {
write(i)
}
// Read all the remaining logs off.
expect(5, 3) // logs 6, 7 and 8 logs dropped here
for i := 9; i < maxLen+3+maxLen; i++ {
expect(i, 0)
}
logsendertest.ExpectLogStats(c, s.writer, logsender.LogStats{
Enqueued: maxLen*2 + 3,
Sent: maxLen*2 + 3 - 5,
Dropped: 5,
})
}
func (s *bufferedLogWriterSuite) TestClose(c *gc.C) {
s.writer.Close()
s.shouldClose = false // Prevent the usual teardown (calling Close twice will panic)
// Output channel closing means the bufferedLogWriterSuite loop
// has finished.
select {
case _, ok := <-s.writer.Logs():
c.Assert(ok, jc.IsFalse)
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for output channel to close")
}
// Further Write attempts should fail.
c.Assert(func() { s.writeAndReceive(c) }, gc.PanicMatches, ".*send on closed channel")
}
func (s *bufferedLogWriterSuite) TestInstallBufferedLogWriter(c *gc.C) {
bufferedLogger, err := logsender.InstallBufferedLogWriter(loggo.DefaultContext(), 10)
c.Assert(err, jc.ErrorIsNil)
defer logsender.UninstallBufferedLogWriter()
logger := loggo.GetLogger("bufferedLogWriter-test")
for i := 0; i < 5; i++ {
logger.Infof("%d", i)
}
logsCh := bufferedLogger.Logs()
for i := 0; i < 5; i++ {
select {
case rec := <-logsCh:
c.Assert(rec.Message, gc.Equals, strconv.Itoa(i))
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for logs")
}
}
}
func (s *bufferedLogWriterSuite) TestUninstallBufferedLogWriter(c *gc.C) {
_, err := logsender.InstallBufferedLogWriter(loggo.DefaultContext(), 10)
c.Assert(err, jc.ErrorIsNil)
err = logsender.UninstallBufferedLogWriter()
c.Assert(err, jc.ErrorIsNil)
// Second uninstall attempt should fail
err = logsender.UninstallBufferedLogWriter()
c.Assert(err, gc.ErrorMatches, "failed to uninstall log buffering: .+")
}
func (s *bufferedLogWriterSuite) writeAndReceive(c *gc.C) {
now := time.Now()
s.writer.Write(
loggo.Entry{
Level: loggo.INFO,
Module: "module",
Filename: "filename",
Line: 99,
Timestamp: now,
Message: "message",
})
c.Assert(*s.receiveOne(c), gc.DeepEquals, logsender.LogRecord{
Time: now,
Module: "module",
Location: "filename:99",
Level: loggo.INFO,
Message: "message",
})
}
func (s *bufferedLogWriterSuite) receiveOne(c *gc.C) *logsender.LogRecord {
select {
case rec := <-s.writer.Logs():
return rec
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for log record")
}
panic("should never get here")
}