-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathconcurrent.test.js
More file actions
executable file
·252 lines (216 loc) · 5.96 KB
/
Copy pathconcurrent.test.js
File metadata and controls
executable file
·252 lines (216 loc) · 5.96 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
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
'use strict'
const { TestEnv } = require('./env/test-env')
const env = new TestEnv()
const chai = require('chai')
const assert = chai.assert
const sql = require('../lib/sql')
const { configureTestLogging } = require('./common/logging-helper')
// Configure logging based on environment variables
// By default, tests run silently. To enable logging:
// - MSNODESQLV8_TEST_VERBOSE=true npm test (for full trace logging)
// - MSNODESQLV8_TEST_LOG_LEVEL=DEBUG MSNODESQLV8_TEST_LOG_CONSOLE=true npm test
// - MSNODESQLV8_TEST_LOG_LEVEL=INFO MSNODESQLV8_TEST_LOG_FILE=/tmp/test.log npm test
configureTestLogging(sql)
describe('concurrent', function () {
this.timeout(10000)
this.beforeEach(async function () {
sql.logger.info('Starting test setup', 'concurrent.test.beforeEach')
await env.open()
sql.logger.info('Test environment opened successfully', 'concurrent.test.beforeEach')
})
this.afterEach(async function () {
sql.logger.info('Starting test cleanup', 'concurrent.test.afterEach')
await env.close()
sql.logger.info('Test environment closed successfully', 'concurrent.test.afterEach')
})
function checkClean (connections, testDone) {
const c0 = connections[0]
const c1 = connections[1]
const c2 = connections[2]
const t1 = c0 === c1 && c1 === c2
assert(!t1)
assert(c0.id !== c1.id)
assert(c1.id !== c2.id)
const clean = [
asyncDone => {
c0.close(() => {
asyncDone()
})
},
asyncDone => {
c1.close(() => {
asyncDone()
})
},
asyncDone => {
c2.close(() => {
asyncDone()
})
}
]
env.async.series(clean, () => {
testDone()
})
}
it('open connections in sequence and prove distinct connection objects created', testDone => {
const connections = []
open(conn1 => {
connections.push(conn1)
open(conn2 => {
connections.push(conn2)
open(conn3 => {
connections.push(conn3)
checkClean(connections, testDone)
})
})
})
})
const open = done => {
env.sql.open(env.connectionString, (err, conn) => {
assert.ifError(err)
done(conn)
})
}
it('check for blocked calls to api with event emission', testDone => {
const delays = []
const start = Date.now()
const expected = ['a', 'b', 'c', 'd']
const seq = []
function test () {
assert.deepStrictEqual(expected, seq)
testDone()
}
function pushTest (c) {
seq.push(c)
delays.push(Date.now() - start)
if (seq.length === expected.length) {
test()
}
}
const req = env.theConnection.query('waitfor delay \'00:00:02\';')
req.on('done', () => {
pushTest('a')
process.nextTick(() => {
pushTest('b')
})
setImmediate(() => {
pushTest('d')
})
process.nextTick(() => {
pushTest('c')
})
})
})
it('check for blocked calls to api', testDone => {
const seq = []
const delays = []
const start = Date.now()
const expected = ['a', 'b', 'c', 'd', 'e']
function pushTest (c) {
seq.push(c)
delays.push(Date.now() - start)
if (seq.length === expected.length) {
assert.deepStrictEqual(expected, seq)
testDone()
}
}
pushTest('a')
process.nextTick(() => {
pushTest('c')
})
env.theConnection.query('waitfor delay \'00:00:02\';', () => {
pushTest('e')
})
pushTest('b')
process.nextTick(() => {
pushTest('d')
})
})
it('check for blocked calls to api with nested query', testDone => {
const seq = []
const delays = []
const start = Date.now()
const expected = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
function pushTest (c) {
seq.push(c)
delays.push(Date.now() - start)
if (seq.length === expected.length) {
assert.deepStrictEqual(expected, seq)
testDone()
}
}
pushTest('a')
process.nextTick(() => {
pushTest('c')
})
env.theConnection.query('waitfor delay \'00:00:02\';', [], () => {
pushTest('e')
pushTest('f')
process.nextTick(() => {
pushTest('h')
})
env.theConnection.query('waitfor delay \'00:00:02\';', [], () => {
pushTest('j')
})
pushTest('g')
process.nextTick(() => {
pushTest('i')
})
})
pushTest('b')
process.nextTick(() => {
pushTest('d')
})
})
it('open connections simultaneously and prove distinct connection objects created', testDone => {
const connections = []
open(conn1 => {
connections.push(conn1)
if (connections.length === 3) checkClean(connections, testDone)
})
open(conn2 => {
connections.push(conn2)
if (connections.length === 3) checkClean(connections, testDone)
})
open(conn3 => {
connections.push(conn3)
if (connections.length === 3) checkClean(connections, testDone)
})
})
it('make sure two concurrent connections each have unique spid ', testDone => {
let spid1
let spid2
open(c1 => {
open(c2 => {
c1.query('select @@SPID as id, CURRENT_USER as name', (err, res) => {
assert.ifError(err)
assert(res.length === 1)
spid1 = res[0].id
assert(spid1 !== null)
c2.query('select @@SPID as id, CURRENT_USER as name', (err, res) => {
assert.ifError(err)
assert(res.length === 1)
spid2 = res[0].id
assert(spid2 !== null)
assert(spid1 !== spid2)
const clean = [
asyncDone => {
c1.close(() => {
asyncDone()
})
},
asyncDone => {
c2.close(() => {
asyncDone()
})
}
]
env.async.series(clean, () => {
testDone()
})
})
})
})
})
})
})