-
Notifications
You must be signed in to change notification settings - Fork 0
/
race_test.go
91 lines (78 loc) · 1.7 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
package pg_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/pg.v4"
)
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 = 10, 1000
if testing.Short() {
C = 4
N = 100
}
})
AfterEach(func() {
err := db.Close()
Expect(err).NotTo(HaveOccurred())
})
It("SelectOrCreate 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").
SelectOrCreate(&a.ID)
Expect(err).NotTo(HaveOccurred())
Expect(a.ID).NotTo(BeZero())
if i%2 == 0 {
err := db.Delete(a)
if err != pg.ErrNoRows {
Expect(err).NotTo(HaveOccurred())
}
}
}
})
count, err := db.Model(&Author{}).Count()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})
It("SelectOrCreate 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").
SelectOrCreate(&a.ID)
Expect(err).NotTo(HaveOccurred())
Expect(a.ID).NotTo(BeZero())
if i%2 == 0 {
err := db.Delete(a)
if err != pg.ErrNoRows {
Expect(err).NotTo(HaveOccurred())
}
}
}
})
count, err := db.Model(&Author{}).Count()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})
})