From 0f0e5db62d485e07105f5401349382692271cf31 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 11 Jun 2018 16:41:02 +0200 Subject: vm/adb: don't fail if port 35099 is busy --- vm/vmimpl/vmimpl.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'vm/vmimpl/vmimpl.go') diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go index a4dba2f65..532611abd 100644 --- a/vm/vmimpl/vmimpl.go +++ b/vm/vmimpl/vmimpl.go @@ -11,6 +11,8 @@ import ( "errors" "fmt" "io" + "math/rand" + "net" "os/exec" "time" @@ -137,3 +139,18 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t }() return merger.Output, errc, nil } + +func RandomPort() int { + return rand.Intn(64<<10-1<<10) + 1<<10 +} + +func UnusedTCPPort() int { + for { + port := RandomPort() + ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", port)) + if err == nil { + ln.Close() + return port + } + } +} -- cgit mrf-deployment