From 48889092082d802bb35e2e4d6e2d2f18fbdb93d7 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 26 Apr 2023 19:53:53 +0800 Subject: [PATCH] fix: fix IsPingConnected failed in windows --- netutil/net.go | 8 +++++++- netutil/net_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/netutil/net.go b/netutil/net.go index 53d69bb..e9e40f8 100644 --- a/netutil/net.go +++ b/netutil/net.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "os/exec" + "runtime" "strings" "time" @@ -251,7 +252,12 @@ func DownloadFile(filepath string, url string) error { // IsPingConnected checks if can ping specified host or not. // Play: todo func IsPingConnected(host string) bool { - cmd := exec.Command("ping", host, "-c", "1", "-W", "6") + cmd := exec.Command("ping", host, "-c", "4", "-W", "6") + + if runtime.GOOS == "windows" { + cmd = exec.Command("ping", host, "-n", "4", "-w", "6") + } + err := cmd.Run() if err != nil { return false diff --git a/netutil/net_test.go b/netutil/net_test.go index 7e07b0e..cd46945 100644 --- a/netutil/net_test.go +++ b/netutil/net_test.go @@ -121,7 +121,7 @@ func TestIsPingConnected(t *testing.T) { assert := internal.NewAssert(t, "TestIsPingConnected") // in github action env, this will fail - // result1 := IsPingConnected("bing.com") + // result1 := IsPingConnected("www.baidu.com") // assert.Equal(true, result1) result2 := IsPingConnected("www.!@#&&&.com")