From bfaaf76d9e6e96e30d37cfa5fa6ebd609c8300ea Mon Sep 17 00:00:00 2001 From: David Weitzman Date: Thu, 28 May 2020 18:20:58 -0700 Subject: [PATCH] cache_impl_test.go: fix failing test with ipv6 A newly-added test in #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]: Signed-off-by: David Weitzman --- test/redis/cache_impl_test.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/redis/cache_impl_test.go b/test/redis/cache_impl_test.go index e0807689b..34339e292 100644 --- a/test/redis/cache_impl_test.go +++ b/test/redis/cache_impl_test.go @@ -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) @@ -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) {