| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package pingo
- import (
- "errors"
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- )
- // integration test
- func Test_SystemPing(t *testing.T) {
- type testCase struct {
- // constructors
- address string
- // results
- expectedMatch float64
- err error
- }
- t.Run("ping localhost (via 127.0.0.1)", func(t *testing.T) {
- tests := []testCase{{"127.0.0.1", 0, nil}}
- for _, test := range tests {
- result, err := runPing(test.address)
- // result data prep
- lines := strings.Split(strings.ReplaceAll(string(*result), "\r\n", "\n"), "\n")
- var has_addr bool
- for _, line := range lines {
- if strings.Contains(line, "127.0.0.1") {
- has_addr = true
- }
- }
- // assertions
- assert.NoError(t, err)
- assert.True(t, has_addr)
- }
- })
- }
- func Test_SplitBytes(t *testing.T) {
- type testCase struct {
- // test data
- bytes *[]byte
- // expected results
- lines []string
- }
- t.Run("test bytes to lines", func(t *testing.T) {
- var tests []testCase
- // init tests
- for range 5 {
- bytes, _ := runPing("127.0.0.1")
- lines := strings.Split(strings.ReplaceAll(string(*bytes), "\r\n", "\n"), "\n")
- tests = append(tests, testCase{bytes: bytes, lines: lines})
- }
- // run tests
- for _, test := range tests {
- lines := splitBytesToLines(test.bytes)
- assert.Equal(t, lines, test.lines)
- }
- })
- }
- func Test_Ping(t *testing.T) {
- type testCase struct {
- address string
- delay float64
- err error
- }
- var tests []testCase
- // init tests
- tests = append(tests, testCase{
- address: "127.0.0.1",
- delay: 0,
- err: nil,
- })
- tests = append(tests,
- testCase{
- address: "willneverresolveever",
- delay: -1,
- err: errors.New("test failure"),
- })
- for _, test := range tests {
- delay, err := Ping(test.address)
- assert.GreaterOrEqual(t, delay, test.delay)
- if test.err != nil {
- assert.Error(t, err)
- assert.Equal(t, delay, test.delay)
- } else {
- assert.NoError(t, err)
- }
- }
- }
- func spawnAddress(a string, r []float64, m int) Address {
- return Address{Address: a, results: r, max_results: m}
- }
- func Test_Address_Truncate(t *testing.T) {
- testA := spawnAddress("test", make([]float64, 20), 10)
- postTruncate := testA.Truncate()
- assert.True(t, len(postTruncate) == 10)
- }
- func Test_Address_Last(t *testing.T) {
- testA := spawnAddress("test", []float64{20}, 10)
- assert.True(t, testA.Last() == 20)
- testA = spawnAddress("test", []float64{}, 10)
- assert.True(t, testA.Last() == -1)
- }
|