fix tests flaky due to timeouts

This commit is contained in:
Daniel Smith 2015-04-13 15:16:59 -07:00
parent a0fa592b80
commit 1ec6b02e99

View File

@ -45,8 +45,7 @@ func Example() {
fifo := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, downstream) fifo := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, downstream)
// Let's do threadsafe output to get predictable test results. // Let's do threadsafe output to get predictable test results.
outputSetLock := sync.Mutex{} deletionCounter := make(chan string, 1000)
outputSet := util.StringSet{}
cfg := &framework.Config{ cfg := &framework.Config{
Queue: fifo, Queue: fifo,
@ -68,6 +67,7 @@ func Example() {
return err return err
} }
// Delete this object.
source.Delete(newest.Object.(runtime.Object)) source.Delete(newest.Object.(runtime.Object))
} else { } else {
// Update our downstream store. // Update our downstream store.
@ -83,10 +83,8 @@ func Example() {
return err return err
} }
// Record some output. // Report this deletion.
outputSetLock.Lock() deletionCounter <- key
defer outputSetLock.Unlock()
outputSet.Insert(key)
} }
return nil return nil
}, },
@ -94,20 +92,23 @@ func Example() {
// Create the controller and run it until we close stop. // Create the controller and run it until we close stop.
stop := make(chan struct{}) stop := make(chan struct{})
defer close(stop)
go framework.New(cfg).Run(stop) go framework.New(cfg).Run(stop)
// Let's add a few objects to the source. // Let's add a few objects to the source.
for _, name := range []string{"a-hello", "b-controller", "c-framework"} { testIDs := []string{"a-hello", "b-controller", "c-framework"}
for _, name := range testIDs {
// Note that these pods are not valid-- the fake source doesn't // Note that these pods are not valid-- the fake source doesn't
// call validation or anything. // call validation or anything.
source.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: name}}) source.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: name}})
} }
// Let's wait for the controller to process the things we just added. // Let's wait for the controller to process the things we just added.
time.Sleep(500 * time.Millisecond) outputSet := util.StringSet{}
close(stop) for i := 0; i < len(testIDs); i++ {
outputSet.Insert(<-deletionCounter)
}
outputSetLock.Lock()
for _, key := range outputSet.List() { for _, key := range outputSet.List() {
fmt.Println(key) fmt.Println(key)
} }
@ -122,8 +123,7 @@ func ExampleInformer() {
source := framework.NewFakeControllerSource() source := framework.NewFakeControllerSource()
// Let's do threadsafe output to get predictable test results. // Let's do threadsafe output to get predictable test results.
outputSetLock := sync.Mutex{} deletionCounter := make(chan string, 1000)
outputSet := util.StringSet{}
// Make a controller that immediately deletes anything added to it, and // Make a controller that immediately deletes anything added to it, and
// logs anything deleted. // logs anything deleted.
@ -141,30 +141,31 @@ func ExampleInformer() {
key = "oops something went wrong with the key" key = "oops something went wrong with the key"
} }
// Record some output when items are deleted. // Report this deletion.
outputSetLock.Lock() deletionCounter <- key
defer outputSetLock.Unlock()
outputSet.Insert(key)
}, },
}, },
) )
// Run the controller and run it until we close stop. // Run the controller and run it until we close stop.
stop := make(chan struct{}) stop := make(chan struct{})
defer close(stop)
go controller.Run(stop) go controller.Run(stop)
// Let's add a few objects to the source. // Let's add a few objects to the source.
for _, name := range []string{"a-hello", "b-controller", "c-framework"} { testIDs := []string{"a-hello", "b-controller", "c-framework"}
for _, name := range testIDs {
// Note that these pods are not valid-- the fake source doesn't // Note that these pods are not valid-- the fake source doesn't
// call validation or perform any other checking. // call validation or anything.
source.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: name}}) source.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: name}})
} }
// Let's wait for the controller to process the things we just added. // Let's wait for the controller to process the things we just added.
time.Sleep(500 * time.Millisecond) outputSet := util.StringSet{}
close(stop) for i := 0; i < len(testIDs); i++ {
outputSet.Insert(<-deletionCounter)
}
outputSetLock.Lock()
for _, key := range outputSet.List() { for _, key := range outputSet.List() {
fmt.Println(key) fmt.Println(key)
} }