1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
22 freedFrom := make(map[dbConn]string)
23 putConnHook = func(db *DB, c *driverConn) {
24 for _, oc := range db.freeConn {
26 // print before panic, as panic may get lost due to conflicting panic
27 // (all goroutines asleep) elsewhere, since we might not unlock
28 // the mutex in freeConn here.
29 println("double free of conn. conflicts are:\nA) " + freedFrom[dbConn{db, c}] + "\n\nand\nB) " + stack())
30 panic("double free of conn.")
33 freedFrom[dbConn{db, c}] = stack()
37 const fakeDBName = "foo"
39 var chrisBirthday = time.Unix(123456789, 0)
41 type testOrBench interface {
42 Fatalf(string, ...interface{})
43 Errorf(string, ...interface{})
46 Logf(string, ...interface{})
49 func newTestDB(t testOrBench, name string) *DB {
50 db, err := Open("test", fakeDBName)
52 t.Fatalf("Open: %v", err)
54 if _, err := db.Exec("WIPE"); err != nil {
55 t.Fatalf("exec wipe: %v", err)
58 exec(t, db, "CREATE|people|name=string,age=int32,photo=blob,dead=bool,bdate=datetime")
59 exec(t, db, "INSERT|people|name=Alice,age=?,photo=APHOTO", 1)
60 exec(t, db, "INSERT|people|name=Bob,age=?,photo=BPHOTO", 2)
61 exec(t, db, "INSERT|people|name=Chris,age=?,photo=CPHOTO,bdate=?", 3, chrisBirthday)
63 if name == "magicquery" {
64 // Magic table name and column, known by fakedb_test.go.
65 exec(t, db, "CREATE|magicquery|op=string,millis=int32")
66 exec(t, db, "INSERT|magicquery|op=sleep,millis=10")
71 func exec(t testOrBench, db *DB, query string, args ...interface{}) {
72 _, err := db.Exec(query, args...)
74 t.Fatalf("Exec of %q: %v", query, err)
78 func closeDB(t testOrBench, db *DB) {
79 if e := recover(); e != nil {
80 fmt.Printf("Panic: %v\n", e)
83 defer setHookpostCloseConn(nil)
84 setHookpostCloseConn(func(_ *fakeConn, err error) {
86 t.Errorf("Error closing fakeConn: %v", err)
89 for i, dc := range db.freeConn {
90 if n := len(dc.openStmt); n > 0 {
91 // Just a sanity check. This is legal in
92 // general, but if we make the tests clean up
93 // their statements first, then we can safely
94 // verify this is always zero here, and any
95 // other value is a leak.
96 t.Errorf("while closing db, freeConn %d/%d had %d open stmts; want 0", i, len(db.freeConn), n)
101 t.Fatalf("error closing DB: %v", err)
105 // numPrepares assumes that db has exactly 1 idle conn and returns
106 // its count of calls to Prepare
107 func numPrepares(t *testing.T, db *DB) int {
108 if n := len(db.freeConn); n != 1 {
109 t.Fatalf("free conns = %d; want 1", n)
111 return db.freeConn[0].ci.(*fakeConn).numPrepare
114 func (db *DB) numDeps() int {
120 // Dependencies are closed via a goroutine, so this polls waiting for
121 // numDeps to fall to want, waiting up to d.
122 func (db *DB) numDepsPollUntil(want int, d time.Duration) int {
123 deadline := time.Now().Add(d)
126 if n <= want || time.Now().After(deadline) {
129 time.Sleep(50 * time.Millisecond)
133 func (db *DB) numFreeConns() int {
136 return len(db.freeConn)
139 func (db *DB) dumpDeps(t *testing.T) {
140 for fc := range db.dep {
141 db.dumpDep(t, 0, fc, map[finalCloser]bool{})
145 func (db *DB) dumpDep(t *testing.T, depth int, dep finalCloser, seen map[finalCloser]bool) {
147 indent := strings.Repeat(" ", depth)
150 t.Logf("%s%T (%p) waiting for -> %T (%p)", indent, dep, dep, k, k)
151 if fc, ok := k.(finalCloser); ok {
153 db.dumpDep(t, depth+1, fc, seen)
159 func TestQuery(t *testing.T) {
160 db := newTestDB(t, "people")
162 prepares0 := numPrepares(t, db)
163 rows, err := db.Query("SELECT|people|age,name|")
165 t.Fatalf("Query: %v", err)
174 err = rows.Scan(&r.age, &r.name)
176 t.Fatalf("Scan: %v", err)
182 t.Fatalf("Err: %v", err)
185 {age: 1, name: "Alice"},
186 {age: 2, name: "Bob"},
187 {age: 3, name: "Chris"},
189 if !reflect.DeepEqual(got, want) {
190 t.Errorf("mismatch.\n got: %#v\nwant: %#v", got, want)
193 // And verify that the final rows.Next() call, which hit EOF,
194 // also closed the rows connection.
195 if n := db.numFreeConns(); n != 1 {
196 t.Fatalf("free conns after query hitting EOF = %d; want 1", n)
198 if prepares := numPrepares(t, db) - prepares0; prepares != 1 {
199 t.Errorf("executed %d Prepare statements; want 1", prepares)
203 func TestByteOwnership(t *testing.T) {
204 db := newTestDB(t, "people")
206 rows, err := db.Query("SELECT|people|name,photo|")
208 t.Fatalf("Query: %v", err)
217 err = rows.Scan(&r.name, &r.photo)
219 t.Fatalf("Scan: %v", err)
223 corruptMemory := []byte("\xffPHOTO")
225 {name: []byte("Alice"), photo: corruptMemory},
226 {name: []byte("Bob"), photo: corruptMemory},
227 {name: []byte("Chris"), photo: corruptMemory},
229 if !reflect.DeepEqual(got, want) {
230 t.Errorf("mismatch.\n got: %#v\nwant: %#v", got, want)
234 err = db.QueryRow("SELECT|people|photo|name=?", "Alice").Scan(&photo)
236 t.Error("want error scanning into RawBytes from QueryRow")
240 func TestRowsColumns(t *testing.T) {
241 db := newTestDB(t, "people")
243 rows, err := db.Query("SELECT|people|age,name|")
245 t.Fatalf("Query: %v", err)
247 cols, err := rows.Columns()
249 t.Fatalf("Columns: %v", err)
251 want := []string{"age", "name"}
252 if !reflect.DeepEqual(cols, want) {
253 t.Errorf("got %#v; want %#v", cols, want)
257 func TestQueryRow(t *testing.T) {
258 db := newTestDB(t, "people")
262 var birthday time.Time
264 err := db.QueryRow("SELECT|people|age,name|age=?", 3).Scan(&age)
265 if err == nil || !strings.Contains(err.Error(), "expected 2 destination arguments") {
266 t.Errorf("expected error from wrong number of arguments; actually got: %v", err)
269 err = db.QueryRow("SELECT|people|bdate|age=?", 3).Scan(&birthday)
270 if err != nil || !birthday.Equal(chrisBirthday) {
271 t.Errorf("chris birthday = %v, err = %v; want %v", birthday, err, chrisBirthday)
274 err = db.QueryRow("SELECT|people|age,name|age=?", 2).Scan(&age, &name)
276 t.Fatalf("age QueryRow+Scan: %v", err)
279 t.Errorf("expected name Bob, got %q", name)
282 t.Errorf("expected age 2, got %d", age)
285 err = db.QueryRow("SELECT|people|age,name|name=?", "Alice").Scan(&age, &name)
287 t.Fatalf("name QueryRow+Scan: %v", err)
290 t.Errorf("expected name Alice, got %q", name)
293 t.Errorf("expected age 1, got %d", age)
297 err = db.QueryRow("SELECT|people|photo|name=?", "Alice").Scan(&photo)
299 t.Fatalf("photo QueryRow+Scan: %v", err)
301 want := []byte("APHOTO")
302 if !reflect.DeepEqual(photo, want) {
303 t.Errorf("photo = %q; want %q", photo, want)
307 func TestStatementErrorAfterClose(t *testing.T) {
308 db := newTestDB(t, "people")
310 stmt, err := db.Prepare("SELECT|people|age|name=?")
312 t.Fatalf("Prepare: %v", err)
316 t.Fatalf("Close: %v", err)
319 err = stmt.QueryRow("foo").Scan(&name)
321 t.Errorf("expected error from QueryRow.Scan after Stmt.Close")
325 func TestStatementQueryRow(t *testing.T) {
326 db := newTestDB(t, "people")
328 stmt, err := db.Prepare("SELECT|people|age|name=?")
330 t.Fatalf("Prepare: %v", err)
334 for n, tt := range []struct {
342 if err := stmt.QueryRow(tt.name).Scan(&age); err != nil {
343 t.Errorf("%d: on %q, QueryRow/Scan: %v", n, tt.name, err)
344 } else if age != tt.want {
345 t.Errorf("%d: age=%d, want %d", n, age, tt.want)
351 // golang.org/issue/3734
352 func TestStatementQueryRowConcurrent(t *testing.T) {
353 db := newTestDB(t, "people")
355 stmt, err := db.Prepare("SELECT|people|age|name=?")
357 t.Fatalf("Prepare: %v", err)
362 ch := make(chan error, n)
363 for i := 0; i < n; i++ {
366 err := stmt.QueryRow("Alice").Scan(&age)
367 if err == nil && age != 1 {
368 err = fmt.Errorf("unexpected age %d", age)
373 for i := 0; i < n; i++ {
374 if err := <-ch; err != nil {
380 // just a test of fakedb itself
381 func TestBogusPreboundParameters(t *testing.T) {
382 db := newTestDB(t, "foo")
384 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
385 _, err := db.Prepare("INSERT|t1|name=?,age=bogusconversion")
387 t.Fatalf("expected error")
389 if err.Error() != `fakedb: invalid conversion to int32 from "bogusconversion"` {
390 t.Errorf("unexpected error: %v", err)
394 func TestExec(t *testing.T) {
395 db := newTestDB(t, "foo")
397 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
398 stmt, err := db.Prepare("INSERT|t1|name=?,age=?")
400 t.Errorf("Stmt, err = %v, %v", stmt, err)
404 type execTest struct {
408 execTests := []execTest{
410 {[]interface{}{"Brad", 31}, ""},
411 {[]interface{}{"Brad", int64(31)}, ""},
412 {[]interface{}{"Bob", "32"}, ""},
413 {[]interface{}{7, 9}, ""},
415 // Invalid conversions:
416 {[]interface{}{"Brad", int64(0xFFFFFFFF)}, "sql: converting argument #1's type: sql/driver: value 4294967295 overflows int32"},
417 {[]interface{}{"Brad", "strconv fail"}, "sql: converting argument #1's type: sql/driver: value \"strconv fail\" can't be converted to int32"},
419 // Wrong number of args:
420 {[]interface{}{}, "sql: expected 2 arguments, got 0"},
421 {[]interface{}{1, 2, 3}, "sql: expected 2 arguments, got 3"},
423 for n, et := range execTests {
424 _, err := stmt.Exec(et.args...)
429 if errStr != et.wantErr {
430 t.Errorf("stmt.Execute #%d: for %v, got error %q, want error %q",
431 n, et.args, errStr, et.wantErr)
436 func TestTxStmt(t *testing.T) {
437 db := newTestDB(t, "")
439 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
440 stmt, err := db.Prepare("INSERT|t1|name=?,age=?")
442 t.Fatalf("Stmt, err = %v, %v", stmt, err)
445 tx, err := db.Begin()
447 t.Fatalf("Begin = %v", err)
451 _, err = txs.Exec("Bobby", 7)
453 t.Fatalf("Exec = %v", err)
457 t.Fatalf("Commit = %v", err)
461 // Issue: http://golang.org/issue/2784
462 // This test didn't fail before because we got luckly with the fakedb driver.
463 // It was failing, and now not, in github.com/bradfitz/go-sql-test
464 func TestTxQuery(t *testing.T) {
465 db := newTestDB(t, "")
467 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
468 exec(t, db, "INSERT|t1|name=Alice")
470 tx, err := db.Begin()
476 r, err := tx.Query("SELECT|t1|name|")
486 t.Fatal("expected one row")
496 func TestTxQueryInvalid(t *testing.T) {
497 db := newTestDB(t, "")
500 tx, err := db.Begin()
506 _, err = tx.Query("SELECT|t1|name|")
508 t.Fatal("Error expected")
512 // Tests fix for issue 4433, that retries in Begin happen when
513 // conn.Begin() returns ErrBadConn
514 func TestTxErrBadConn(t *testing.T) {
515 db, err := Open("test", fakeDBName+";badConn")
517 t.Fatalf("Open: %v", err)
519 if _, err := db.Exec("WIPE"); err != nil {
520 t.Fatalf("exec wipe: %v", err)
523 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
524 stmt, err := db.Prepare("INSERT|t1|name=?,age=?")
526 t.Fatalf("Stmt, err = %v, %v", stmt, err)
529 tx, err := db.Begin()
531 t.Fatalf("Begin = %v", err)
535 _, err = txs.Exec("Bobby", 7)
537 t.Fatalf("Exec = %v", err)
541 t.Fatalf("Commit = %v", err)
545 // Tests fix for issue 2542, that we release a lock when querying on
546 // a closed connection.
547 func TestIssue2542Deadlock(t *testing.T) {
548 db := newTestDB(t, "people")
550 for i := 0; i < 2; i++ {
551 _, err := db.Query("SELECT|people|age,name|")
553 t.Fatalf("expected error")
558 // From golang.org/issue/3865
559 func TestCloseStmtBeforeRows(t *testing.T) {
560 db := newTestDB(t, "people")
563 s, err := db.Prepare("SELECT|people|name|")
582 // Tests fix for issue 2788, that we bind nil to a []byte if the
583 // value in the column is sql null
584 func TestNullByteSlice(t *testing.T) {
585 db := newTestDB(t, "")
587 exec(t, db, "CREATE|t|id=int32,name=nullstring")
588 exec(t, db, "INSERT|t|id=10,name=?", nil)
592 err := db.QueryRow("SELECT|t|name|id=?", 10).Scan(&name)
597 t.Fatalf("name []byte should be nil for null column value, got: %#v", name)
600 exec(t, db, "INSERT|t|id=11,name=?", "bob")
601 err = db.QueryRow("SELECT|t|name|id=?", 11).Scan(&name)
605 if string(name) != "bob" {
606 t.Fatalf("name []byte should be bob, got: %q", string(name))
610 func TestPointerParamsAndScans(t *testing.T) {
611 db := newTestDB(t, "")
613 exec(t, db, "CREATE|t|id=int32,name=nullstring")
619 exec(t, db, "INSERT|t|id=10,name=?", name)
621 exec(t, db, "INSERT|t|id=20,name=?", name)
623 err := db.QueryRow("SELECT|t|name|id=?", 10).Scan(&name)
625 t.Fatalf("querying id 10: %v", err)
628 t.Errorf("id 10's name = nil; want bob")
629 } else if *name != "bob" {
630 t.Errorf("id 10's name = %q; want bob", *name)
633 err = db.QueryRow("SELECT|t|name|id=?", 20).Scan(&name)
635 t.Fatalf("querying id 20: %v", err)
638 t.Errorf("id 20 = %q; want nil", *name)
642 func TestQueryRowClosingStmt(t *testing.T) {
643 db := newTestDB(t, "people")
647 err := db.QueryRow("SELECT|people|age,name|age=?", 3).Scan(&age, &name)
651 if len(db.freeConn) != 1 {
652 t.Fatalf("expected 1 free conn")
654 fakeConn := db.freeConn[0].ci.(*fakeConn)
655 if made, closed := fakeConn.stmtsMade, fakeConn.stmtsClosed; made != closed {
656 t.Errorf("statement close mismatch: made %d, closed %d", made, closed)
660 type nullTestRow struct {
661 nullParam interface{}
662 notNullParam interface{}
663 scanNullVal interface{}
666 type nullTestSpec struct {
672 func TestNullStringParam(t *testing.T) {
673 spec := nullTestSpec{"nullstring", "string", [6]nullTestRow{
674 {NullString{"aqua", true}, "", NullString{"aqua", true}},
675 {NullString{"brown", false}, "", NullString{"", false}},
676 {"chartreuse", "", NullString{"chartreuse", true}},
677 {NullString{"darkred", true}, "", NullString{"darkred", true}},
678 {NullString{"eel", false}, "", NullString{"", false}},
679 {"foo", NullString{"black", false}, nil},
684 func TestNullInt64Param(t *testing.T) {
685 spec := nullTestSpec{"nullint64", "int64", [6]nullTestRow{
686 {NullInt64{31, true}, 1, NullInt64{31, true}},
687 {NullInt64{-22, false}, 1, NullInt64{0, false}},
688 {22, 1, NullInt64{22, true}},
689 {NullInt64{33, true}, 1, NullInt64{33, true}},
690 {NullInt64{222, false}, 1, NullInt64{0, false}},
691 {0, NullInt64{31, false}, nil},
696 func TestNullFloat64Param(t *testing.T) {
697 spec := nullTestSpec{"nullfloat64", "float64", [6]nullTestRow{
698 {NullFloat64{31.2, true}, 1, NullFloat64{31.2, true}},
699 {NullFloat64{13.1, false}, 1, NullFloat64{0, false}},
700 {-22.9, 1, NullFloat64{-22.9, true}},
701 {NullFloat64{33.81, true}, 1, NullFloat64{33.81, true}},
702 {NullFloat64{222, false}, 1, NullFloat64{0, false}},
703 {10, NullFloat64{31.2, false}, nil},
708 func TestNullBoolParam(t *testing.T) {
709 spec := nullTestSpec{"nullbool", "bool", [6]nullTestRow{
710 {NullBool{false, true}, true, NullBool{false, true}},
711 {NullBool{true, false}, false, NullBool{false, false}},
712 {true, true, NullBool{true, true}},
713 {NullBool{true, true}, false, NullBool{true, true}},
714 {NullBool{true, false}, true, NullBool{false, false}},
715 {true, NullBool{true, false}, nil},
720 func nullTestRun(t *testing.T, spec nullTestSpec) {
721 db := newTestDB(t, "")
723 exec(t, db, fmt.Sprintf("CREATE|t|id=int32,name=string,nullf=%s,notnullf=%s", spec.nullType, spec.notNullType))
725 // Inserts with db.Exec:
726 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 1, "alice", spec.rows[0].nullParam, spec.rows[0].notNullParam)
727 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 2, "bob", spec.rows[1].nullParam, spec.rows[1].notNullParam)
729 // Inserts with a prepared statement:
730 stmt, err := db.Prepare("INSERT|t|id=?,name=?,nullf=?,notnullf=?")
732 t.Fatalf("prepare: %v", err)
735 if _, err := stmt.Exec(3, "chris", spec.rows[2].nullParam, spec.rows[2].notNullParam); err != nil {
736 t.Errorf("exec insert chris: %v", err)
738 if _, err := stmt.Exec(4, "dave", spec.rows[3].nullParam, spec.rows[3].notNullParam); err != nil {
739 t.Errorf("exec insert dave: %v", err)
741 if _, err := stmt.Exec(5, "eleanor", spec.rows[4].nullParam, spec.rows[4].notNullParam); err != nil {
742 t.Errorf("exec insert eleanor: %v", err)
745 // Can't put null val into non-null col
746 if _, err := stmt.Exec(6, "bob", spec.rows[5].nullParam, spec.rows[5].notNullParam); err == nil {
747 t.Errorf("expected error inserting nil val with prepared statement Exec")
750 _, err = db.Exec("INSERT|t|id=?,name=?,nullf=?", 999, nil, nil)
752 // TODO: this test fails, but it's just because
753 // fakeConn implements the optional Execer interface,
754 // so arguably this is the correct behavior. But
755 // maybe I should flesh out the fakeConn.Exec
756 // implementation so this properly fails.
757 // t.Errorf("expected error inserting nil name with Exec")
760 paramtype := reflect.TypeOf(spec.rows[0].nullParam)
761 bindVal := reflect.New(paramtype).Interface()
763 for i := 0; i < 5; i++ {
765 if err := db.QueryRow("SELECT|t|nullf|id=?", id).Scan(bindVal); err != nil {
766 t.Errorf("id=%d Scan: %v", id, err)
768 bindValDeref := reflect.ValueOf(bindVal).Elem().Interface()
769 if !reflect.DeepEqual(bindValDeref, spec.rows[i].scanNullVal) {
770 t.Errorf("id=%d got %#v, want %#v", id, bindValDeref, spec.rows[i].scanNullVal)
775 // golang.org/issue/4859
776 func TestQueryRowNilScanDest(t *testing.T) {
777 db := newTestDB(t, "people")
779 var name *string // nil pointer
780 err := db.QueryRow("SELECT|people|name|").Scan(name)
781 want := "sql: Scan error on column index 0: destination pointer is nil"
782 if err == nil || err.Error() != want {
783 t.Errorf("error = %q; want %q", err.Error(), want)
787 func TestIssue4902(t *testing.T) {
788 db := newTestDB(t, "people")
791 driver := db.driver.(*fakeDriver)
792 opens0 := driver.openCount
796 for i := 0; i < 10; i++ {
797 stmt, err = db.Prepare("SELECT|people|name|")
807 opens := driver.openCount - opens0
809 t.Errorf("opens = %d; want <= 1", opens)
810 t.Logf("db = %#v", db)
811 t.Logf("driver = %#v", driver)
812 t.Logf("stmt = %#v", stmt)
817 // This used to deadlock.
818 func TestSimultaneousQueries(t *testing.T) {
819 db := newTestDB(t, "people")
822 tx, err := db.Begin()
828 r1, err := tx.Query("SELECT|people|name|")
834 r2, err := tx.Query("SELECT|people|name|")
841 func TestMaxIdleConns(t *testing.T) {
842 db := newTestDB(t, "people")
845 tx, err := db.Begin()
850 if got := len(db.freeConn); got != 1 {
851 t.Errorf("freeConns = %d; want 1", got)
854 db.SetMaxIdleConns(0)
856 if got := len(db.freeConn); got != 0 {
857 t.Errorf("freeConns after set to zero = %d; want 0", got)
865 if got := len(db.freeConn); got != 0 {
866 t.Errorf("freeConns = %d; want 0", got)
870 // golang.org/issue/5323
871 func TestStmtCloseDeps(t *testing.T) {
873 t.Skip("skipping in short mode")
875 defer setHookpostCloseConn(nil)
876 setHookpostCloseConn(func(_ *fakeConn, err error) {
878 t.Errorf("Error closing fakeConn: %v", err)
882 db := newTestDB(t, "magicquery")
885 driver := db.driver.(*fakeDriver)
888 opens0 := driver.openCount
889 closes0 := driver.closeCount
891 openDelta0 := opens0 - closes0
893 stmt, err := db.Prepare("SELECT|magicquery|op|op=?,millis=?")
898 // Start 50 parallel slow queries.
904 var wg sync.WaitGroup
905 for batch := 0; batch < nbatch; batch++ {
906 for i := 0; i < nquery; i++ {
911 if err := stmt.QueryRow("sleep", sleepMillis).Scan(&op); err != nil && err != ErrNoRows {
916 // Sleep for twice the expected length of time for the
917 // batch of 50 queries above to finish before starting
919 time.Sleep(2 * sleepMillis * time.Millisecond)
923 if g, w := db.numFreeConns(), 2; g != w {
924 t.Errorf("free conns = %d; want %d", g, w)
927 if n := db.numDepsPollUntil(4, time.Second); n > 4 {
928 t.Errorf("number of dependencies = %d; expected <= 4", n)
933 opens := driver.openCount - opens0
934 closes := driver.closeCount - closes0
936 openDelta := (driver.openCount - driver.closeCount) - openDelta0
939 t.Logf("open calls = %d", opens)
940 t.Logf("close calls = %d", closes)
941 t.Logf("open delta = %d", openDelta)
942 t.Errorf("db connections opened = %d; want <= 2", openDelta)
946 if len(stmt.css) > nquery {
947 t.Errorf("len(stmt.css) = %d; want <= %d", len(stmt.css), nquery)
950 if err := stmt.Close(); err != nil {
954 if g, w := db.numFreeConns(), 2; g != w {
955 t.Errorf("free conns = %d; want %d", g, w)
958 if n := db.numDepsPollUntil(2, time.Second); n > 2 {
959 t.Errorf("number of dependencies = %d; expected <= 2", n)
963 db.SetMaxIdleConns(0)
965 if g, w := db.numFreeConns(), 0; g != w {
966 t.Errorf("free conns = %d; want %d", g, w)
969 if n := db.numDepsPollUntil(0, time.Second); n > 0 {
970 t.Errorf("number of dependencies = %d; expected 0", n)
975 // golang.org/issue/5046
976 func TestCloseConnBeforeStmts(t *testing.T) {
977 db := newTestDB(t, "people")
980 defer setHookpostCloseConn(nil)
981 setHookpostCloseConn(func(_ *fakeConn, err error) {
983 t.Errorf("Error closing fakeConn: %v; from %s", err, stack())
985 t.Errorf("DB = %#v", db)
989 stmt, err := db.Prepare("SELECT|people|name|")
994 if len(db.freeConn) != 1 {
995 t.Fatalf("expected 1 freeConn; got %d", len(db.freeConn))
999 t.Errorf("conn shouldn't be closed")
1002 if n := len(dc.openStmt); n != 1 {
1003 t.Errorf("driverConn num openStmt = %d; want 1", n)
1007 t.Errorf("db Close = %v", err)
1010 t.Errorf("after db.Close, driverConn should be closed")
1012 if n := len(dc.openStmt); n != 0 {
1013 t.Errorf("driverConn num openStmt = %d; want 0", n)
1018 t.Errorf("Stmt close = %v", err)
1022 t.Errorf("conn should be closed")
1025 t.Errorf("after Stmt Close, driverConn's Conn interface should be nil")
1029 // golang.org/issue/5283: don't release the Rows' connection in Close
1030 // before calling Stmt.Close.
1031 func TestRowsCloseOrder(t *testing.T) {
1032 db := newTestDB(t, "people")
1033 defer closeDB(t, db)
1035 db.SetMaxIdleConns(0)
1036 setStrictFakeConnClose(t)
1037 defer setStrictFakeConnClose(nil)
1039 rows, err := db.Query("SELECT|people|age,name|")
1049 func manyConcurrentQueries(t testOrBench) {
1050 maxProcs, numReqs := 16, 500
1051 if testing.Short() {
1052 maxProcs, numReqs = 4, 50
1054 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(maxProcs))
1056 db := newTestDB(t, "people")
1057 defer closeDB(t, db)
1059 stmt, err := db.Prepare("SELECT|people|name|")
1065 var wg sync.WaitGroup
1068 reqs := make(chan bool)
1071 for i := 0; i < maxProcs*2; i++ {
1073 for _ = range reqs {
1074 rows, err := stmt.Query()
1076 t.Errorf("error on query: %v", err)
1092 for i := 0; i < numReqs; i++ {
1099 func TestConcurrency(t *testing.T) {
1100 manyConcurrentQueries(t)
1103 func BenchmarkConcurrency(b *testing.B) {
1105 for i := 0; i < b.N; i++ {
1106 manyConcurrentQueries(b)