Skip to content

Commit 185f249

Browse files
finish test/basic-transaction-error-test.js
1 parent 8b3f268 commit 185f249

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

test/basic-transaction-error-test.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -118,53 +118,47 @@ test('test error in COMMIT', assert => {
118118
}
119119
}
120120
})
121-
return
122-
test('test error in ROLLBACK', assert => {
121+
122+
test('test error in ROLLBACK: does not reuse connection', assert => {
123123
const domain1 = domain.create()
124+
class RollbackError extends Error {}
124125

125-
db.install(domain1, () => new Promise((_, reject) => {
126-
reject(new TestError('cannot connect'))
127-
}), {maxConcurrency: 0})
128-
126+
db.install(domain1, getConnection, {maxConcurrency: 1})
127+
128+
var connectionPair = null
129129
domain1.run(() => {
130-
return db.transaction(() => {
131-
assert.fail('should not reach here.')
132-
})()
133-
})
134-
.catch(err => assert.fail(err))
135-
.finally(() => domain1.exit())
136-
.finally(assert.end)
137-
})
130+
const first = db.transaction(() => {
131+
return db.getConnection().then(pair => {
132+
connectionPair = pair.pair
133+
pair.release()
134+
throw new Error('any kind of error, really')
135+
})
136+
})().reflect()
138137

139-
// what happens when getConnection fails before BEGIN
140-
test('test getConnection error', assert => {
141-
const domain1 = domain.create()
138+
const second = db.getConnection().then(pair => {
139+
// with concurrency=1, we will try to re-use
140+
// the connection if we can. since we had an error,
141+
// it's best not to use the connection!
142+
assert.notEqual(connectionPair, pair)
143+
pair.release()
144+
})
142145

143-
db.install(domain1, () => new Promise((_, reject) => {
144-
reject(new TestError('cannot connect'))
145-
}), {maxConcurrency: 0})
146-
147-
domain1.run(() => {
148-
return db.transaction(() => {
149-
assert.fail('should not reach here.')
150-
})()
146+
return Promise.join(first, second)
151147
})
152148
.catch(err => assert.fail(err))
153149
.finally(() => domain1.exit())
154150
.finally(assert.end)
155-
})
156151

157-
function innerGetConnection () {
158-
return {
159-
connection: {query (sql, ready) {
160-
if (shouldErrorToggle) {
161-
var err = shouldErrorToggle
162-
shouldErrorToggle = false
163-
return ready(err)
152+
function getConnection () {
153+
return {
154+
connection: {query (sql, ready) {
155+
if (sql === 'ROLLBACK') {
156+
return ready(new RollbackError('failed ROLLBACK'))
157+
}
158+
return ready()
159+
}},
160+
release (err) {
164161
}
165-
return ready()
166-
}},
167-
release (err) {
168162
}
169163
}
170-
}
164+
})

0 commit comments

Comments
 (0)