forked from johnnadratowski/golang-neo4j-bolt-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
286 lines (232 loc) · 6.56 KB
/
driver_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package golangNeo4jBoltDriver
import (
"os"
"testing"
"time"
"sync"
"github.com/johnnadratowski/golang-neo4j-bolt-driver/log"
)
var (
neo4jConnStr = ""
)
func TestMain(m *testing.M) {
log.SetLevel(os.Getenv("BOLT_DRIVER_LOG"))
neo4jConnStr = os.Getenv("NEO4J_BOLT")
if neo4jConnStr != "" {
log.Info("Using NEO4J for tests:", neo4jConnStr)
} else if os.Getenv("ENSURE_NEO4J_BOLT") != "" {
log.Fatal("Must give NEO4J_BOLT environment variable")
}
if neo4jConnStr != "" {
// If we're using a DB for testing neo, clear it out after all the test runs
clearNeo()
}
output := m.Run()
if neo4jConnStr != "" {
// If we're using a DB for testing neo, clear it out after all the test runs
clearNeo()
}
os.Exit(output)
}
func clearNeo() {
driver := NewDriver()
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
panic("Error getting conn to clear DB")
}
stmt, err := conn.PrepareNeo(`MATCH (n) DETACH DELETE n`)
if err != nil {
panic("Error getting stmt to clear DB")
}
defer stmt.Close()
_, err = stmt.ExecNeo(nil)
if err != nil {
panic("Error running query to clear DB")
}
}
func TestBoltDriverPool_OpenNeo(t *testing.T) {
if neo4jConnStr == "" {
t.Skip("Cannot run this test when in recording mode")
}
pool, err := NewDriverPool(neo4jConnStr, 25)
if err != nil {
t.Fatalf("An error occurred opening driver pool: %#v", err)
}
now := time.Now().Unix()
for i := 0; i < 25; i++ {
go func() {
c, err := pool.OpenPool()
if err != nil {
t.Fatalf("An error occurred opening conn from pool: %#v", err)
}
defer c.Close()
time.Sleep(time.Millisecond * time.Duration(200))
}()
}
c, err := pool.OpenPool()
if !(time.Now().Unix()-now < 200) {
t.Fatalf("An error occurred opening conn from pool at end: %#v", err)
}
defer c.Close()
}
func TestBoltDriverPool_Concurrent(t *testing.T) {
if neo4jConnStr == "" {
t.Skip("Cannot run this test when in recording mode")
}
var wg sync.WaitGroup
wg.Add(2)
driver, err := NewDriverPool(neo4jConnStr, 2)
if err != nil {
t.Fatalf("An error occurred opening driver pool: %#v", err)
}
one := make(chan bool)
two := make(chan bool)
three := make(chan bool)
four := make(chan bool)
five := make(chan bool)
six := make(chan bool)
seven := make(chan bool)
go func() {
defer wg.Done()
conn, err := driver.OpenPool()
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
defer conn.Close()
data, _, _, err := conn.QueryNeoAll(`MATCH (n) RETURN n`, nil)
if err != nil {
t.Fatalf("An error occurred querying neo: %s", err)
}
log.Info("FIRST: WRITE OUT 1")
one <- true
log.Info("FIRST: FINISHED WRITE OUT 1")
log.Info("FIRST: WAIT ON 2")
<-two
log.Info("FIRST: FINISHED WAIT ON 2")
if len(data) != 0 {
log.Panicf("Expected no data: %#v", data)
}
data, _, _, err = conn.QueryNeoAll(`MATCH (n) RETURN n`, nil)
if err != nil {
log.Panicf("An error occurred querying neo: %s", err)
}
log.Infof("data: %#v", data)
if len(data) != 1 {
log.Panicf("Expected no data: %#v", data)
}
log.Info("FIRST: WRITE OUT 3")
three <- true
log.Info("FIRST: FINISHED WRITE OUT 3")
log.Info("FIRST: WAIT ON 4")
<-four
log.Info("FIRST: FINISHED WAIT ON 4")
data, _, _, err = conn.QueryNeoAll(`MATCH path=(:FOO)-[:BAR]->(:BAZ) RETURN path`, nil)
if err != nil {
log.Panicf("An error occurred querying neo: %s", err)
}
if len(data) != 1 {
log.Panicf("Expected no data: %#v", data)
}
log.Info("FIRST: WRITE OUT 5")
five <- true
log.Info("FIRST: FINISHED WRITE OUT 5")
log.Info("FIRST: WAIT ON 6")
<-six
log.Info("FIRST: FINISHED WAIT ON 6")
data, _, _, err = conn.QueryNeoAll(`MATCH path=(:FOO)-[:BAR]->(:BAZ) RETURN path`, nil)
if err != nil {
log.Panicf("An error occurred querying neo: %s", err)
}
if len(data) != 0 {
log.Panicf("Expected no data: %#v", data)
}
log.Info("FIRST: WRITE OUT 7")
seven <- true
log.Info("FIRST: FINISHED WRITE OUT 7")
}()
go func() {
log.Info("SECOND: WAIT ON 1")
<-one
log.Info("SECOND: FINISHED WAIT ON 1")
defer wg.Done()
conn, err := driver.OpenPool()
if err != nil {
log.Panicf("An error occurred opening conn: %s", err)
}
defer conn.Close()
_, err = conn.ExecNeo(`CREATE (f:FOO)`, nil)
if err != nil {
log.Panicf("An error occurred creating f neo: %s", err)
}
log.Info("SECOND: WRITE OUT 2")
two <- true
log.Info("SECOND: FINISHED WRITE OUT 2")
log.Info("SECOND: WAIT ON 3")
<-three
log.Info("SECOND: FINISHED WAIT ON 3")
_, err = conn.ExecNeo(`MATCH (f:FOO) CREATE UNIQUE (f)-[b:BAR]->(c:BAZ)`, nil)
if err != nil {
log.Panicf("An error occurred creating f neo: %s", err)
}
log.Info("SECOND: WRITE OUT 4")
four <- true
log.Info("SECOND: FINISHED WRITE OUT 4")
log.Info("SECOND: WAIT ON 5")
<-five
log.Info("SECOND: FINISHED WAIT ON 5")
_, err = conn.ExecNeo(`MATCH (:FOO)-[b:BAR]->(:BAZ) DELETE b`, nil)
if err != nil {
log.Panicf("An error occurred creating f neo: %s", err)
}
_, err = conn.ExecNeo(`MATCH (n) DETACH DELETE n`, nil)
if err != nil {
log.Panicf("An error occurred creating f neo: %s", err)
}
log.Info("SECOND: WRITE OUT 6")
six <- true
log.Info("SECOND: FINISHED WRITE OUT 6")
log.Info("SECOND: WAIT ON 7")
<-seven
log.Info("SECOND: FINISHED WAIT ON 7")
}()
wg.Wait()
}
func TestBoltDriverPool_ReclaimBadConn(t *testing.T) {
if neo4jConnStr == "" {
t.Skip("Cannot run this test when in recording mode")
}
driver, err := NewDriverPool(neo4jConnStr, 1)
if err != nil {
t.Fatalf("An error occurred opening driver pool: %#v", err)
}
conn, err := driver.OpenPool()
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
_, err = conn.ExecNeo(`CREATE (f:FOO)`, nil)
if err != nil {
t.Fatalf("An error occurred creating f neo: %s", err)
}
err = conn.(*boltConn).conn.Close()
if err != nil {
t.Fatalf("An error occurred closing underlying connection: %s", err)
}
_, err = conn.ExecNeo(`CREATE (f:FOO)`, nil)
if err == nil {
t.Fatal("An error should have occurred when trying to make a call with a closed connection")
} else if conn.(*boltConn).connErr == nil {
t.Fatal("A connection error should have been associated to the conn after a bad connection")
}
err = conn.Close()
if err != nil {
t.Fatalf("Got an error closing a bad connection: %s", err)
}
conn, err = driver.OpenPool()
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
_, err = conn.ExecNeo(`CREATE (f:FOO)`, nil)
if err != nil {
t.Fatalf("A new conn should have been established for an old conn that had an error. However, we got an error: %s", err)
}
}