github.com/avenga/couper@v1.12.2/internal/test/server.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "net" 6 "strconv" 7 "time" 8 ) 9 10 const waitForBackoff = 10 11 12 func WaitForClosedPort(port int) { 13 start := time.Now() 14 round := time.Duration(0) 15 16 for { 17 conn, dialErr := net.Dial("tcp4", ":"+strconv.Itoa(port)) 18 if conn != nil { 19 _ = conn.Close() 20 } 21 if dialErr != nil { 22 break 23 } 24 25 round++ 26 if round == waitForBackoff { 27 panic(fmt.Sprintf("port is still in use after %s: %d", time.Since(start).String(), port)) 28 } 29 time.Sleep(time.Second + (time.Second*round)/2) 30 } 31 } 32 33 func WaitForOpenPort(port int) { 34 start := time.Now() 35 round := time.Duration(0) 36 for { 37 conn, dialErr := net.Dial("tcp4", ":"+strconv.Itoa(port)) 38 if conn != nil { 39 _ = conn.Close() 40 } 41 if dialErr == nil { 42 return 43 } 44 45 time.Sleep(time.Second + (time.Second*round)/2) 46 round++ 47 if round == waitForBackoff { 48 panic(fmt.Sprintf("port is still not listening after %s: %d", time.Since(start).String(), port)) 49 } 50 } 51 }