Skip to content

Commit 1eb1803

Browse files
committed
Fix race conditions reported by go test -race
1 parent e2ba841 commit 1eb1803

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

tailer/bufferedTailer_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ func TestLineBufferSequential(t *testing.T) {
6464
if stillOpen {
6565
t.Error("Source tailer was not closed.")
6666
}
67-
if !metric.registerCalled {
67+
registerCalled, unregisterCalled := metric.Get()
68+
if !registerCalled {
6869
t.Error("metric.Register() not called.")
6970
}
70-
if !metric.unregisterCalled {
71+
if !unregisterCalled {
7172
t.Error("metric.Unregister() not called.")
7273
}
7374
// The peak load should be 9999 or 9998, depending on how quick
@@ -119,31 +120,45 @@ func TestLineBufferParallel(t *testing.T) {
119120
if stillOpen {
120121
t.Error("Source tailer was not closed.")
121122
}
122-
if !metric.registerCalled {
123+
registerCalled, unregisterCalled := metric.Get()
124+
if !registerCalled {
123125
t.Error("metric.Register() not called.")
124126
}
125-
if !metric.unregisterCalled {
127+
if !unregisterCalled {
126128
t.Error("metric.Unregister() not called.")
127129
}
128130
// Should be much less than 10000, because consumer and producer work in parallel.
129131
fmt.Printf("peak load: %v\n", metric.peakLoad)
130132
}
131133

132134
type peakLoadMetric struct {
135+
lock sync.Mutex
133136
registerCalled, unregisterCalled bool
134137
peakLoad float64
135138
}
136139

137140
func (m *peakLoadMetric) Register() {
141+
m.lock.Lock()
138142
m.registerCalled = true
143+
m.lock.Unlock()
139144
}
140145

141146
func (m *peakLoadMetric) Observe(currentLoad float64) {
147+
m.lock.Lock()
142148
if currentLoad > m.peakLoad {
143149
m.peakLoad = currentLoad
144150
}
151+
m.lock.Unlock()
145152
}
146153

147154
func (m *peakLoadMetric) Unregister() {
155+
m.lock.Lock()
148156
m.unregisterCalled = true
157+
m.lock.Unlock()
158+
}
159+
160+
func (m *peakLoadMetric) Get() (bool, bool) {
161+
m.lock.Lock()
162+
defer m.lock.Unlock()
163+
return m.registerCalled, m.unregisterCalled
149164
}

tailer/fswatcher_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"runtime/pprof"
3030
"strconv"
3131
"strings"
32+
"sync"
3233
"testing"
3334
"time"
3435
)
@@ -233,6 +234,7 @@ type context struct {
233234
log logrus.FieldLogger
234235
tailer fswatcher.FileTailer
235236
lines map[string]chan string
237+
linesLock sync.Mutex
236238
}
237239

238240
func exec(t *testing.T, ctx *context, cmd []string) {
@@ -503,12 +505,14 @@ func startFileTailer(t *testing.T, ctx *context, params []string) {
503505
if !open {
504506
return // tailer closed
505507
}
508+
ctx.linesLock.Lock()
506509
c, ok := ctx.lines[line.File]
507510
if !ok {
508511
c = make(chan string)
509512
ctx.log.Debugf("adding lines channel for %v", line.File)
510513
ctx.lines[line.File] = c
511514
}
515+
ctx.linesLock.Unlock()
512516
c <- line.Line
513517
case err, open := <-tailer.Errors():
514518
if !open {
@@ -525,10 +529,12 @@ func startFileTailer(t *testing.T, ctx *context, params []string) {
525529
}
526530

527531
func expect(t *testing.T, ctx *context, line string, file string) {
528-
var (
529-
timeout = 5 * time.Second
530-
c = ctx.lines[filepath.Join(ctx.basedir, file)]
531-
)
532+
var timeout = 5 * time.Second
533+
534+
ctx.linesLock.Lock()
535+
c := ctx.lines[filepath.Join(ctx.basedir, file)]
536+
ctx.linesLock.Unlock()
537+
532538
for c == nil {
533539
time.Sleep(100 * time.Millisecond)
534540
timeout = timeout - 10*time.Millisecond
@@ -537,7 +543,9 @@ func expect(t *testing.T, ctx *context, line string, file string) {
537543
return
538544
}
539545
ctx.log.Debugf("waiting for lines channel for %v", filepath.Join(ctx.basedir, file))
546+
ctx.linesLock.Lock()
540547
c = ctx.lines[filepath.Join(ctx.basedir, file)]
548+
ctx.linesLock.Unlock()
541549
}
542550
select {
543551
case l := <-c:

0 commit comments

Comments
 (0)