consul: fix data race in leader CA tests

Some global variables are patched to shorter values in these tests. But the goroutines that read
them can outlive the test because nothing waited for them to exit.

This commit adds a Wait() method to the routine manager, so that tests can wait for the goroutines
to exit. This prevents the data race because the 'reset to original value' can happen
after all other goroutines have stopped.
This commit is contained in:
Daniel Nephin 2021-06-02 18:59:31 -04:00
parent 4a92f2d428
commit baa2b8628e
2 changed files with 28 additions and 6 deletions

View File

@ -373,7 +373,10 @@ func TestLeader_Vault_PrimaryCA_IntermediateRenew(t *testing.T) {
}
})
defer os.RemoveAll(dir1)
defer s1.Shutdown()
defer func() {
s1.Shutdown()
s1.leaderRoutineManager.Wait()
}()
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -482,7 +485,10 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
}
})
defer os.RemoveAll(dir1)
defer s1.Shutdown()
defer func() {
s1.Shutdown()
s1.leaderRoutineManager.Wait()
}()
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -493,7 +499,10 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
c.Build = "1.6.0"
})
defer os.RemoveAll(dir2)
defer s2.Shutdown()
defer func() {
s2.Shutdown()
s2.leaderRoutineManager.Wait()
}()
// Create the WAN link
joinWAN(t, s2, s1)

View File

@ -24,6 +24,10 @@ func (r *routineTracker) running() bool {
}
}
func (r *routineTracker) wait() {
<-r.stoppedCh
}
type Manager struct {
lock sync.RWMutex
logger hclog.Logger
@ -131,6 +135,8 @@ func (m *Manager) stopInstance(name string) *routineTracker {
return instance
}
// StopAll goroutines. Once StopAll is called, it is no longer safe to add no
// goroutines to the Manager.
func (m *Manager) StopAll() {
m.lock.Lock()
defer m.lock.Unlock()
@ -142,7 +148,14 @@ func (m *Manager) StopAll() {
m.logger.Debug("stopping routine", "routine", name)
routine.cancel()
}
// just wipe out the entire map
m.routines = make(map[string]*routineTracker)
}
// Wait for all goroutines to stop after StopAll is called.
func (m *Manager) Wait() {
m.lock.Lock()
defer m.lock.Unlock()
for _, routine := range m.routines {
routine.wait()
}
}