1
0
mirror of https://github.com/golang/go synced 2024-11-25 12:07:56 -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:
Jan Ziak 2012-06-24 19:22:48 -04:00 committed by Russ Cox
parent e34079bb59
commit f5f3c3fe09

View File

@ -9,6 +9,7 @@ package inotify
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"sync/atomic"
"testing" "testing"
"time" "time"
) )
@ -43,13 +44,13 @@ func TestInotifyEvents(t *testing.T) {
// Receive events on the event channel on a separate goroutine // Receive events on the event channel on a separate goroutine
eventstream := watcher.Event eventstream := watcher.Event
var eventsReceived = 0 var eventsReceived int32 = 0
done := make(chan bool) done := make(chan bool)
go func() { go func() {
for event := range eventstream { for event := range eventstream {
// Only count relevant events // Only count relevant events
if event.Name == testFile { if event.Name == testFile {
eventsReceived++ atomic.AddInt32(&eventsReceived, 1)
t.Logf("event received: %s", event) t.Logf("event received: %s", event)
} else { } else {
t.Logf("unexpected event received: %s", event) 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 // We expect this event to be received almost immediately, but let's wait 1 s to be sure
time.Sleep(1 * time.Second) 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") t.Fatal("inotify event hasn't been received after 1 second")
} }