Skip to content

Commit

Permalink
Fix TestOpen_openAndPingFails (#3551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Bellamy authored Apr 19, 2021
1 parent 3b1409b commit 56d94a2
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions exp/services/recoverysigner/internal/db/db_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package db

import (
"fmt"
"net"
"regexp"
"testing"

"github.com/stellar/go/support/db/dbtest"
Expand All @@ -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(),
)
}

0 comments on commit 56d94a2

Please sign in to comment.