-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from luin/reconnect-on-error
Support reconnecting on the specified error. #144
- Loading branch information
Showing
6 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
describe('reconnectOnError', function () { | ||
it('should pass the error as the first param', function (done) { | ||
var pending = 2; | ||
function assert(err) { | ||
expect(err.name).to.eql('ReplyError'); | ||
expect(err.command.name).to.eql('set'); | ||
expect(err.command.args).to.eql(['foo']); | ||
if (!--pending) { | ||
done(); | ||
} | ||
} | ||
var redis = new Redis({ | ||
reconnectOnError: function (err) { | ||
assert(err); | ||
} | ||
}); | ||
|
||
redis.set('foo', function (err) { | ||
assert(err); | ||
}); | ||
}); | ||
|
||
it('should not reconnect if reconnectOnError returns false', function (done) { | ||
var redis = new Redis({ | ||
reconnectOnError: function (err) { | ||
return false; | ||
} | ||
}); | ||
|
||
redis.disconnect = function () { | ||
throw new Error('should not disconnect'); | ||
}; | ||
|
||
redis.set('foo', function (err) { | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should reconnect if reconnectOnError returns true or 1', function (done) { | ||
var redis = new Redis({ | ||
reconnectOnError: function () { | ||
return true; | ||
} | ||
}); | ||
|
||
redis.set('foo', function () { | ||
redis.on('ready', function () { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should reconnect and retry the command if reconnectOnError returns 2', function (done) { | ||
var redis = new Redis({ | ||
reconnectOnError: function () { | ||
redis.del('foo'); | ||
return 2; | ||
} | ||
}); | ||
|
||
redis.set('foo', 'bar'); | ||
redis.sadd('foo', 'a', function (err, res) { | ||
expect(res).to.eql(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should select the currect database', function (done) { | ||
var redis = new Redis({ | ||
reconnectOnError: function () { | ||
redis.select(3); | ||
redis.del('foo'); | ||
redis.select(0); | ||
return 2; | ||
} | ||
}); | ||
|
||
redis.select(3); | ||
redis.set('foo', 'bar'); | ||
redis.sadd('foo', 'a', function (err, res) { | ||
expect(res).to.eql(1); | ||
redis.select(3); | ||
redis.type('foo', function (err, type) { | ||
expect(type).to.eql('set'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should work with pipeline', function (done) { | ||
var redis = new Redis({ | ||
reconnectOnError: function () { | ||
redis.del('foo'); | ||
return 2; | ||
} | ||
}); | ||
|
||
redis.set('foo', 'bar'); | ||
redis.pipeline().get('foo').sadd('foo', 'a').exec(function (err, res) { | ||
expect(res).to.eql([[null, 'bar'], [null, 1]]); | ||
done(); | ||
}); | ||
}); | ||
}); |