Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cluster): quit() ignores errors caused by disconnected connection #720

Merged
merged 2 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,16 @@ class Cluster extends EventEmitter {
return ret
}
return asCallback(
Promise.all(this.nodes().map(function (node) {
return node.quit()
})).then(function () {
return 'OK'
}),
Promise.all(this.nodes().map((node) => (
node.quit().catch((err) => {
// Ignore the error caused by disconnecting since
// we're disconnecting...
if (err.message === CONNECTION_CLOSED_ERROR_MSG) {
return 'OK'
}
throw err
})
))).then(() => 'OK'),
callback
)
}
Expand Down
58 changes: 58 additions & 0 deletions test/functional/cluster/quit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe('cluster:quit', () => {
it('quit successfully when server is disconnecting', (done) => {
const slotTable = [
[0, 1000, ['127.0.0.1', 30001]],
[1001, 16383, ['127.0.0.1', 30002]]
]
const server = new MockServer(30001, (argv, c) => {
if (argv[0] === 'quit') {
c.destroy()
}
}, slotTable)
new MockServer(30002, (argv, c) => {
if (argv[0] === 'quit') {
c.destroy()
}
}, slotTable)

const cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
])
cluster.on('ready', () => {
server.disconnect()
cluster.quit((err, res) => {
expect(err).to.eql(null)
expect(res).to.eql('OK')
done()
})
})
})

it('failed when quit returns error', function (done) {
const ERROR_MESSAGE = 'quit random error'
const slotTable = [
[0, 1000, ['127.0.0.1', 30001]],
[1001, 16383, ['127.0.0.1', 30002]]
]
new MockServer(30001, function (argv, c) {
if (argv[0] === 'quit') {
return new Error(ERROR_MESSAGE)
}
}, slotTable)
new MockServer(30002, function (argv, c) {
if (argv[0] === 'quit') {
c.destroy()
}
}, slotTable)

const cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
])
cluster.on('ready', () => {
cluster.quit((err) => {
expect(err.message).to.eql(ERROR_MESSAGE)
done()
})
})
})
})
7 changes: 6 additions & 1 deletion test/helpers/mock_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ afterEach(function (done) {
}
});

function MockServer(port, handler) {
function MockServer(port, handler, slotTable) {
EventEmitter.call(this);

this.port = port;
this.handler = handler;
this.slotTable = slotTable;

this.clients = [];

Expand All @@ -60,6 +61,10 @@ MockServer.prototype.connect = function () {
if (reply.length === 3 && reply[0].toLowerCase() === 'client' && reply[1].toLowerCase() === 'setname') {
c._connectionName = reply[2]
}
if (_this.slotTable && reply.length === 2 && reply[0].toLowerCase() === 'cluster' && reply[1].toLowerCase() === 'slots') {
_this.write(c, _this.slotTable)
return
}
_this.write(c, _this.handler && _this.handler(reply, c));
},
returnError: function () { }
Expand Down