change FakeWatcher.Stopped to be a private field, as read it directly may cause Read/Write conflict race

This commit is contained in:
chenjun.cj 2019-12-10 20:22:18 +08:00
parent 34f349257f
commit a8839a9735

View File

@ -90,7 +90,7 @@ func (w emptyWatch) ResultChan() <-chan Event {
// FakeWatcher lets you test anything that consumes a watch.Interface; threadsafe. // FakeWatcher lets you test anything that consumes a watch.Interface; threadsafe.
type FakeWatcher struct { type FakeWatcher struct {
result chan Event result chan Event
Stopped bool stopped bool
sync.Mutex sync.Mutex
} }
@ -110,24 +110,24 @@ func NewFakeWithChanSize(size int, blocking bool) *FakeWatcher {
func (f *FakeWatcher) Stop() { func (f *FakeWatcher) Stop() {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
if !f.Stopped { if !f.stopped {
klog.V(4).Infof("Stopping fake watcher.") klog.V(4).Infof("Stopping fake watcher.")
close(f.result) close(f.result)
f.Stopped = true f.stopped = true
} }
} }
func (f *FakeWatcher) IsStopped() bool { func (f *FakeWatcher) IsStopped() bool {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.Stopped return f.stopped
} }
// Reset prepares the watcher to be reused. // Reset prepares the watcher to be reused.
func (f *FakeWatcher) Reset() { func (f *FakeWatcher) Reset() {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.Stopped = false f.stopped = false
f.result = make(chan Event) f.result = make(chan Event)
} }