Skip to content

Commit

Permalink
cache_impl_test.go: fix failing test with ipv6
Browse files Browse the repository at this point in the history
A newly-added test in envoyproxy#137 checks the exact text of an error message which
seems to vary when the network is tcp4 vs tcp6. This change relaxes the
assertion to look for "connection refused" in a panic without making
assumptions about what an IP address looks like.

Example failure:

--- FAIL: TestNewClientImpl (0.00s)
    --- FAIL: TestNewClientImpl/connection_refused (0.00s)
        cache_impl_test.go:442:
                Error Trace:    cache_impl_test.go:442
                Error:          func (assert.PanicTestFunc)(0x1724110) should panic with error message: "dial tcp 127.0.0.1:12345: connect: connection refused"
                                        Panic value:    "dial tcp [::1]:12345: connect: connection refused"
                                        Panic stack:    goroutine 27 [running]:

The testify assert package doesn't seem to support inexact matching on error messages, so the code gets a bit uglier than before.

Signed-off-by: David Weitzman <dweitzman@pinterest.com>
  • Loading branch information
dweitzman committed May 29, 2020
1 parent afda91c commit 126fb2c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions test/redis/cache_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ func mustNewRedisServer() *miniredis.Miniredis {
return srv
}

func expectPanicError(t *testing.T, f assert.PanicTestFunc) (result error) {
t.Helper()
defer func() {
panicResult := recover()
assert.NotNil(t, panicResult, "Expected a panic")
result = panicResult.(error)
}()
f()
return
}

func TestNewClientImpl(t *testing.T) {
redisAuth := "123"
statsStore := stats.NewStore(stats.NewNullSink(), false)
Expand All @@ -439,11 +450,10 @@ func TestNewClientImpl(t *testing.T) {
}

t.Run("connection refused", func(t *testing.T) {
assert.PanicsWithError(t, "dial tcp 127.0.0.1:12345: connect: connection refused", func() {
// It's possible there is a redis server listening on 6379 in ci environment, so
// use a random port.
mkRedisClient("", "localhost:12345")
})
// It's possible there is a redis server listening on 6379 in ci environment, so
// use a random port.
panicErr := expectPanicError(t, func() { mkRedisClient("", "localhost:12345") })
assert.Contains(t, panicErr.Error(), "connection refused")
})

t.Run("ok", func(t *testing.T) {
Expand Down

0 comments on commit 126fb2c

Please sign in to comment.