ping_test.go 584 B

123456789101112131415161718192021222324252627282930
  1. package networkdiagnostic_test
  2. import (
  3. "fmt"
  4. "github.com/go-ping/ping"
  5. "runtime"
  6. "testing"
  7. "time"
  8. )
  9. func TestPing(t *testing.T) {
  10. pinger, err := ping.NewPinger("console.cengcloud.com")
  11. if err != nil {
  12. panic(err)
  13. }
  14. os := runtime.GOOS
  15. pinger.Count = 3
  16. pinger.Timeout = 5 * time.Second
  17. pinger.Interval = 1 * time.Second
  18. if os == "windows" {
  19. pinger.SetPrivileged(true)
  20. }
  21. err = pinger.Run() // Blocks until finished.
  22. if err != nil {
  23. panic(err)
  24. }
  25. stats := pinger.Statistics() // get send/receive/duplicate/rtt stats
  26. fmt.Println("", stats)
  27. fmt.Println("", os)
  28. }