diff options
| author | Damien Neil <dneil@google.com> | 2026-02-13 09:40:19 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-25 13:32:34 -0700 |
| commit | bfe4cc85e8f0187f3cdb7c80b9ff5a52be66e08b (patch) | |
| tree | f61f21247eb52db4ed19e13d880008db2860ccf9 /src/database/sql/sql.go | |
| parent | cdfc8c771301c8c1f32e50e773d620a6b8767078 (diff) | |
| download | go-bfe4cc85e8f0187f3cdb7c80b9ff5a52be66e08b.tar.xz | |
database/sql: use synctest in tests
Replace various polling loops waiting for conditions to occur with
synctest-based synchronization. Replace fake time with synctest's
bubbled time.
Reduces time for "go test database/sql -count=10" from
~12s to ~0.5s on my M4 machine.
Change-Id: I7ea8d740d443e27c50df4d2f22aec6136a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/758065
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Neal Patel <nealpatel@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/database/sql/sql.go')
| -rw-r--r-- | src/database/sql/sql.go | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index bb771ccd46..6b7d073359 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -47,9 +47,6 @@ var driversMu sync.RWMutex //go:linkname drivers var drivers = make(map[string]driver.Driver) -// nowFunc returns the current time; it's overridden in tests. -var nowFunc = time.Now - // Register makes a database driver available by the provided name. // If Register is called twice with the same name or if driver is nil, // it panics. @@ -595,7 +592,7 @@ func (dc *driverConn) expired(timeout time.Duration) bool { if timeout <= 0 { return false } - return dc.createdAt.Add(timeout).Before(nowFunc()) + return dc.createdAt.Add(timeout).Before(time.Now()) } // resetSession checks if the driver connection needs the @@ -1151,7 +1148,7 @@ func (db *DB) connectionCleanerRunLocked(d time.Duration) (time.Duration, []*dri if db.maxIdleTime > 0 { // As freeConn is ordered by returnedAt process // in reverse order to minimise the work needed. - idleSince := nowFunc().Add(-db.maxIdleTime) + idleSince := time.Now().Add(-db.maxIdleTime) last := len(db.freeConn) - 1 for i := last; i >= 0; i-- { c := db.freeConn[i] @@ -1176,7 +1173,7 @@ func (db *DB) connectionCleanerRunLocked(d time.Duration) (time.Duration, []*dri } if db.maxLifetime > 0 { - expiredSince := nowFunc().Add(-db.maxLifetime) + expiredSince := time.Now().Add(-db.maxLifetime) for i := 0; i < len(db.freeConn); i++ { c := db.freeConn[i] if c.createdAt.Before(expiredSince) { @@ -1297,8 +1294,8 @@ func (db *DB) openNewConnection(ctx context.Context) { } dc := &driverConn{ db: db, - createdAt: nowFunc(), - returnedAt: nowFunc(), + createdAt: time.Now(), + returnedAt: time.Now(), ci: ci, } if db.putConnDBLocked(dc, err) { @@ -1370,7 +1367,7 @@ func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn db.waitCount++ db.mu.Unlock() - waitStart := nowFunc() + waitStart := time.Now() // Timeout the connection request with the context. select { @@ -1446,8 +1443,8 @@ func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn db.mu.Lock() dc := &driverConn{ db: db, - createdAt: nowFunc(), - returnedAt: nowFunc(), + createdAt: time.Now(), + returnedAt: time.Now(), ci: ci, inUse: true, } @@ -1508,7 +1505,7 @@ func (db *DB) putConn(dc *driverConn, err error, resetSession bool) { db.lastPut[dc] = stack() } dc.inUse = false - dc.returnedAt = nowFunc() + dc.returnedAt = time.Now() for _, fn := range dc.onPut { fn() |
