fix data races in controller

This commit is contained in:
Nan Deng
2014-06-30 14:48:57 -07:00
parent 655bca7e55
commit 62055090b4
2 changed files with 39 additions and 11 deletions

View File

@@ -435,17 +435,18 @@ func TestWatchControllers(t *testing.T) {
fakeEtcd := util.MakeFakeEtcdClient(t)
manager := MakeReplicationManager(fakeEtcd, nil)
var testControllerSpec api.ReplicationController
receivedCount := 0
received := make(chan bool)
manager.syncHandler = func(controllerSpec api.ReplicationController) error {
if !reflect.DeepEqual(controllerSpec, testControllerSpec) {
t.Errorf("Expected %#v, but got %#v", testControllerSpec, controllerSpec)
}
receivedCount++
close(received)
return nil
}
go manager.watchControllers()
time.Sleep(10 * time.Millisecond)
fakeEtcd.WaitToWatch()
// Test normal case
testControllerSpec.ID = "foo"
@@ -456,14 +457,14 @@ func TestWatchControllers(t *testing.T) {
},
}
time.Sleep(10 * time.Millisecond)
if receivedCount != 1 {
t.Errorf("Expected 1 call but got %v", receivedCount)
select {
case <-received:
case <-time.After(10 * time.Millisecond):
t.Errorf("Expected 1 call but got 0")
}
// Test error case
fakeEtcd.WatchInjectError <- fmt.Errorf("Injected error")
time.Sleep(10 * time.Millisecond)
// Did everything shut down?
if _, open := <-fakeEtcd.WatchResponse; open {
@@ -472,9 +473,8 @@ func TestWatchControllers(t *testing.T) {
// Test purposeful shutdown
go manager.watchControllers()
time.Sleep(10 * time.Millisecond)
fakeEtcd.WaitToWatch()
fakeEtcd.WatchStop <- true
time.Sleep(10 * time.Millisecond)
// Did everything shut down?
if _, open := <-fakeEtcd.WatchResponse; open {