1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
package borp_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var unsupportedDrivers map[string]bool = map[string]bool{
"mymysql": true,
}
type SleepDialect interface {
SleepClause(d time.Duration) string
}
func TestWithNotCanceledContext(t *testing.T) {
dbmap := initDBMap(t)
defer dropAndClose(dbmap)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := dbmap.ExecContext(ctx, "SELECT 1")
assert.Nil(t, err)
}
func TestWithCanceledContext(t *testing.T) {
dialect, driver := dialectAndDriver()
if unsupportedDrivers[driver] {
t.Skipf("Cancellation is not yet supported by all drivers. Not known to be supported in %s.", driver)
}
sleepDialect, ok := dialect.(SleepDialect)
if !ok {
t.Skipf("Sleep is not supported in all dialects. Not known to be supported in %s.", driver)
}
dbmap := initDBMap(t)
defer dropAndClose(dbmap)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
startTime := time.Now()
_, err := dbmap.ExecContext(ctx, "SELECT "+sleepDialect.SleepClause(1*time.Second))
if d := time.Since(startTime); d > 500*time.Millisecond {
t.Errorf("too long execution time: %s", d)
}
switch driver {
case "postgres":
if err.Error() != "pq: canceling statement due to user request" {
t.Errorf("expected context.DeadlineExceeded, got %v", err)
}
default:
if err != context.DeadlineExceeded {
t.Errorf("expected context.DeadlineExceeded, got %v", err)
}
}
}
|