Skip to content
Permalink
Browse files

Merge pull request #246 from kamichidu/fix-leaking

refs #245 add test to check connection leaking
  • Loading branch information...
mcuadros committed May 16, 2019
2 parents 0f81f1a + c4daf22 commit 98631d1635a6f6d13ca042d2f4c73e520ab79adc
Showing with 97 additions and 0 deletions.
  1. +1 −0 store.go
  2. +36 −0 tests/common_test.go
  3. +1 −0 tests/resultset_test.go
  4. +59 −0 tests/store_test.go
@@ -439,6 +439,7 @@ func (s *Store) Reload(schema Schema, record Record) error {
if err != nil {
return err
}
defer rows.Close()

rs := NewResultSet(rows, false, nil, columns...)
if !rs.Next() {
@@ -22,6 +22,10 @@ type BaseTestSuite struct {
db *sql.DB
schemas []string
tables []string

// the count of opened connections.
// this will be set in the SetupTest function.
openConnectionsBeforeTest int
}

func NewBaseSuite(schemas []string, tables ...string) BaseTestSuite {
@@ -41,6 +45,11 @@ func (s *BaseTestSuite) SetupSuite() {
panic(fmt.Sprintf("It was unable to connect to the DB.\n%s\n", err))
}

// set all connections will be closed immediately.
// this is required to check connections are leaked or not.
// because database/sql keep connection in the pool by default.
db.SetMaxIdleConns(0)

s.db = db
}

@@ -49,6 +58,9 @@ func (s *BaseTestSuite) TearDownSuite() {
}

func (s *BaseTestSuite) SetupTest() {
// save current open connection count for detecting that connection was leaked while a test.
s.openConnectionsBeforeTest = s.db.Stats().OpenConnections

if len(s.tables) == 0 {
return
}
@@ -57,6 +69,12 @@ func (s *BaseTestSuite) SetupTest() {
}

func (s *BaseTestSuite) TearDownTest() {
openConnections := s.db.Stats().OpenConnections
leakedConnections := openConnections - s.openConnectionsBeforeTest
if leakedConnections > 0 {
s.Fail(fmt.Sprintf("%d database connections were leaked", leakedConnections))
}

if len(s.tables) == 0 {
return
}
@@ -113,6 +131,24 @@ func (s *BaseTestSuite) resultOrError(res interface{}, err error) bool {
return true
}

func (s *BaseTestSuite) resultsOrError(res interface{}, err error) bool {
if reflect.ValueOf(res).Kind() != reflect.Slice {
panic("resultsOrError expects res is a slice")
}

if err == nil && res == nil {
s.Fail("FindAll should return an error or a documents, but nothing was returned")
return false
}

if err != nil && res != nil {
s.Fail("FindAll should return only an error or a documents, but it was returned both")
return false
}

return true
}

func envOrDefault(key string, def string) string {
v := os.Getenv(key)
if v == "" {
@@ -128,6 +128,7 @@ func (s *ResulsetSuite) TestResultSetForEachError() {

s.NotPanics(func() {
rs := store.MustFind(NewResultSetFixtureQuery())
defer rs.Close()
err := rs.ForEach(func(*ResultSetFixture) error {
return fail
})
@@ -89,6 +89,7 @@ func (s *StoreSuite) TestStoreMustFind() {
query := NewStoreFixtureQuery()
s.NotPanics(func() {
rs := store.MustFind(query)
defer rs.Close()
s.NotNil(rs)
})
}
@@ -111,6 +112,64 @@ func (s *StoreSuite) TestStoreFindOneReturnValues() {
s.resultOrError(doc, err)
}

func (s *StoreSuite) TestStoreFindAllReturnValues() {
store := NewStoreWithConstructFixtureStore(s.db)
s.Nil(store.Insert(NewStoreWithConstructFixture("foo")))
s.Nil(store.Insert(NewStoreWithConstructFixture("bar")))

notFoundQuery := NewStoreWithConstructFixtureQuery()
notFoundQuery.Where(kallax.Eq(Schema.ResultSetFixture.ID, kallax.NewULID()))
docs, err := store.FindAll(notFoundQuery)
s.resultsOrError(docs, err)
s.NotPanics(func() {
s.Equal(0, len(docs))
})

docs, err = store.FindAll(NewStoreWithConstructFixtureQuery().Order(kallax.Asc(Schema.StoreWithConstructFixture.Foo)))
s.resultsOrError(docs, err)
s.NotPanics(func() {
s.Equal(2, len(docs))
s.Equal("bar", docs[0].Foo)
s.Equal("foo", docs[1].Foo)
})
}

func (s *StoreSuite) TestStoreCount() {
store := NewStoreWithConstructFixtureStore(s.db)
s.Nil(store.Insert(NewStoreWithConstructFixture("foo")))
s.Nil(store.Insert(NewStoreWithConstructFixture("bar")))

notFoundQuery := NewStoreWithConstructFixtureQuery()
notFoundQuery.Where(kallax.Eq(Schema.ResultSetFixture.ID, kallax.NewULID()))
count, err := store.Count(notFoundQuery)
s.Nil(err)
s.NotPanics(func() {
s.Equal(int64(0), count)
})

count, err = store.Count(NewStoreWithConstructFixtureQuery())
s.Nil(err)
s.NotPanics(func() {
s.Equal(int64(2), count)
})
}

func (s *StoreSuite) TestStoreReload() {
store := NewStoreWithConstructFixtureStore(s.db)
s.Nil(store.Insert(NewStoreWithConstructFixture("bar")))

doc, err := store.FindOne(NewStoreWithConstructFixtureQuery().FindByFoo("bar").Select(Schema.StoreWithConstructFixture.ID))
s.Nil(err)
s.NotPanics(func() {
s.Equal("", doc.Foo)
})
err = store.Reload(doc)
s.Nil(err)
s.NotPanics(func() {
s.Equal("bar", doc.Foo)
})
}

func (s *StoreSuite) TestStoreInsertUpdateMustFind() {
store := NewStoreWithConstructFixtureStore(s.db)

0 comments on commit 98631d1

Please sign in to comment.
You can’t perform that action at this time.