add connections leak test

This commit is contained in:
kinggo 2021-11-03 19:40:32 +08:00
parent c170af11e9
commit 220e9ca702

24
tests/conn_test.go Normal file
View File

@ -0,0 +1,24 @@
package tests_test
import (
"context"
"database/sql"
. "gorm.io/gorm/utils/tests"
"reflect"
"testing"
)
func TestConnLeak(t *testing.T) {
DB.Table("non_existent").WithContext(context.Background()).FirstOrCreate(&User{Name: "foo"})
DB.Table("non_existent").WithContext(context.Background()).FirstOrCreate(&User{Name: "foo"})
DB.Table("non_existent").WithContext(context.Background()).FirstOrCreate(&User{Name: "foo"})
connPool := DB.ConnPool.(*sql.DB)
v := reflect.ValueOf(connPool).Elem()
f := v.FieldByName("numOpen")
if f.Int() > 1 {
t.Errorf("Expected open one connections but found %d", f.Int())
}
}