kara
Version:
A third generation EX-400 android
104 lines (88 loc) • 2.96 kB
JavaScript
var assert = require("chai").assert,
stream = require("stream"),
sinon = require("sinon"),
redis = require("redis"),
Kara = require("../kara");
describe("Redis", function(){
beforeEach(function(done) {
var client = redis.createClient();
client.select(2, function(err, res) {
client.flushdb(function(err, res) {
done();
});
});
});
it("should receive configuration from Kara", function(done) {
var lib = sinon.mock(redis);
client = new redis.RedisClient(new stream.PassThrough());
// var client = redis.createClient(port, host);
lib.expects("createClient").withArgs(1234, "redis.test").returns(client);
// client.select(dbIndex, function(err, done) { ... });
sinon.stub(client, "select", function(dbIndex, callback){
assert.strictEqual(dbIndex, 5);
done();
});
var k = new Kara({ redis: {
port: 1234,
host: "redis.test",
dbIndex: 5,
lib: redis,
}});
k.redis.ping();
setTimeout(client.emit.bind(client, "connect"), 50);
lib.verify();
});
it("should connect using defaults when no configuration is given", function(done) {
var k = new Kara();
k.once("redis:ready", function() {
assert.strictEqual(k.redis.port, 6379);
assert.strictEqual(k.redis.host, "127.0.0.1");
assert.strictEqual(k.redis.selected_db, 0);
done();
});
k.redis.ping();
});
it("should emit redis:ready after selecting a db", function(done) {
var k = new Kara({redis: {dbIndex: 2}});
k.once("redis:ready", function() {
assert.strictEqual(k.redis.selected_db, 2);
done();
});
k.redis.ping();
});
it("should emit redis:error with a bad redis.dbIndex", function(done) {
var k = new Kara({redis: {dbIndex: 100}});
k.once("redis:error", function(err) {
assert.strictEqual(err.message, "ERR invalid DB index");
done();
});
k.redis.ping();
});
it("should emit redis:error with a bad connection attempt", function(done) {
var k = new Kara({redis: {host: "redis.fake.test"}});
k.once("redis:error", function(err) {
assert.strictEqual(err.message, "Redis connection to redis.fake.test:6379 failed - getaddrinfo ENOTFOUND");
done();
});
k.redis.ping();
});
it("should automatically reconnect after losing connection", function(done) {
var k = new Kara({redis: {dbIndex: 2}});
k.once("redis:ready", function() {
k.once("redis:ready", function() { done() });
k.redis.stream.end();
});
k.redis.ping();
});
it("should be available to use before Kara connects", function(done) {
var k = new Kara({
buildTransport: function() { return new stream.PassThrough() },
redis: {dbIndex: 2}
});
k.redis.set("foo", "bar");
k.redis.get("foo", function(err, res) {
assert.strictEqual(res, "bar");
done();
});
});
});