From 26d8870cab8df6aa1a4cebe0dfbc842c6e0de8cc Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Tue, 12 Jan 2021 21:26:28 -0500 Subject: [PATCH] Improve test coverage --- client/client.go | 4 +++- client/client_test.go | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/client/client.go b/client/client.go index 79ad2350..ded131e8 100644 --- a/client/client.go +++ b/client/client.go @@ -12,6 +12,8 @@ import ( var ( secureHTTPClient *http.Client insecureHTTPClient *http.Client + + pingTimeout = 5 * time.Second ) // GetHTTPClient returns the shared HTTP client @@ -62,7 +64,7 @@ func Ping(address string) (bool, time.Duration) { return false, 0 } pinger.Count = 1 - pinger.Timeout = 5 * time.Second + pinger.Timeout = pingTimeout pinger.SetNetwork("ip4") pinger.SetPrivileged(true) err = pinger.Run() diff --git a/client/client_test.go b/client/client_test.go index 33268959..75632063 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2,6 +2,7 @@ package client import ( "testing" + "time" ) func TestGetHTTPClient(t *testing.T) { @@ -28,6 +29,7 @@ func TestGetHTTPClient(t *testing.T) { } func TestPing(t *testing.T) { + pingTimeout = time.Second if success, rtt := Ping("127.0.0.1"); !success { t.Error("expected true") if rtt == 0 { @@ -35,7 +37,13 @@ func TestPing(t *testing.T) { } } 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 { t.Error("Round-trip time returned on failure should've been 0") }