From 80f4bbae3fdc239d780b67ea4e5b63bdd6da4f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 10 May 2022 19:28:39 +0800 Subject: [PATCH] fix #245: tcping error handling --- utils/tcping.go | 10 +++------- utils/tcping_test.go | 11 ++++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/utils/tcping.go b/utils/tcping.go index 4b899673..97c8dfb1 100644 --- a/utils/tcping.go +++ b/utils/tcping.go @@ -2,7 +2,6 @@ package utils import ( "net" - "strings" "time" ) @@ -47,13 +46,10 @@ func RunTCPPingLoop(ipport string, count int) (r ICMPPingResult) { func tcping(ipport string) (int64, error) { t := time.Now().UnixMilli() - conn, err := net.DialTimeout("tcp", ipport, time.Second*2) + conn, err := net.DialTimeout("tcp", ipport, time.Second*10) if err != nil { - if strings.Contains(err.Error(), "timeout") { - return 9999, err - } - } else { - _ = conn.Close() + return 9999, err } + _ = conn.Close() return time.Now().UnixMilli() - t, nil } diff --git a/utils/tcping_test.go b/utils/tcping_test.go index 6ad4c42c..2567021d 100644 --- a/utils/tcping_test.go +++ b/utils/tcping_test.go @@ -2,11 +2,16 @@ package utils import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestPing(t *testing.T) { r := RunTCPPingLoop("127.0.0.1:23333", 4) - if r.PacketsLoss == r.PacketsSent { - t.Fatal(r) - } + assert.Equal(t, 4, r.PacketsLoss) + assert.Equal(t, 4, r.PacketsSent) + r = RunTCPPingLoop("114.114.114.114:53", 4) + assert.Equal(t, 0, r.PacketsLoss) + assert.Equal(t, 4, r.PacketsSent) + t.Log(r.AvgTimeMill) }