Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 3c09824

Browse files
author
Ivan Mirić
committed
Move checking of log message to a separate CacheLogrusHook method
Resolves #130 (comment)
1 parent 165614c commit 3c09824

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

common/remote_object_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,7 @@ func TestParseRemoteObject(t *testing.T) {
256256
assert.Equal(t, tc.expected, val)
257257

258258
if tc.name == "overflow" {
259-
var gotMsg bool
260-
for _, evt := range logHook.Drain() {
261-
if evt.Message == "object will be parsed partially" {
262-
gotMsg = true
263-
}
264-
}
265-
assert.True(t, gotMsg)
259+
assert.True(t, logHook.Contains("object will be parsed partially"))
266260
}
267261
})
268262
}

tests/network_manager_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,5 @@ func TestDataURLSkipRequest(t *testing.T) {
4141

4242
p.Goto("data:text/html,hello", nil)
4343

44-
var gotMsg bool
45-
for _, evt := range logHook.Drain() {
46-
if evt.Message == "skipped request handling of data URL" {
47-
gotMsg = true
48-
}
49-
}
50-
assert.True(t, gotMsg)
44+
assert.True(t, logHook.Contains("skipped request handling of data URL"))
5145
}

testutils/logrus_hook.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package testutils
2222

2323
import (
2424
"io/ioutil"
25+
"strings"
2526
"sync"
2627

2728
"github.com/sirupsen/logrus"
@@ -31,32 +32,45 @@ import (
3132
// if log messages were outputted
3233
type CacheLogrusHook struct {
3334
HookedLevels []logrus.Level
34-
mutex sync.Mutex
35+
mutex sync.RWMutex
3536
messageCache []logrus.Entry
3637
}
3738

3839
// Levels just returns whatever was stored in the HookedLevels slice
39-
func (smh *CacheLogrusHook) Levels() []logrus.Level {
40-
return smh.HookedLevels
40+
func (clh *CacheLogrusHook) Levels() []logrus.Level {
41+
return clh.HookedLevels
4142
}
4243

4344
// Fire saves whatever message the logrus library passed in the cache
44-
func (smh *CacheLogrusHook) Fire(e *logrus.Entry) error {
45-
smh.mutex.Lock()
46-
defer smh.mutex.Unlock()
47-
smh.messageCache = append(smh.messageCache, *e)
45+
func (clh *CacheLogrusHook) Fire(e *logrus.Entry) error {
46+
clh.mutex.Lock()
47+
defer clh.mutex.Unlock()
48+
clh.messageCache = append(clh.messageCache, *e)
4849
return nil
4950
}
5051

5152
// Drain returns the currently stored messages and deletes them from the cache
52-
func (smh *CacheLogrusHook) Drain() []logrus.Entry {
53-
smh.mutex.Lock()
54-
defer smh.mutex.Unlock()
55-
res := smh.messageCache
56-
smh.messageCache = []logrus.Entry{}
53+
func (clh *CacheLogrusHook) Drain() []logrus.Entry {
54+
clh.mutex.Lock()
55+
defer clh.mutex.Unlock()
56+
res := clh.messageCache
57+
clh.messageCache = []logrus.Entry{}
5758
return res
5859
}
5960

61+
// Contains returns true if msg is contained in any of the cached logged events
62+
// or false otherwise.
63+
func (clh *CacheLogrusHook) Contains(msg string) bool {
64+
clh.mutex.RLock()
65+
defer clh.mutex.RUnlock()
66+
for _, evt := range clh.messageCache {
67+
if strings.Contains(evt.Message, msg) {
68+
return true
69+
}
70+
}
71+
return false
72+
}
73+
6074
var _ logrus.Hook = &CacheLogrusHook{}
6175

6276
// LogHook sets logger to DebugLevel, attaches a CacheLogrusHook and returns it.

0 commit comments

Comments
 (0)