Block ports in range 0 to 10000 when running tests (#666)

This commit is contained in:
Egon Elbre 2018-11-19 17:40:44 +02:00 committed by GitHub
parent 4a82c47427
commit 5bcb6fa4fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -39,6 +39,7 @@ matrix:
- popd - popd
- go install -race ./... - go install -race ./...
script: script:
- go run scripts/use-ports.go -from 1024 -to 10000 &
- go test -race -cover -coverprofile=.coverprofile -json ./... | tparse -all - go test -race -cover -coverprofile=.coverprofile -json ./... | tparse -all
- goveralls -coverprofile=.coverprofile -service=travis-ci - goveralls -coverprofile=.coverprofile -service=travis-ci
- rm .coverprofile - rm .coverprofile

49
scripts/use-ports.go Normal file
View File

@ -0,0 +1,49 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
// +build ignore
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"syscall"
)
var (
fromPort = flag.Int("from", 0, "first port")
toPort = flag.Int("to", 10000, "last port")
)
func main() {
flag.Parse()
var listeners []net.Listener
var unableToStart []int
for port := *fromPort; port < *toPort; port++ {
listener, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
if err != nil {
unableToStart = append(unableToStart, port)
continue
}
listeners = append(listeners, listener)
}
fmt.Printf("use-ports: unable to start on %v\n", unableToStart)
fmt.Printf("use-ports: listening on ports %v to %v\n", *fromPort, *toPort)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGQUIT)
<-sigs
for _, listener := range listeners {
err := listener.Close()
if err != nil {
fmt.Printf("unable to close: %v\n", err)
}
}
}