Browse files

add test for stability

  • Loading branch information...
1 parent 16dd662 commit 1afc59413f60264d7daf254a23d095b769c36374 @dead-horse dead-horse committed Mar 19, 2012
Showing with 171 additions and 0 deletions.
  1. +92 −0 test/multi_redis_functional_test.js
  2. +79 −0 test/multi_redis_stability_test.js
View
92 test/multi_redis_functional_test.js
@@ -0,0 +1,92 @@
+var multiRedis = require('../lib/multi_redis');
+
+/**
+ * be sure you have a redis server
+ */
+var client;
+var options = {
+ host : ['127.0.0.1', '127.0.0.1'],
+ port : [1239, 1240],
+ debug : false
+}
+describe('functional test', function() {
+ describe('#createClient()', function(){
+ it('should create redis client ok', function(done) {
+ client = multiRedis.createClient(options);
+ client.clients.should.have.length(2);
+ done();
+ })
+ })
+
+ describe('#set()', function() {
+ it('should set ok', function(done){
+ client.set('test', 'mytest', function(err, ok) {
+ (!err).should.be.ok;
+ console.log(ok);
+ ok.should.equal('OK');
+ done();
+ });
+ })
+ })
+
+ describe('#get()', function() {
+ it('should get ok', function(done) {
+ client.get('test', function(err, data) {
+ (!err).should.be.ok;
+ data.should.equal('mytest');
+ done();
+ })
+ })
+ it('should get ok by other server', function(done) {
+ client.get('test', function(err, data) {
+ (!err).should.be.ok;
+ data.should.equal('mytest');
+ done();
+ })
+ })
+ })
+
+ describe('#del()', function() {
+ it('shoud del ok', function(done) {
+ client.del('test', function(err, data){
+ client.get('test', function(err, data) {
+ (!err).should.be.ok;
+ console.log(err, data);
+ (!data).should.be.ok;
+ client.get('test',function(err, data) {
+ (!err).should.be.ok;
+ (!data).should.be.ok;
+ done();
+ })
+ })
+ })
+ })
+ })
+ describe('#onEvent', function() {
+ it('should emit error', function(done) {
+ client.on('error', function(err) {
+ err.message.should.equal('test error');
+ done();
+ })
+ client.clients[0].emit('error', new Error('test error'));
+ })
+ it('should emit end', function(done) {
+ client.on('end', function(client) {
+ client.port.should.equal(1239);
+ done();
+ })
+ client.clients[0].emit('end', client.clients[0]);
+ })
+ })
+ describe('#end()', function() {
+ it('should end all the servers', function(done) {
+ var num = options.port.length;
+ setTimeout(function() {
+ client.clients.should.have.length(0);
+ done();
+ }, 200)
+ client.end();
+ })
+ })
+
+})
View
79 test/multi_redis_stability_test.js
@@ -0,0 +1,79 @@
+var multiRedis = require('../lib/multi_redis');
+
+/**
+ * be sure you have a redis server
+ */
+
+
+var client;
+var redis;
+var options = {
+ server : ['127.0.0.1:1239', '127.0.0.1:1240'],
+ debug : false,
+ speedFirst : true,
+ pingInterval : 3000
+}
+
+describe('multi redis statbility test', function() {
+ describe('#createClient()', function(){
+ it('should create redis client ok', function(done) {
+ client = multiRedis.createClient(options);
+ client.clients.should.have.length(2);
+ done();
+ })
+ })
+
+ describe('#one server down', function() {
+ it('should ok when working server done', function(done) {
+ redis = client.clients[client._getIndex()];
+ redis.emit('end');
+ setTimeout(function() {
+ client.set('test', '123');
+ client.get('test', function(err, data) {
+ (!err).should.be.ok;
+ data.should.equal('123');
+ done();
+ })
+ }, 10);
+ })
+ })
+
+ describe('#reconnect', function() {
+ it('should work fine', function(done) {
+ redis.emit('connect');
+ setTimeout(function() {
+ client.clients.should.have.length(2);
+ client.set('test1', 'name');
+ client.get('test1', function(err, data) {
+ (!err).should.be.ok;
+ data.should.equal('name');
+ done();
+ })
+ },10);
+ })
+ })
+
+ describe('#one server down', function() {
+ it('should work fine when sleeping server done', function(done) {
+ client.clients[client._getIndex()].emit('end');
+ setTimeout(function() {
+ client.set('test2', 'down');
+ client.get('test2', function(err, data) {
+ (!err).should.be.ok;
+ data.should.equal('down');
+ done();
+ });
+ }, 10);
+ })
+ })
+
+ describe('#all server down', function() {
+ it('should get an error', function(done) {
+ client.on('error', function(err){
+ err.message.should.equal('All servers are down.');
+ done();
+ })
+ client.clients[0].emit('end');
+ })
+ })
+})

0 comments on commit 1afc594

Please sign in to comment.