Skip to content

Commit

Permalink
Improved flaky dial pinger test - calling a local listener
Browse files Browse the repository at this point in the history
  • Loading branch information
eranharel committed May 27, 2021
1 parent cd29c5c commit 8cce27d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions checks/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package checks

import (
"context"
"net"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -51,15 +53,26 @@ func mockPinger(failing bool) PingContextFunc {
}
}

func TestNewDialPinger(t *testing.T) {
func TestNewDialPinger_bogusAddress(t *testing.T) {
assertions := assert.New(t)

pinger := NewDialPinger("tcp", "there.should.be.no.such.host.com:666")

ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
assertions.Error(pinger.PingContext(ctx), "expecting a ping error for non existing address")
}

func TestNewDialPinger_existingAddress(t *testing.T) {
assertions := assert.New(t)

ln, err := net.Listen("tcp", ":0")
require.NoError(t, err, "failed to start a test listener")
defer func() { _ = ln.Close() }()

pinger = NewDialPinger("tcp", "example.com:80")
pinger := NewDialPinger("tcp", ln.Addr().String())

ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
assertions.NoError(pinger.PingContext(ctx), "expecting success for an existing address")
}

0 comments on commit 8cce27d

Please sign in to comment.