Skip to content

Commit 68833fe

Browse files
committed
test: disable postgres serialization retries in unit tests
Unit tests should not require retries.
1 parent 343f8ec commit 68833fe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

coderd/database/db.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"context"
1313
"database/sql"
1414
"errors"
15+
"flag"
1516
"time"
1617

1718
"github.com/jmoiron/sqlx"
@@ -80,6 +81,16 @@ func (q *sqlQuerier) Ping(ctx context.Context) (time.Duration, error) {
8081
return time.Since(start), err
8182
}
8283

84+
// retryAmount is an arbitrary number.
85+
// Unit tests should disable retries.
86+
var retryAmount = 3
87+
88+
func init() {
89+
if flag.Lookup("test.v") != nil {
90+
retryAmount = 1
91+
}
92+
}
93+
8394
func (q *sqlQuerier) InTx(function func(Store) error, txOpts *sql.TxOptions) error {
8495
_, inTx := q.db.(*sqlx.Tx)
8596
isolation := sql.LevelDefault
@@ -93,8 +104,6 @@ func (q *sqlQuerier) InTx(function func(Store) error, txOpts *sql.TxOptions) err
93104
// If we are in a transaction already, the parent InTx call will handle the retry.
94105
// We do not want to duplicate those retries.
95106
if !inTx && isolation == sql.LevelSerializable {
96-
// This is an arbitrarily chosen number.
97-
const retryAmount = 3
98107
var err error
99108
attempts := 0
100109
for attempts = 0; attempts < retryAmount; attempts++ {

0 commit comments

Comments
 (0)