From 220e9ca7020463662074f9b80efa0c1505d9506a Mon Sep 17 00:00:00 2001 From: kinggo <1510613524@qq.com> Date: Wed, 3 Nov 2021 19:40:32 +0800 Subject: [PATCH] add connections leak test --- tests/conn_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/conn_test.go diff --git a/tests/conn_test.go b/tests/conn_test.go new file mode 100644 index 00000000..35647509 --- /dev/null +++ b/tests/conn_test.go @@ -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()) + } +}