1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

Merge pull request #272 from fumiama/delay

fix #245: tcping error handling
This commit is contained in:
Mrs4s 2022-05-10 19:35:36 +08:00 committed by GitHub
commit fba6bc90e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
} }