Skip to content

Commit

Permalink
Merge pull request #877 from dhensby/pulls/v8-pause
Browse files Browse the repository at this point in the history
Integrate pause/resume for msnodesqlv8 driver
  • Loading branch information
willmorgan committed Aug 7, 2019
2 parents 755f19c + 03180aa commit b680381
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
install:
- ps: Install-Product node $env:nodejs_version
- npm install
- npm install msnodesqlv8@0.6.12
- IF "%nodejs_version%" == "10" (npm install msnodesqlv8@0.8.2) ELSE (npm install msnodesqlv8@0.6.12)

cache:
- node_modules
Expand Down
12 changes: 10 additions & 2 deletions lib/msnodesqlv8.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ class Request extends base.Request {
query_timeout: config.requestTimeout / 1000 // msnodesqlv8 timeouts are in seconds (<1 second not supported)
}, params)

this._setCurrentRequest(req)

this._cancel = () => {
debug('request(%d): cancel', IDS.get(this))
req.cancelQuery(err => {
Expand Down Expand Up @@ -791,11 +793,17 @@ class Request extends base.Request {
}

_pause () {
// noop - not implemented in nodesqlv8
super._pause()
if (this._currentRequest) {
this._currentRequest.pauseQuery()
}
}

_resume () {
// noop - not implemented in nodesqlv8
super._resume()
if (this._currentRequest) {
this._currentRequest.resumeQuery()
}
}
}

Expand Down
23 changes: 11 additions & 12 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ module.exports = (sql, driver) => {
req.query('select * from streaming')
req.on('error', (err) => {
if (err.code !== 'ECANCEL') {
req.cancel()
done(err)
}
})
Expand All @@ -1289,50 +1290,48 @@ module.exports = (sql, driver) => {
// cancel the request in 1 second to give time for any more rows to come in
setTimeout(() => {
req.cancel()
assert.strictEqual(rows, 10)
done()
}, 1000)
}
})

req.on('done', function () {
assert.strictEqual(rows, 10)
done()
})
},

'streaming resume' (done) {
let rows = 0
let started = false
let timeout

const req = new TestRequest()
req.stream = true
req.pause()
req.query('select * from streaming')
req.on('error', (err) => {
if (err.code !== 'ECANCEL') {
req.cancel()
clearTimeout(timeout)
done(err)
}
})

// start the request after 1 seecond
setTimeout(() => {
// start the request after 1 second
timeout = setTimeout(() => {
assert.ok(!started)
assert.strictEqual(rows, 0)
started = true
req.resume()
}, 1000)

req.on('row', row => {
assert.ok(started, 'row event received before stream resumed')
rows++
if (rows >= 10) {
req.pause()
req.cancel()
assert.strictEqual(rows, 10)
done()
}
})

req.on('done', function () {
assert.strictEqual(rows, 10)
done()
})
},

'new Table' (done) {
Expand Down
19 changes: 14 additions & 5 deletions test/msnodesqlv8/msnodesqlv8.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ const config = function () {
let connection1 = null
let connection2 = null

function itNode10 () {
const [major] = process.version.split('.')
if (major.substr(1) >= 10) {
it(...arguments)
} else {
it.skip(...arguments)
}
}

describe('msnodesqlv8', function () {
before(done =>
sql.connect(config(), function (err) {
Expand Down Expand Up @@ -63,14 +72,14 @@ describe('msnodesqlv8', function () {
it('query with pipe', done => TESTS['query with pipe'](done))
it('batch', done => TESTS['batch'](done))
it('batch (stream)', done => TESTS['batch'](done, true))
it.skip('create procedure batch (temporarily disabled because of issues introduced in 0.5.3)', done => TESTS['create procedure batch'](done))
it('create procedure batch', done => TESTS['create procedure batch'](done))
it('prepared statement', done => TESTS['prepared statement'](done))
it('prepared statement with duplicate parameters throws', done => TESTS['prepared statement with duplicate parameters throws'](done))
it('prepared statement parameters can be replaced', done => TESTS['prepared statement parameters can be replaced'](done))
it('prepared statement with affected rows', done => TESTS['prepared statement with affected rows'](done))
it('prepared statement in transaction', done => TESTS['prepared statement in transaction'](done))
it('transaction with rollback', done => TESTS['transaction with rollback'](done))
it.skip('transaction with commit (temporarily disabled because of issues introduced in 0.5.3)', done => TESTS['transaction with commit'](done))
it('transaction with commit', done => TESTS['transaction with commit'](done))
it('transaction throws on bad isolation level', done => TESTS['transaction throws on bad isolation level'](done))
it('transaction accepts good isolation levels', done => TESTS['transaction accepts good isolation levels'](done))
it('cancel request', done => TESTS['cancel request'](done))
Expand Down Expand Up @@ -194,8 +203,8 @@ describe('msnodesqlv8', function () {
it.skip('concurrent requests', done => TESTS['concurrent requests'](done))
it.skip('streaming off', done => TESTS['streaming off'](done))
it.skip('streaming on', done => TESTS['streaming on'](done))
it.skip('streaming pause', done => TESTS['streaming pause'](done))
it.skip('streaming resume', done => TESTS['streaming resume'](done))
itNode10('streaming pause', done => TESTS['streaming pause'](done))
itNode10('streaming resume', done => TESTS['streaming resume'](done))

after(done => sql.close(done))
})
Expand All @@ -211,7 +220,7 @@ describe('msnodesqlv8', function () {
after(() => sql.close())
})

after(done =>
after('cleanup', done =>
sql.connect(config(), function (err) {
if (err) return done(err)

Expand Down

0 comments on commit b680381

Please sign in to comment.