fix data races in controller

This commit is contained in:
Nan Deng 2014-07-02 17:30:56 -07:00
parent bf44347340
commit e13e31866d

View File

@ -18,7 +18,6 @@ package tools
import (
"fmt"
"sync"
"testing"
"github.com/coreos/go-etcd/etcd"
@ -30,8 +29,7 @@ type EtcdResponseWithError struct {
}
type FakeEtcdClient struct {
condWatchCompleted *sync.Cond
condLock sync.Mutex
watchCompletedChan chan bool
Data map[string]EtcdResponseWithError
DeletedKeys []string
@ -59,12 +57,11 @@ func MakeFakeEtcdClient(t *testing.T) *FakeEtcdClient {
// They are only available when Watch() is called. If users of
// FakeEtcdClient want to use any of these channels, they have to call
// WaitForWatchCompletion before any operation on these channels.
// Internally, FakeEtcdClient use condWatchCompleted to indicate if the
// Internally, FakeEtcdClient use watchCompletedChan to indicate if the
// Watch() method has been called. WaitForWatchCompletion() will wait
// on condWatchCompleted. By the end of the Watch() method, it will
// call Broadcast() on condWatchCompleted, which will awake any
// goroutine waiting on this condition.
ret.condWatchCompleted = sync.NewCond(&ret.condLock)
// on this channel. WaitForWatchCompletion() will return only when
// WatchResponse, WatchInjectError and WatchStop are ready to read/write.
ret.watchCompletedChan = make(chan bool)
return ret
}
@ -116,9 +113,7 @@ func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, err
}
func (f *FakeEtcdClient) WaitForWatchCompletion() {
f.condLock.Lock()
defer f.condLock.Unlock()
f.condWatchCompleted.Wait()
<-f.watchCompletedChan
}
func (f *FakeEtcdClient) Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error) {
@ -129,8 +124,7 @@ func (f *FakeEtcdClient) Watch(prefix string, waitIndex uint64, recursive bool,
defer close(injectedError)
f.WatchInjectError = injectedError
f.condWatchCompleted.Broadcast()
f.watchCompletedChan <- true
select {
case <-stop:
return nil, etcd.ErrWatchStoppedByUser