Repository navigation
Expand file tree
/
Copy pathtimeout_test.go
More file actions
141 lines (125 loc) Β· 3.65 KB
/
Copy pathtimeout_test.go
File metadata and controls
141 lines (125 loc) Β· 3.65 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package oro
import (
"context"
"database/sql"
"errors"
"testing"
"time"
)
type timeoutFactory struct {
DefaultFactory
executor Executor
}
func (factory timeoutFactory) NewExecutor(rt *Runtime) Executor {
return factory.executor
}
type deadlineExecutor struct {
deadline chan time.Time
block bool
}
func (executor deadlineExecutor) Query(ctx context.Context, exec ExecContext, sql CompiledSQL) (*RowsResult, error) {
if deadline, ok := ctx.Deadline(); ok {
executor.deadline <- deadline
} else {
executor.deadline <- time.Time{}
}
if executor.block {
<-ctx.Done()
return nil, ctx.Err()
}
return &RowsResult{Rows: []Map{}}, nil
}
func (executor deadlineExecutor) Exec(ctx context.Context, exec ExecContext, sql CompiledSQL) (ExecResult, error) {
if deadline, ok := ctx.Deadline(); ok {
executor.deadline <- deadline
} else {
executor.deadline <- time.Time{}
}
if executor.block {
<-ctx.Done()
return ExecResult{}, ctx.Err()
}
return ExecResult{}, nil
}
func newTimeoutTestDB(t *testing.T, executor Executor, config Config) *DB {
t.Helper()
if config.Connections == nil {
config.Connections = map[string]ConnectionConfig{
"default": {Driver: fakeDriver{db: (*sql.DB)(nil)}},
}
}
config.Factory = timeoutFactory{executor: executor}
db, err := Open(config)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := db.Close(context.Background()); err != nil {
t.Fatal(err)
}
})
return db
}
func TestQueryUsesConfigTimeout(t *testing.T) {
deadlines := make(chan time.Time, 1)
executor := deadlineExecutor{deadline: deadlines}
db := newTimeoutTestDB(t, executor, Config{
Timeout: TimeoutConfig{Query: 50 * time.Millisecond},
})
startedAt := time.Now()
if _, err := db.Table("products").Get(context.Background()); err != nil {
t.Fatal(err)
}
deadline := <-deadlines
if deadline.IsZero() {
t.Fatal("expected query deadline")
}
remaining := time.Until(deadline)
if remaining <= 0 || deadline.Sub(startedAt) > 200*time.Millisecond {
t.Fatalf("unexpected deadline %s remaining %s", deadline, remaining)
}
}
func TestQueryTimeoutOverridesConfigTimeout(t *testing.T) {
deadlines := make(chan time.Time, 1)
executor := deadlineExecutor{deadline: deadlines}
db := newTimeoutTestDB(t, executor, Config{
Timeout: TimeoutConfig{Query: time.Second},
})
startedAt := time.Now()
if _, err := db.Table("products").Timeout(30 * time.Millisecond).Get(context.Background()); err != nil {
t.Fatal(err)
}
deadline := <-deadlines
if deadline.IsZero() {
t.Fatal("expected query deadline")
}
if deadline.Sub(startedAt) > 150*time.Millisecond {
t.Fatalf("expected query timeout override, deadline delta %s", deadline.Sub(startedAt))
}
}
func TestCallerDeadlineIsNotExtended(t *testing.T) {
deadlines := make(chan time.Time, 1)
executor := deadlineExecutor{deadline: deadlines}
db := newTimeoutTestDB(t, executor, Config{
Timeout: TimeoutConfig{Query: time.Second},
})
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
callerDeadline, _ := ctx.Deadline()
if _, err := db.Table("products").Timeout(time.Second).Get(ctx); err != nil {
t.Fatal(err)
}
deadline := <-deadlines
if !deadline.Equal(callerDeadline) {
t.Fatalf("expected caller deadline %s, got %s", callerDeadline, deadline)
}
}
func TestQueryTimeoutReturnsDeadlineExceeded(t *testing.T) {
deadlines := make(chan time.Time, 1)
executor := deadlineExecutor{deadline: deadlines, block: true}
db := newTimeoutTestDB(t, executor, Config{})
_, err := db.Table("products").Timeout(time.Millisecond).Get(context.Background())
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected deadline exceeded, got %v", err)
}
}