diff --git a/.travis.yml b/.travis.yml index 691d930..db9fcfb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,9 @@ os: - osx language: go go: + - "master" - "1.11.x" install: - - go get "github.com/pkg/errors" \ No newline at end of file + - go get "github.com/pkg/errors" +script: go test -v -bench=. -benchmem ./... + diff --git a/checker_test.go b/checker_test.go index 97eb310..daee22c 100644 --- a/checker_test.go +++ b/checker_test.go @@ -3,8 +3,10 @@ package tcp import ( "context" "fmt" + "net" "net/http" "net/http/httptest" + "os" "runtime" "sync" "testing" @@ -12,10 +14,33 @@ import ( ) const ( - AddrDead = "127.0.0.1:1" - AddrTimeout = "10.255.255.1:80" + AddrDead = "127.0.0.1:1" ) +var timeoutAddrs = []string{ + "10.255.255.1:80", + "10.0.0.0:1", +} +var AddrTimeout = timeoutAddrs[0] + +func _setAddrTimeout() { + for _, addr := range timeoutAddrs { + conn, err := net.DialTimeout("tcp", addr, time.Millisecond*50) + if err == nil { + conn.Close() + continue + } + if os.IsTimeout(err) { + AddrTimeout = addr + return + } + } +} + +func init() { + _setAddrTimeout() +} + // assert calls t.Fatal if the result is false func assert(t *testing.T, result bool) { if !result { diff --git a/result_pipes_linux_test.go b/result_pipes_linux_test.go index b4f57bf..2e8528a 100644 --- a/result_pipes_linux_test.go +++ b/result_pipes_linux_test.go @@ -26,7 +26,8 @@ func _benchmarkChecker(b *testing.B, c *Checker, addr string) { b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - c.CheckAddr(addr, time.Second) + // if timeout >= 1s, this func will run only once in the benchmark. + c.CheckAddr(addr, time.Millisecond*900) } }) b.StopTimer()