testing: use t.Cleanup in TestAgent for returnPorts

pull/8509/head
Daniel Nephin 2020-08-13 17:09:37 -04:00
parent 4e8e0de8f0
commit b1679508d4
1 changed files with 4 additions and 20 deletions

View File

@ -58,12 +58,9 @@ type TestAgent struct {
// the callers responsibility to clean up the data directory. // the callers responsibility to clean up the data directory.
// Otherwise, a temporary data directory is created and removed // Otherwise, a temporary data directory is created and removed
// when Shutdown() is called. // when Shutdown() is called.
// TODO:
Config *config.RuntimeConfig Config *config.RuntimeConfig
// returnPortsFn will put the ports claimed for the test back into the
// general freeport pool
returnPortsFn func()
// LogOutput is the sink for the logs. If nil, logs are written // LogOutput is the sink for the logs. If nil, logs are written
// to os.Stderr. // to os.Stderr.
LogOutput io.Writer LogOutput io.Writer
@ -71,9 +68,11 @@ type TestAgent struct {
// DataDir is the data directory which is used when Config.DataDir // DataDir is the data directory which is used when Config.DataDir
// is not set. It is created automatically and removed when // is not set. It is created automatically and removed when
// Shutdown() is called. // Shutdown() is called.
// TODO:
DataDir string DataDir string
// Key is the optional encryption key for the LAN and WAN keyring. // Key is the optional encryption key for the LAN and WAN keyring.
// TODO:
Key string Key string
// UseTLS, if true, will disable the HTTP port and enable the HTTPS // UseTLS, if true, will disable the HTTP port and enable the HTTPS
@ -198,7 +197,7 @@ func (a *TestAgent) Start(t *testing.T) (err error) {
}) })
portsConfig, returnPortsFn := randomPortsSource(a.UseTLS) portsConfig, returnPortsFn := randomPortsSource(a.UseTLS)
a.returnPortsFn = returnPortsFn t.Cleanup(returnPortsFn)
nodeID := NodeID() nodeID := NodeID()
@ -221,13 +220,6 @@ func (a *TestAgent) Start(t *testing.T) (err error) {
), ),
} }
defer func() {
if err != nil && a.returnPortsFn != nil {
a.returnPortsFn()
a.returnPortsFn = nil
}
}()
// write the keyring // write the keyring
if a.Key != "" { if a.Key != "" {
writeKey := func(key, filename string) error { writeKey := func(key, filename string) error {
@ -357,14 +349,6 @@ func (a *TestAgent) Shutdown() error {
return nil return nil
} }
// Return ports last of all
defer func() {
if a.returnPortsFn != nil {
a.returnPortsFn()
a.returnPortsFn = nil
}
}()
// shutdown agent before endpoints // shutdown agent before endpoints
defer a.Agent.ShutdownEndpoints() defer a.Agent.ShutdownEndpoints()
if err := a.Agent.ShutdownAgent(); err != nil { if err := a.Agent.ShutdownAgent(); err != nil {