-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathmonitor_test.go
183 lines (148 loc) · 3.85 KB
/
monitor_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package monitor
import (
"fmt"
"testing"
"time"
log "github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)
func TestMonitor_Start(t *testing.T) {
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Error,
})
m := New(Config{
BufferSize: 512,
Logger: logger,
LoggerOptions: &log.LoggerOptions{
Level: log.Debug},
})
logCh := m.Start()
defer m.Stop()
logger.Debug("test log")
for {
select {
case log := <-logCh:
require.Contains(t, string(log), "[DEBUG] test log")
return
case <-time.After(3 * time.Second):
t.Fatal("Expected to receive from log channel")
}
}
}
func TestMonitor_Stop(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Error,
})
m := New(Config{
BufferSize: 512,
Logger: logger,
LoggerOptions: &log.LoggerOptions{
Level: log.Debug,
},
})
logCh := m.Start()
logger.Debug("test log")
require.Eventually(t, func() bool {
return len(logCh) == 1
}, 3*time.Second, 100*time.Millisecond, "expected logCh to have 1 log in it")
m.Stop()
// This log line should not be output to the log channel
logger.Debug("After Stop")
for {
select {
case log := <-logCh:
if string(log) != "" {
require.Contains(t, string(log), "[DEBUG] test log")
} else {
return
}
case <-time.After(3 * time.Second):
t.Fatal("Expected to receive from log channel")
}
}
}
func TestMonitor_DroppedMessages(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Warn,
})
mcfg := Config{
BufferSize: 5,
Logger: logger,
LoggerOptions: &log.LoggerOptions{
Level: log.Debug,
},
}
m := New(mcfg)
doneCh := make(chan struct{})
defer close(doneCh)
logCh := m.Start()
// Overflow the configured buffer size before we attempt to receive from the
// logCh. We choose (2*bufSize+1) to account for:
// - buffer size of internal write channel
// - buffer size of channel returned from Start()
// - A message guaranteed to be dropped because all buffers are full
for i := 0; i <= 2*mcfg.BufferSize+1; i++ {
logger.Debug(fmt.Sprintf("test message %d", i))
}
// Make sure we do not stop before the goroutines have time to process.
require.Eventually(t, func() bool {
return len(logCh) == mcfg.BufferSize
}, 3*time.Second, 100*time.Millisecond, "expected logCh to have a full log buffer")
dropped := m.Stop()
// The number of dropped messages is non-deterministic, so we only assert
// that we dropped at least 1.
require.GreaterOrEqual(t, dropped, 1)
}
func TestMonitor_ZeroBufSizeDefault(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Error,
})
m := New(Config{
BufferSize: 0,
Logger: logger,
LoggerOptions: &log.LoggerOptions{
Level: log.Debug,
},
})
logCh := m.Start()
defer m.Stop()
logger.Debug("test log")
// If we do not default the buffer size, the monitor will be unable to buffer
// a log line.
require.Eventually(t, func() bool {
return len(logCh) == 1
}, 3*time.Second, 100*time.Millisecond, "expected logCh to have 1 log buffered")
for {
select {
case log := <-logCh:
require.Contains(t, string(log), "[DEBUG] test log")
return
case <-time.After(3 * time.Second):
t.Fatal("Expected to receive from log channel")
}
}
}
func TestMonitor_WriteStopped(t *testing.T) {
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Error,
})
mwriter := &monitor{
logger: logger,
doneCh: make(chan struct{}, 1),
}
mwriter.Stop()
n, err := mwriter.Write([]byte("write after close"))
require.Equal(t, n, 0)
require.EqualError(t, err, "monitor stopped")
}