-
Notifications
You must be signed in to change notification settings - Fork 0
/
race_test.go
187 lines (157 loc) · 3.47 KB
/
race_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package pg_test
import (
"testing"
"time"
"github.com/go-pg/pg"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func concurrency() (int, int) {
if testing.Short() {
return 4, 100
}
return 10, 1000
}
var _ = Describe("DB timeout race", func() {
var db *pg.DB
var C, N int
BeforeEach(func() {
C, N = concurrency()
N = 100
})
AfterEach(func() {
pool := db.Pool()
Expect(pool.Len()).To(Equal(0))
Expect(pool.IdleLen()).To(Equal(0))
err := db.Close()
Expect(err).NotTo(HaveOccurred())
// Give Postgres some time to recover.
time.Sleep(time.Second)
})
test := func() {
It("is race free", func() {
perform(C, func(id int) {
for i := 0; i < N; i++ {
_, err := db.Exec("SELECT pg_sleep(1)")
Expect(err).To(HaveOccurred())
}
})
})
}
Describe("dial timeout", func() {
BeforeEach(func() {
opt := pgOptions()
opt.DialTimeout = time.Nanosecond
db = pg.Connect(opt)
})
test()
})
Describe("read timeout", func() {
BeforeEach(func() {
opt := pgOptions()
opt.ReadTimeout = time.Nanosecond
db = pg.Connect(opt)
})
test()
})
Describe("write timeout", func() {
BeforeEach(func() {
opt := pgOptions()
opt.WriteTimeout = time.Nanosecond
db = pg.Connect(opt)
})
test()
})
})
var _ = Describe("DB race", func() {
var db *pg.DB
var C, N int
BeforeEach(func() {
db = pg.Connect(pgOptions())
err := createTestSchema(db)
Expect(err).NotTo(HaveOccurred())
C, N = concurrency()
})
AfterEach(func() {
err := db.Close()
Expect(err).NotTo(HaveOccurred())
})
It("invalid Scan is race free", func() {
perform(C, func(id int) {
for i := 0; i < N; i++ {
var n int
if i%2 == 0 {
_, err := db.QueryOne(pg.Scan(&n), "SELECT 1, 2")
Expect(err).To(HaveOccurred())
} else {
_, err := db.QueryOne(pg.Scan(&n), "SELECT 123")
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(123))
}
}
})
})
It("SelectOrInsert with OnConflict is race free", func() {
perform(C, func(id int) {
a := &Author{
Name: "R. Scott Bakker",
}
for i := 0; i < N; i++ {
a.ID = 0
_, err := db.Model(a).
Column("id").
Where("name = ?name").
OnConflict("DO NOTHING").
Returning("id").
SelectOrInsert(&a.ID)
Expect(err).NotTo(HaveOccurred())
Expect(a.ID).NotTo(BeZero())
if i%(N/C) == 0 {
err := db.Delete(a)
if err != pg.ErrNoRows {
Expect(err).NotTo(HaveOccurred())
}
}
}
})
count, err := db.Model((*Author)(nil)).Count()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})
It("SelectOrInsert without OnConflict is race free", func() {
perform(C, func(id int) {
a := &Author{
Name: "R. Scott Bakker",
}
for i := 0; i < N; i++ {
a.ID = 0
_, err := db.Model(a).
Column("id").
Where("name = ?name").
Returning("id").
SelectOrInsert(&a.ID)
Expect(err).NotTo(HaveOccurred())
Expect(a.ID).NotTo(BeZero())
if i%(N/C) == 0 {
err := db.Delete(a)
if err != pg.ErrNoRows {
Expect(err).NotTo(HaveOccurred())
}
}
}
})
count, err := db.Model((*Author)(nil)).Count()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})
It("fully initializes model table", func() {
type TestTable struct {
tableName struct{} `sql:"'generate_series(0, 9)'"`
}
perform(C, func(id int) {
n, err := db.Model((*TestTable)(nil)).Count()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(10))
})
})
})