55 "log/syslog"
66 "regexp"
77 "strings"
8- "time"
98)
109
1110// UseMock sets a mock logger as the default logger, and returns it.
@@ -20,13 +19,6 @@ func NewMock() *Mock {
2019 return & Mock {impl {newMockWriter ()}}
2120}
2221
23- // NewWaitingMock creates a mock logger implementing the writer interface.
24- // It stores all logged messages in a buffer for inspection by test
25- // functions.
26- func NewWaitingMock () * WaitingMock {
27- return & WaitingMock {impl {newWaitingMockWriter ()}}
28- }
29-
3022// Mock is a logger that stores all log messages in memory to be examined by a
3123// test.
3224type Mock struct {
@@ -130,39 +122,3 @@ func (m *Mock) Clear() {
130122 w := m .w .(* mockWriter )
131123 w .clearChan <- struct {}{}
132124}
133-
134- type waitingMockWriter struct {
135- logChan chan string
136- }
137-
138- // newWaitingMockWriter returns a new waitingMockWriter
139- func newWaitingMockWriter () * waitingMockWriter {
140- logChan := make (chan string , 1000 )
141- return & waitingMockWriter {
142- logChan ,
143- }
144- }
145-
146- func (m * waitingMockWriter ) logAtLevel (p syslog.Priority , msg string , a ... any ) {
147- m .logChan <- fmt .Sprintf ("%s: %s" , levelName [p & 7 ], fmt .Sprintf (msg , a ... ))
148- }
149-
150- // WaitForMatch returns the first log line matching a regex. It accepts a
151- // regexp string and timeout. If the timeout value is met before the
152- // matching pattern is read from the channel, an error is returned.
153- func (m * WaitingMock ) WaitForMatch (reString string , timeout time.Duration ) (string , error ) {
154- w := m .w .(* waitingMockWriter )
155- deadline := time .After (timeout )
156- re := regexp .MustCompile (reString )
157- for {
158- select {
159- case logLine := <- w .logChan :
160- if re .MatchString (logLine ) {
161- close (w .logChan )
162- return logLine , nil
163- }
164- case <- deadline :
165- return "" , fmt .Errorf ("timeout waiting for match: %q" , reString )
166- }
167- }
168- }
0 commit comments