From 56d94a2f370fde4e8a7500658cc7fa57ddfc08e4 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Mon, 19 Apr 2021 18:02:29 +0100 Subject: [PATCH] Fix TestOpen_openAndPingFails (#3551) --- .../recoverysigner/internal/db/db_test.go | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/exp/services/recoverysigner/internal/db/db_test.go b/exp/services/recoverysigner/internal/db/db_test.go index 3ef0bc34e1..91dba7d5b2 100644 --- a/exp/services/recoverysigner/internal/db/db_test.go +++ b/exp/services/recoverysigner/internal/db/db_test.go @@ -1,6 +1,9 @@ package db import ( + "fmt" + "net" + "regexp" "testing" "github.com/stellar/go/support/db/dbtest" @@ -20,10 +23,25 @@ func TestOpen_openAndPingSucceeds(t *testing.T) { } func TestOpen_openAndPingFails(t *testing.T) { - sqlxDB, err := Open("postgres://localhost:0") + // Find an empty port + listener, err := net.Listen("tcp", ":0") + require.NoError(t, err) + port := listener.Addr().(*net.TCPAddr).Port + require.NoError(t, listener.Close()) + // Slight race here with other stuff on the system, which could claim this port. + + sqlxDB, err := Open(fmt.Sprintf("postgres://localhost:%d", port)) require.NoError(t, err) assert.Equal(t, "postgres", sqlxDB.DriverName()) err = sqlxDB.Ping() - require.EqualError(t, err, "dial tcp 127.0.0.1:0: connect: connection refused") + require.Error(t, err) + require.Regexp( + t, + regexp.MustCompile( + // regex to support both ipv4 and ipv6, on the port we found. + fmt.Sprintf("dial tcp (127\\.0\\.0\\.1|\\[::1\\]):%d: connect: connection refused", port), + ), + err.Error(), + ) }