1
0

ping_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pingo
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // integration test
  9. func Test_SystemPing(t *testing.T) {
  10. type testCase struct {
  11. // constructors
  12. address string
  13. // results
  14. expectedMatch float64
  15. err error
  16. }
  17. t.Run("ping localhost (via 127.0.0.1)", func(t *testing.T) {
  18. tests := []testCase{{"127.0.0.1", 0, nil}}
  19. for _, test := range tests {
  20. result, err := runPing(test.address)
  21. // result data prep
  22. lines := strings.Split(strings.ReplaceAll(string(*result), "\r\n", "\n"), "\n")
  23. var has_addr bool
  24. for _, line := range lines {
  25. if strings.Contains(line, "127.0.0.1") {
  26. has_addr = true
  27. }
  28. }
  29. // assertions
  30. assert.NoError(t, err)
  31. assert.True(t, has_addr)
  32. }
  33. })
  34. }
  35. func Test_SplitBytes(t *testing.T) {
  36. type testCase struct {
  37. // test data
  38. bytes *[]byte
  39. // expected results
  40. lines []string
  41. }
  42. t.Run("test bytes to lines", func(t *testing.T) {
  43. var tests []testCase
  44. // init tests
  45. for range 5 {
  46. bytes, _ := runPing("127.0.0.1")
  47. lines := strings.Split(strings.ReplaceAll(string(*bytes), "\r\n", "\n"), "\n")
  48. tests = append(tests, testCase{bytes: bytes, lines: lines})
  49. }
  50. // run tests
  51. for _, test := range tests {
  52. lines := splitBytesToLines(test.bytes)
  53. assert.Equal(t, lines, test.lines)
  54. }
  55. })
  56. }
  57. func Test_Ping(t *testing.T) {
  58. type testCase struct {
  59. address string
  60. delay float64
  61. err error
  62. }
  63. var tests []testCase
  64. // init tests
  65. tests = append(tests, testCase{
  66. address: "127.0.0.1",
  67. delay: 0,
  68. err: nil,
  69. })
  70. tests = append(tests,
  71. testCase{
  72. address: "willneverresolveever",
  73. delay: -1,
  74. err: errors.New("test failure"),
  75. })
  76. for _, test := range tests {
  77. delay, err := Ping(test.address)
  78. assert.GreaterOrEqual(t, delay, test.delay)
  79. if test.err != nil {
  80. assert.Error(t, err)
  81. assert.Equal(t, delay, test.delay)
  82. } else {
  83. assert.NoError(t, err)
  84. }
  85. }
  86. }
  87. func spawnAddress(a string, r []float64, m int) Address {
  88. return Address{Address: a, Results: r, MaxResults: m}
  89. }
  90. func Test_Address_Truncate(t *testing.T) {
  91. testA := spawnAddress("test", make([]float64, 20), 10)
  92. postTruncate := testA.Truncate()
  93. assert.True(t, len(postTruncate) == 10)
  94. }
  95. func Test_Address_Last(t *testing.T) {
  96. testA := spawnAddress("test", []float64{20}, 10)
  97. assert.True(t, testA.Last() == 20)
  98. testA = spawnAddress("test", []float64{}, 10)
  99. assert.True(t, testA.Last() == -1)
  100. }