Skip to content

Commit

Permalink
Merge pull request #60 from shashankmehra/fix_postgres_connleak
Browse files Browse the repository at this point in the history
Fix postgres connection leak
  • Loading branch information
cainlevy committed Aug 1, 2018
2 parents 808901c + ef571df commit 2f08893
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/postgres/account_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (db *AccountStore) Create(u string, p []byte) (*models.Account, error) {
if err != nil {
return nil, err
}
defer result.Close()
result.Next()
var id int64
err = result.Scan(&id)
Expand Down
44 changes: 44 additions & 0 deletions data/testers/account_store_testers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testers
import (
"testing"

"database/sql"

"github.com/keratin/authn-server/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,6 +22,18 @@ var AccountStoreTesters = []func(*testing.T, data.AccountStore){
testFindByOauthAccount,
}

type hasStats interface {
Stats() sql.DBStats
}

func getOpenConnectionCount(store data.AccountStore) int {
if st, ok := store.(hasStats); ok {
return st.Stats().OpenConnections
} else {
return 1
}
}

func testCreate(t *testing.T, store data.AccountStore) {
account, err := store.Create("authn@keratin.tech", []byte("password"))
require.NoError(t, err)
Expand All @@ -36,6 +50,9 @@ func testCreate(t *testing.T, store data.AccountStore) {
if !data.IsUniquenessError(err) {
t.Errorf("expected uniqueness error, got %T %v", err, err)
}

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testFindByUsername(t *testing.T, store data.AccountStore) {
Expand All @@ -49,6 +66,9 @@ func testFindByUsername(t *testing.T, store data.AccountStore) {
account, err = store.FindByUsername("authn@keratin.tech")
assert.NoError(t, err)
assert.NotNil(t, account)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testLockAndUnlock(t *testing.T, store data.AccountStore) {
Expand All @@ -70,6 +90,9 @@ func testLockAndUnlock(t *testing.T, store data.AccountStore) {
require.NoError(t, err)
require.NotEmpty(t, after2)
assert.False(t, after2.Locked)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testArchive(t *testing.T, store data.AccountStore) {
Expand All @@ -92,6 +115,9 @@ func testArchive(t *testing.T, store data.AccountStore) {
err = store.Archive(account2.ID)
assert.NoError(t, err)
}

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testArchiveWithOauth(t *testing.T, store data.AccountStore) {
Expand All @@ -106,6 +132,9 @@ func testArchiveWithOauth(t *testing.T, store data.AccountStore) {
found, err := store.FindByOauthAccount("PROVIDER", "PROVIDERID")
require.NoError(t, err)
assert.Empty(t, found)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testRequireNewPassword(t *testing.T, store data.AccountStore) {
Expand All @@ -119,6 +148,9 @@ func testRequireNewPassword(t *testing.T, store data.AccountStore) {
after, err := store.Find(account.ID)
require.NoError(t, err)
assert.True(t, after.RequireNewPassword)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testSetPassword(t *testing.T, store data.AccountStore) {
Expand All @@ -135,6 +167,9 @@ func testSetPassword(t *testing.T, store data.AccountStore) {
assert.Equal(t, []byte("new"), after.Password)
assert.False(t, after.RequireNewPassword)
assert.NotEqual(t, account.PasswordChangedAt, after.PasswordChangedAt)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testUpdateUsername(t *testing.T, store data.AccountStore) {
Expand All @@ -147,6 +182,9 @@ func testUpdateUsername(t *testing.T, store data.AccountStore) {
after, err := store.Find(account.ID)
require.NoError(t, err)
assert.Equal(t, "new", after.Username)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testAddOauthAccount(t *testing.T, store data.AccountStore) {
Expand All @@ -173,6 +211,9 @@ func testAddOauthAccount(t *testing.T, store data.AccountStore) {
if err == nil || !data.IsUniquenessError(err) {
t.Errorf("expected uniqueness error, got %T %v", err, err)
}

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

func testFindByOauthAccount(t *testing.T, store data.AccountStore) {
Expand All @@ -196,4 +237,7 @@ func testFindByOauthAccount(t *testing.T, store data.AccountStore) {
found, err = store.FindByOauthAccount("OAUTHPROVIDER", "PROVIDERID")
assert.NoError(t, err)
assert.Equal(t, account.ID, found.ID)

// Assert that db connections are released to pool
assert.Equal(t, 1, getOpenConnectionCount(store))
}

0 comments on commit 2f08893

Please sign in to comment.