aboutsummaryrefslogtreecommitdiff
path: root/src/database/sql/sql_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/sql/sql_test.go')
-rw-r--r--src/database/sql/sql_test.go131
1 files changed, 101 insertions, 30 deletions
diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go
index d835bc160a..48c872d8c6 100644
--- a/src/database/sql/sql_test.go
+++ b/src/database/sql/sql_test.go
@@ -142,6 +142,20 @@ func (db *DB) numFreeConns() int {
return len(db.freeConn)
}
+// clearAllConns closes all connections in db.
+func (db *DB) clearAllConns(t *testing.T) {
+ db.SetMaxIdleConns(0)
+
+ if g, w := db.numFreeConns(), 0; g != w {
+ t.Errorf("free conns = %d; want %d", g, w)
+ }
+
+ if n := db.numDepsPollUntil(0, time.Second); n > 0 {
+ t.Errorf("number of dependencies = %d; expected 0", n)
+ db.dumpDeps(t)
+ }
+}
+
func (db *DB) dumpDeps(t *testing.T) {
for fc := range db.dep {
db.dumpDep(t, 0, fc, map[finalCloser]bool{})
@@ -991,16 +1005,7 @@ func TestMaxOpenConns(t *testing.T) {
// Force the number of open connections to 0 so we can get an accurate
// count for the test
- db.SetMaxIdleConns(0)
-
- if g, w := db.numFreeConns(), 0; g != w {
- t.Errorf("free conns = %d; want %d", g, w)
- }
-
- if n := db.numDepsPollUntil(0, time.Second); n > 0 {
- t.Errorf("number of dependencies = %d; expected 0", n)
- db.dumpDeps(t)
- }
+ db.clearAllConns(t)
driver.mu.Lock()
opens0 := driver.openCount
@@ -1096,16 +1101,7 @@ func TestMaxOpenConns(t *testing.T) {
db.dumpDeps(t)
}
- db.SetMaxIdleConns(0)
-
- if g, w := db.numFreeConns(), 0; g != w {
- t.Errorf("free conns = %d; want %d", g, w)
- }
-
- if n := db.numDepsPollUntil(0, time.Second); n > 0 {
- t.Errorf("number of dependencies = %d; expected 0", n)
- db.dumpDeps(t)
- }
+ db.clearAllConns(t)
}
// Issue 9453: tests that SetMaxOpenConns can be lowered at runtime
@@ -1263,6 +1259,90 @@ func TestStats(t *testing.T) {
}
}
+func TestConnMaxLifetime(t *testing.T) {
+ t0 := time.Unix(1000000, 0)
+ offset := time.Duration(0)
+
+ nowFunc = func() time.Time { return t0.Add(offset) }
+ defer func() { nowFunc = time.Now }()
+
+ db := newTestDB(t, "magicquery")
+ defer closeDB(t, db)
+
+ driver := db.driver.(*fakeDriver)
+
+ // Force the number of open connections to 0 so we can get an accurate
+ // count for the test
+ db.clearAllConns(t)
+
+ driver.mu.Lock()
+ opens0 := driver.openCount
+ closes0 := driver.closeCount
+ driver.mu.Unlock()
+
+ db.SetMaxIdleConns(10)
+ db.SetMaxOpenConns(10)
+
+ tx, err := db.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ offset = time.Second
+ tx2, err := db.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tx.Commit()
+ tx2.Commit()
+
+ driver.mu.Lock()
+ opens := driver.openCount - opens0
+ closes := driver.closeCount - closes0
+ driver.mu.Unlock()
+
+ if opens != 2 {
+ t.Errorf("opens = %d; want 2", opens)
+ }
+ if closes != 0 {
+ t.Errorf("closes = %d; want 0", closes)
+ }
+ if g, w := db.numFreeConns(), 2; g != w {
+ t.Errorf("free conns = %d; want %d", g, w)
+ }
+
+ // Expire first conn
+ offset = time.Second * 11
+ db.SetConnMaxLifetime(time.Second * 10)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tx, err = db.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+ tx2, err = db.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+ tx.Commit()
+ tx2.Commit()
+
+ driver.mu.Lock()
+ opens = driver.openCount - opens0
+ closes = driver.closeCount - closes0
+ driver.mu.Unlock()
+
+ if opens != 3 {
+ t.Errorf("opens = %d; want 3", opens)
+ }
+ if closes != 1 {
+ t.Errorf("closes = %d; want 1", closes)
+ }
+}
+
// golang.org/issue/5323
func TestStmtCloseDeps(t *testing.T) {
if testing.Short() {
@@ -1356,16 +1436,7 @@ func TestStmtCloseDeps(t *testing.T) {
db.dumpDeps(t)
}
- db.SetMaxIdleConns(0)
-
- if g, w := db.numFreeConns(), 0; g != w {
- t.Errorf("free conns = %d; want %d", g, w)
- }
-
- if n := db.numDepsPollUntil(0, time.Second); n > 0 {
- t.Errorf("number of dependencies = %d; expected 0", n)
- db.dumpDeps(t)
- }
+ db.clearAllConns(t)
}
// golang.org/issue/5046