Skip to content

Commit

Permalink
add tests for ForceTerminateConnections
Browse files Browse the repository at this point in the history
  • Loading branch information
peterldowns committed Sep 25, 2024
1 parent 123abb7 commit 674af26
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions testdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,59 @@ func defaultMigrator() pgtestdb.Migrator {
}
}

func TestDefaultDroppingBehaviorFailsWithOpenConnections(t *testing.T) {
t.Parallel()
conf := pgtestdb.Config{
DriverName: "pgx",
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
// ForceTerminateConnections defaults to false
}
var migrator pgtestdb.Migrator = pgtestdb.NoopMigrator{}

tt := &MockT{}
db := pgtestdb.New(tt, conf, migrator)
db.SetMaxOpenConns(1)
// Intentionally hold the connection option by beginning a transaction and
// then never terminating it. Since `ForceTerminateConnections == false`,
// pgtestdb will error during its cleanup phase, and will mark the test as
// failed.
tx, err := db.Begin()
assert.Nil(t, err)
_ = tx
tt.DoCleanup()
assert.True(t, tt.Failed())
}

func TestForceTerminateConnectionsWorksCorrectly(t *testing.T) {
t.Parallel()
conf := pgtestdb.Config{
DriverName: "pgx",
User: "postgres",
Password: "password",
Host: "localhost",
Port: "5433",
Options: "sslmode=disable",
ForceTerminateConnections: true,
}
var migrator pgtestdb.Migrator = pgtestdb.NoopMigrator{}
tt := &MockT{}
db := pgtestdb.New(tt, conf, migrator)
db.SetMaxOpenConns(1)
// Intentionally hold the connection option by beginning a transaction and
// then never terminating it. Since `ForceTerminateConnections == true`,
// pgtestdb will force-terminate this connection during its cleanup phase
// and the test will complete successfully.
tx, err := db.Begin()
assert.Nil(t, err)
_ = tx
tt.DoCleanup()
assert.False(t, tt.Failed())
}

// sqlMigrator is a test helper that satisfies the pgtestdb.Migrator interface.
type sqlMigrator struct {
preparations []string
Expand Down

0 comments on commit 674af26

Please sign in to comment.