diff --git a/pkg/tools/fake_etcd_client.go b/pkg/tools/fake_etcd_client.go index 47509bdff8a..dc08b9c3c5a 100644 --- a/pkg/tools/fake_etcd_client.go +++ b/pkg/tools/fake_etcd_client.go @@ -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