Закрывать listener API, если сервер не стартовал после его открытия
This commit is contained in:
+28
-4
@@ -40,6 +40,31 @@ func TestMain(m *testing.M) {
|
||||
// A start-up failure after the background work began must still end the
|
||||
// process with an error, not leave it waiting for a signal.
|
||||
func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) {
|
||||
if err := runWithBusyDebugAddr(t, 0); err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedStartReleasesTheAPIPort(t *testing.T) {
|
||||
probe, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port := probe.Addr().(*net.TCPAddr).Port
|
||||
probe.Close()
|
||||
|
||||
if err := runWithBusyDebugAddr(t, port); err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
t.Fatalf("the API port is still taken after the failed start: %v", err)
|
||||
}
|
||||
ln.Close()
|
||||
}
|
||||
|
||||
func runWithBusyDebugAddr(t *testing.T, port int) error {
|
||||
t.Helper()
|
||||
busy, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -47,7 +72,7 @@ func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) {
|
||||
defer busy.Close()
|
||||
cfg := config.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 0,
|
||||
Port: port,
|
||||
ShutdownTimeout: time.Second,
|
||||
MaxBodyBytes: 1024,
|
||||
DatabaseURL: db.DSN,
|
||||
@@ -63,10 +88,9 @@ func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) {
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
return err
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("run hung instead of returning the listen error")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user