1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

fix #245: tcping error handling

This commit is contained in:
源文雨 2022-05-10 19:28:39 +08:00
parent 4164d656d8
commit 80f4bbae3f
2 changed files with 11 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package utils
import ( import (
"net" "net"
"strings"
"time" "time"
) )
@ -47,13 +46,10 @@ func RunTCPPingLoop(ipport string, count int) (r ICMPPingResult) {
func tcping(ipport string) (int64, error) { func tcping(ipport string) (int64, error) {
t := time.Now().UnixMilli() 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 err != nil {
if strings.Contains(err.Error(), "timeout") {
return 9999, err return 9999, err
} }
} else {
_ = conn.Close() _ = conn.Close()
}
return time.Now().UnixMilli() - t, nil return time.Now().UnixMilli() - t, nil
} }

View File

@ -2,11 +2,16 @@ package utils
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
r := RunTCPPingLoop("127.0.0.1:23333", 4) r := RunTCPPingLoop("127.0.0.1:23333", 4)
if r.PacketsLoss == r.PacketsSent { assert.Equal(t, 4, r.PacketsLoss)
t.Fatal(r) 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)
} }