Skip to content

Commit 6422d1f

Browse files
committed
Added t.parallel() to logger_test.go functions and used goroutines in logger.go LogMatcher function
Signed-off-by: Insomniac2904 <[email protected]>
1 parent 640615e commit 6422d1f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

pkg/testutils/logger.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,30 @@ func (b *Buffer) Write(p []byte) (int, error) {
9393
return b.Buffer.Write(p)
9494
}
9595

96-
// LogMatcher is a helper func that returns true if the subStr appears more than 'occurrences' times in the logs.
9796
var LogMatcher = func(occurrences int, subStr string, logs []string) (bool, string) {
9897
errMsg := fmt.Sprintf("subStr '%s' does not occur %d time(s) in %v", subStr, occurrences, logs)
9998
if len(logs) < occurrences {
10099
return false, errMsg
101100
}
101+
102+
// Count occurrences in parallel
102103
var count int
104+
var wg sync.WaitGroup
105+
var mu sync.Mutex
106+
103107
for _, log := range logs {
104-
if strings.Contains(log, subStr) {
105-
count++
106-
}
108+
wg.Add(1)
109+
go func(log string) {
110+
defer wg.Done()
111+
if strings.Contains(log, subStr) {
112+
mu.Lock()
113+
count++
114+
mu.Unlock()
115+
}
116+
}(log)
107117
}
118+
wg.Wait()
119+
108120
if count >= occurrences {
109121
return true, ""
110122
}

pkg/testutils/logger_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
func TestNewLogger(t *testing.T) {
17+
t.Parallel()
1718
logger, log := NewLogger()
1819
logger.Warn("hello", zap.String("x", "y"))
1920

@@ -26,11 +27,13 @@ func TestNewLogger(t *testing.T) {
2627
}
2728

2829
func TestNewEchoLogger(t *testing.T) {
30+
t.Parallel()
2931
logger, _ := NewEchoLogger(t)
3032
logger.Warn("hello", zap.String("x", "y"))
3133
}
3234

3335
func TestJSONLineError(t *testing.T) {
36+
t.Parallel()
3437
log := &Buffer{}
3538
log.WriteString("bad-json\n")
3639
_, ok := log.JSONLine(0)["error"]
@@ -64,6 +67,7 @@ func TestRaceCondition(*testing.T) {
6467
}
6568

6669
func TestLogMatcher(t *testing.T) {
70+
t.Parallel()
6771
tests := []struct {
6872
occurrences int
6973
subStr string
@@ -80,6 +84,7 @@ func TestLogMatcher(t *testing.T) {
8084
for i, tt := range tests {
8185
test := tt
8286
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
87+
t.Parallel()
8388
match, errMsg := LogMatcher(test.occurrences, test.subStr, test.logs)
8489
assert.Equal(t, test.expected, match)
8590
assert.Equal(t, test.errMsg, errMsg)

0 commit comments

Comments
 (0)