forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
75 lines (58 loc) · 1.84 KB
/
context_test.go
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
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build integration
// +build integration
package borp_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Drivers that don't support cancellation.
var unsupportedDrivers map[string]bool = map[string]bool{
"mymysql": true,
}
type SleepDialect interface {
// string to sleep for d duration
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":
// pq doesn't return standard deadline exceeded error
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)
}
}
}