Improve test coverage

This commit is contained in:
TwinProduction 2021-01-12 21:26:28 -05:00
parent aec867ae69
commit 26d8870cab
2 changed files with 12 additions and 2 deletions

View File

@ -12,6 +12,8 @@ import (
var ( var (
secureHTTPClient *http.Client secureHTTPClient *http.Client
insecureHTTPClient *http.Client insecureHTTPClient *http.Client
pingTimeout = 5 * time.Second
) )
// GetHTTPClient returns the shared HTTP client // GetHTTPClient returns the shared HTTP client
@ -62,7 +64,7 @@ func Ping(address string) (bool, time.Duration) {
return false, 0 return false, 0
} }
pinger.Count = 1 pinger.Count = 1
pinger.Timeout = 5 * time.Second pinger.Timeout = pingTimeout
pinger.SetNetwork("ip4") pinger.SetNetwork("ip4")
pinger.SetPrivileged(true) pinger.SetPrivileged(true)
err = pinger.Run() err = pinger.Run()

View File

@ -2,6 +2,7 @@ package client
import ( import (
"testing" "testing"
"time"
) )
func TestGetHTTPClient(t *testing.T) { func TestGetHTTPClient(t *testing.T) {
@ -28,6 +29,7 @@ func TestGetHTTPClient(t *testing.T) {
} }
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
pingTimeout = time.Second
if success, rtt := Ping("127.0.0.1"); !success { if success, rtt := Ping("127.0.0.1"); !success {
t.Error("expected true") t.Error("expected true")
if rtt == 0 { if rtt == 0 {
@ -35,7 +37,13 @@ func TestPing(t *testing.T) {
} }
} }
if success, rtt := Ping("256.256.256.256"); success { if success, rtt := Ping("256.256.256.256"); success {
t.Error("expected false") t.Error("expected false, because the IP is invalid")
if rtt != 0 {
t.Error("Round-trip time returned on failure should've been 0")
}
}
if success, rtt := Ping("192.168.152.153"); success {
t.Error("expected false, because the IP is valid but the host should be unreachable")
if rtt != 0 { if rtt != 0 {
t.Error("Round-trip time returned on failure should've been 0") t.Error("Round-trip time returned on failure should've been 0")
} }