-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pgx5 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jackc/pgx/v5" | ||
) | ||
|
||
// Config is the PostgreSQL checker configuration settings container. | ||
type Config struct { | ||
// DSN is the PostgreSQL instance connection DSN. Required. | ||
DSN string | ||
} | ||
|
||
// New creates new PostgreSQL health check that verifies the following: | ||
// - connection establishing | ||
// - doing the ping command | ||
// - selecting postgres version | ||
func New(config Config) func(ctx context.Context) error { | ||
return func(ctx context.Context) (checkErr error) { | ||
conn, err := pgx.Connect(ctx, config.DSN) | ||
if err != nil { | ||
checkErr = fmt.Errorf("PostgreSQL health check failed on connect: %w", err) | ||
return | ||
} | ||
|
||
defer func() { | ||
// override checkErr only if there were no other errors | ||
if err := conn.Close(ctx); err != nil && checkErr == nil { | ||
checkErr = fmt.Errorf("PostgreSQL health check failed on connection closing: %w", err) | ||
} | ||
}() | ||
|
||
err = conn.Ping(ctx) | ||
if err != nil { | ||
checkErr = fmt.Errorf("PostgreSQL health check failed on ping: %w", err) | ||
return | ||
} | ||
|
||
rows, err := conn.Query(ctx, `SELECT VERSION()`) | ||
if err != nil { | ||
checkErr = fmt.Errorf("PostgreSQL health check failed on select: %w", err) | ||
return | ||
} | ||
defer func() { | ||
rows.Close() | ||
}() | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package pgx5 | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const pgDSNEnv = "HEALTH_GO_PG_PGX5_DSN" | ||
|
||
func TestNew(t *testing.T) { | ||
initDB(t) | ||
|
||
check := New(Config{ | ||
DSN: getDSN(t), | ||
}) | ||
|
||
err := check(context.Background()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestEnsureConnectionIsClosed(t *testing.T) { | ||
initDB(t) | ||
|
||
pgDSN := getDSN(t) | ||
ctx := context.Background() | ||
|
||
conn, err := pgx.Connect(ctx, getDSN(t)) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
err := conn.Close(ctx) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
var initialConnections int | ||
row := conn.QueryRow(ctx, `SELECT sum(numbackends) FROM pg_stat_database`) | ||
err = row.Scan(&initialConnections) | ||
require.NoError(t, err) | ||
|
||
check := New(Config{ | ||
DSN: pgDSN, | ||
}) | ||
|
||
for i := 0; i < 10; i++ { | ||
err := check(ctx) | ||
assert.NoError(t, err) | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
var currentConnections int | ||
row = conn.QueryRow(ctx, `SELECT sum(numbackends) FROM pg_stat_database`) | ||
err = row.Scan(¤tConnections) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, initialConnections, currentConnections) | ||
} | ||
|
||
func getDSN(t *testing.T) string { | ||
t.Helper() | ||
|
||
pgDSN, ok := os.LookupEnv(pgDSNEnv) | ||
require.True(t, ok) | ||
|
||
return pgDSN | ||
} | ||
|
||
var dbInit sync.Once | ||
|
||
func initDB(t *testing.T) { | ||
t.Helper() | ||
|
||
dbInit.Do(func() { | ||
ctx := context.Background() | ||
|
||
conn, err := pgx.Connect(ctx, getDSN(t)) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
err := conn.Close(ctx) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
_, err = conn.Exec(ctx, ` | ||
CREATE TABLE IF NOT EXISTS test_pgx4 ( | ||
id TEXT NOT NULL PRIMARY KEY, | ||
secret TEXT NOT NULL, | ||
extra TEXT NOT NULL, | ||
redirect_uri TEXT NOT NULL | ||
); | ||
`) | ||
require.NoError(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters