Skip to content

Commit

Permalink
pool: Leave failed postgres db state in pool tests.
Browse files Browse the repository at this point in the history
This modifies the pool tests to avoid cleaning up the postgres data when
there is a test failure and the -failfast test flag is set so the
contents of the database can be examined.

Since a failed run with -failfast will no longer purge the data, it also
adds logic to purge the data prior to running the tests to ensure any
previous failed runs are cleaned up on subsequent runs.
  • Loading branch information
davecgh authored and jholdstock committed Sep 22, 2023
1 parent a8cab74 commit 91f6e87
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pool

import (
"flag"
"os"
"testing"

Expand Down Expand Up @@ -73,6 +74,17 @@ func teardownBoltDB(db *BoltDB, dbPath string) error {
return os.Remove(dbPath)
}

// isFailFastSet returns whether or not the failfast test flag is set.
func isFailFastSet() bool {
failfastFlag := flag.Lookup("test.failfast")
if failfastFlag == nil {
return false
}
failfast := failfastFlag.Value.(flag.Getter).Get()
isSet, ok := failfast.(bool)
return ok && isSet
}

// TestPool runs all pool related tests which require a real database.
func TestPool(t *testing.T) {
// All sub-tests to run. All of these tests will be run with a postgres
Expand Down Expand Up @@ -128,6 +140,17 @@ func TestPool(t *testing.T) {
boltDB.Close()
}

// Cleanup postgres DB from potential failed previous runs.
postgresDB, err := setupPostgresDB()
if err != nil {
t.Fatalf("setupPostgresDB error: %v", err)
}
err = postgresDB.purge()
if err != nil {
t.Fatalf("postgres teardown error: %v", err)
}
postgresDB.Close()

// Run all tests with postgres DB.
for testName, test := range tests {
postgresDB, err := setupPostgresDB()
Expand All @@ -139,6 +162,13 @@ func TestPool(t *testing.T) {

t.Run(testName+"_Postgres", test)

// Avoid purging the database and running other tests on test failures
// when the failfast flag is set.
if t.Failed() && isFailFastSet() {
postgresDB.Close()
break
}

err = postgresDB.purge()
if err != nil {
t.Fatalf("postgres teardown error: %v", err)
Expand Down

0 comments on commit 91f6e87

Please sign in to comment.