Add debugging logs to freeport

This commit is contained in:
Chris S. Kim 2022-08-08 13:59:35 -04:00 committed by Chris S. Kim
parent 0455b5e720
commit b374250d08
3 changed files with 8 additions and 13 deletions

View File

@ -195,6 +195,8 @@ func checkFreedPortsOnce() {
if used := isPortInUse(port); !used {
freePorts.PushBack(port)
remove = append(remove, elem)
} else {
logf("WARN", "port %d still being used", port)
}
}
@ -314,7 +316,6 @@ func Take(n int) (ports []int, err error) {
ports = append(ports, port)
}
// logf("DEBUG", "free ports: %v", ports)
return ports, nil
}
@ -400,9 +401,10 @@ func logf(severity string, format string, a ...interface{}) {
// In the future new methods may be added to this interface, but those methods
// should always be implemented by *testing.T
type TestingT interface {
Cleanup(func())
Helper()
Fatalf(format string, args ...interface{})
Cleanup(func())
Name() string
}
// GetN returns n free ports from the reserved port block, and returns the
@ -413,8 +415,10 @@ func GetN(t TestingT, n int) []int {
if err != nil {
t.Fatalf("failed to take %v ports: %w", n, err)
}
logf("DEBUG", "Test %q took ports %v", t.Name(), ports)
t.Cleanup(func() {
Return(ports)
logf("DEBUG", "Test %q returned ports %v", t.Name(), ports)
})
return ports
}

View File

@ -156,10 +156,7 @@ func defaultServerConfig(t TestingTB) *TestServerConfig {
panic(err)
}
ports, err := freeport.Take(7)
if err != nil {
t.Fatalf("failed to take ports: %v", err)
}
ports := freeport.GetN(t, 7)
logBuffer := NewLogBuffer(t)
@ -194,9 +191,6 @@ func defaultServerConfig(t TestingTB) *TestServerConfig {
"cluster_id": "11111111-2222-3333-4444-555555555555",
},
},
ReturnPorts: func() {
freeport.Return(ports)
},
Stdout: logBuffer,
Stderr: logBuffer,
Peering: &TestPeeringConfig{Enabled: true},
@ -271,7 +265,6 @@ func NewTestServerConfigT(t TestingTB, cb ServerConfigCallback) (*TestServer, er
b, err := json.Marshal(cfg)
if err != nil {
cfg.ReturnPorts()
os.RemoveAll(tmpdir)
return nil, errors.Wrap(err, "failed marshaling json")
}
@ -279,7 +272,6 @@ func NewTestServerConfigT(t TestingTB, cb ServerConfigCallback) (*TestServer, er
t.Logf("CONFIG JSON: %s", string(b))
configFile := filepath.Join(tmpdir, "config.json")
if err := ioutil.WriteFile(configFile, b, 0644); err != nil {
cfg.ReturnPorts()
os.RemoveAll(tmpdir)
return nil, errors.Wrap(err, "failed writing config content")
}
@ -291,7 +283,6 @@ func NewTestServerConfigT(t TestingTB, cb ServerConfigCallback) (*TestServer, er
cmd.Stdout = cfg.Stdout
cmd.Stderr = cfg.Stderr
if err := cmd.Start(); err != nil {
cfg.ReturnPorts()
os.RemoveAll(tmpdir)
return nil, errors.Wrap(err, "failed starting command")
}
@ -336,7 +327,6 @@ func NewTestServerConfigT(t TestingTB, cb ServerConfigCallback) (*TestServer, er
// Stop stops the test Consul server, and removes the Consul data
// directory once we are done.
func (s *TestServer) Stop() error {
defer s.Config.ReturnPorts()
defer os.RemoveAll(s.tmpdir)
// There was no process

View File

@ -9,4 +9,5 @@ type TestingTB interface {
Logf(format string, args ...interface{})
Name() string
Fatalf(fmt string, args ...interface{})
Helper()
}