add connections leak test
This commit is contained in:
		
							parent
							
								
									c170af11e9
								
							
						
					
					
						commit
						220e9ca702
					
				
							
								
								
									
										24
									
								
								tests/conn_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								tests/conn_test.go
									
									
									
									
									
										Normal 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())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user