Skip to content

Commit

Permalink
Fix race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
miccagiann committed Aug 24, 2022
1 parent 3f6be1a commit 1cedc2b
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions xoptest/testlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ func TestLogMethods(t *testing.T) {
}

func TestWithLock(t *testing.T) {
var err1 error
var err2 error
message := make(chan int, 2)
defer close(message)
wg := sync.WaitGroup{}
invocations := 0
f := func(l *xoptest.TestLogger) error {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
invocations++
message <- invocations
Expand All @@ -60,16 +57,18 @@ func TestWithLock(t *testing.T) {
defer tLog.Close()
wg.Add(1)
go func() {
err1 = tLog.WithLock(f)
defer wg.Done()
err := tLog.WithLock(f)
assert.NoError(t, err)
}()
wg.Add(1)
go func() {
err2 = tLog.WithLock(f)
defer wg.Done()
err := tLog.WithLock(f)
assert.NoError(t, err)
}()

wg.Wait()
assert.NoError(t, err1)
assert.NoError(t, err2)
assert.Equal(t, 1, <-message)
assert.Equal(t, 2, <-message)
}
Expand Down

0 comments on commit 1cedc2b

Please sign in to comment.