From d15de72a92c8841d069b1265e433eb52edc29822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 25 Aug 2015 22:51:05 -0700 Subject: [PATCH] Fix race condition in controller_test Our WaitGroup.Add() call might happen after some WaitGroup.Done() calls done by the controller, so make sure that doesn't happen by doing the Add() calls before letting the controller run. --- FAIL: TestUpdate (2.00s) panic: sync: WaitGroup is reused before previous Wait has returned [recovered] panic: sync: WaitGroup is reused before previous Wait has returned --- pkg/controller/framework/controller_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/controller/framework/controller_test.go b/pkg/controller/framework/controller_test.go index 619b994accf..e5d4fb1a7a0 100644 --- a/pkg/controller/framework/controller_test.go +++ b/pkg/controller/framework/controller_test.go @@ -335,10 +335,6 @@ func TestUpdate(t *testing.T) { }, ) - // Run the controller and run it until we close stop. - stop := make(chan struct{}) - go controller.Run(stop) - pod := func(name, check string) *api.Pod { return &api.Pod{ ObjectMeta: api.ObjectMeta{ @@ -371,11 +367,18 @@ func TestUpdate(t *testing.T) { }, } - // run every test a few times, in parallel const threads = 3 + testDoneWG.Add(threads * len(tests)) + + // Run the controller and run it until we close stop. + // Once Run() is called, calls to testDoneWG.Done() might start, so + // all testDoneWG.Add() calls must happen before this point + stop := make(chan struct{}) + go controller.Run(stop) + + // run every test a few times, in parallel var wg sync.WaitGroup wg.Add(threads * len(tests)) - testDoneWG.Add(threads * len(tests)) for i := 0; i < threads; i++ { for j, f := range tests { go func(name string, f func(string)) {