Skip to content

Commit

Permalink
fix: pipeline with transactions causes unhandled warnings (#884)
Browse files Browse the repository at this point in the history
Pipeline#exec() is a little tricky: it has a init process when
called first time (indicated by whether Pipeline#nodeifiedPromise
is true). Transaction#exec() is similar to that, it has a init
process that parse the result from Pipeline#exec before returning
to users. However, #nodeifiedPromise is not checked here.

Closes: #883
  • Loading branch information
luin authored Jun 8, 2019
1 parent bf92b24 commit bbfd2fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export function addTransactionSupport (redis) {
if (this._transactions > 0) {
exec.call(pipeline)
}

// Returns directly when the pipeline
// has been called multiple times (retries).
if (this.nodeifiedPromise) {
return exec.call(pipeline)
}
const promise = exec.call(pipeline)
return asCallback(promise.then(function (result) {
const execResult = result[result.length - 1]
Expand Down
42 changes: 42 additions & 0 deletions test/functional/cluster/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('cluster:transaction', function () {
var cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
]);

cluster.multi().get('foo').set('foo', 'bar').exec(function (err, result) {
expect(err).to.eql(null);
expect(result[0]).to.eql([null, 'bar']);
Expand Down Expand Up @@ -97,4 +98,45 @@ describe('cluster:transaction', function () {
done();
});
});

it('should not print unhandled warnings', function (done) {
const errorMessage = 'Connection is closed.'
var slotTable = [
[0, 16383, ['127.0.0.1', 30001]]
];
new MockServer(30001, function (argv) {
if (argv[0] === 'exec' || argv[1] === 'foo') {
return new Error(errorMessage)
}
}, slotTable);

var cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
], {
maxRedirections: 3
});

let isDoneCalled = false
const wrapDone = function (error) {
if (isDoneCalled) {
return
}
isDoneCalled = true
process.removeAllListeners('unhandledRejection')
done(error)
}

process.on('unhandledRejection', err => {
wrapDone(new Error('got unhandledRejection: ' + err.message))
})
cluster.multi().get('foo').set('foo', 'bar').exec(function (err) {
expect(err).to.have.property('message', errorMessage)
cluster.on('end', function () {
// Wait for the end event to ensure the transaction
// promise has been resolved.
wrapDone();
})
cluster.disconnect();
});
});
});

0 comments on commit bbfd2fc

Please sign in to comment.