Skip to content

Commit

Permalink
Fix detected race in dbconn_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Joos <[email protected]>
  • Loading branch information
danieljoos committed Feb 28, 2024
1 parent db9c819 commit ee52f63
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions go/vt/vttablet/tabletserver/connpool/dbconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -602,20 +603,20 @@ func TestDBExecOnceKillTimeout(t *testing.T) {

// A very long running query that will be killed.
expectedQuery := "select 1"
var timestampQuery time.Time
var timestampQuery atomic.Int64
db.AddQuery(expectedQuery, &sqltypes.Result{})
db.SetBeforeFunc(expectedQuery, func() {
timestampQuery = time.Now()
timestampQuery.Store(time.Now().UnixMicro())
// should take longer than our context deadline below.
time.Sleep(1000 * time.Millisecond)
})

// We expect a kill-query to be fired, too.
// It should also run into a timeout.
var timestampKill time.Time
var timestampKill atomic.Int64
dbConn.killTimeout = 100 * time.Millisecond
db.AddQueryPatternWithCallback(`kill \d+`, &sqltypes.Result{}, func(string) {
timestampKill = time.Now()
timestampKill.Store(time.Now().UnixMicro())
// should take longer than the configured kill timeout above.
time.Sleep(200 * time.Millisecond)
})
Expand All @@ -624,11 +625,13 @@ func TestDBExecOnceKillTimeout(t *testing.T) {
defer cancel()

result, err := dbConn.ExecOnce(ctx, "select 1", 1, false)
timestampDone := time.Now()
timeDone := time.Now()

require.Error(t, err)
require.Equal(t, vtrpcpb.Code_CANCELED, vterrors.Code(err))
require.Nil(t, result)
require.WithinDuration(t, timestampQuery, timestampKill, 150*time.Millisecond)
require.WithinDuration(t, timestampKill, timestampDone, 150*time.Millisecond)
timeQuery := time.UnixMicro(timestampQuery.Load())
timeKill := time.UnixMicro(timestampKill.Load())
require.WithinDuration(t, timeQuery, timeKill, 150*time.Millisecond)
require.WithinDuration(t, timeKill, timeDone, 150*time.Millisecond)
}

0 comments on commit ee52f63

Please sign in to comment.