mirror of
https://github.com/golang/go
synced 2024-11-22 00:04:41 -07:00
exp/inotify: prevent data race during testing
Fixes #3714. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6341047
This commit is contained in:
parent
e34079bb59
commit
f5f3c3fe09
@ -9,6 +9,7 @@ package inotify
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -43,13 +44,13 @@ func TestInotifyEvents(t *testing.T) {
|
||||
|
||||
// Receive events on the event channel on a separate goroutine
|
||||
eventstream := watcher.Event
|
||||
var eventsReceived = 0
|
||||
var eventsReceived int32 = 0
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
for event := range eventstream {
|
||||
// Only count relevant events
|
||||
if event.Name == testFile {
|
||||
eventsReceived++
|
||||
atomic.AddInt32(&eventsReceived, 1)
|
||||
t.Logf("event received: %s", event)
|
||||
} else {
|
||||
t.Logf("unexpected event received: %s", event)
|
||||
@ -67,7 +68,7 @@ func TestInotifyEvents(t *testing.T) {
|
||||
|
||||
// We expect this event to be received almost immediately, but let's wait 1 s to be sure
|
||||
time.Sleep(1 * time.Second)
|
||||
if eventsReceived == 0 {
|
||||
if atomic.AddInt32(&eventsReceived, 0) == 0 {
|
||||
t.Fatal("inotify event hasn't been received after 1 second")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user