Skip to content

Commit

Permalink
Got vows tests working with teardown, so now the tests exit cleanly b…
Browse files Browse the repository at this point in the history
…y closing the Redis connections from any open Clients. Added a Makefile to make running tests less verbose to invoke.
  • Loading branch information
bnoguchi committed Nov 19, 2010
1 parent 648d28e commit abd929b
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 60 deletions.
9 changes: 9 additions & 0 deletions Makefile
@@ -0,0 +1,9 @@

VOWS = vows --spec

TESTS = test/*.vows.js

test:
@$(VOWS) $(TESTS)

.PHONY: test
45 changes: 35 additions & 10 deletions test/general_commands.vows.js
@@ -1,18 +1,25 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClientFactory = require("./utils").usingClient,
usingClient = usingClientFactory.gen(),
usingClient2 = usingClientFactory.gen(),
usingClient3 = usingClientFactory.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


// TODO Test flushdb and flushall // TODO Test flushdb and flushall
vows.describe("Redis General Commands").addBatch({ vows.describe("Redis General Commands").addBatch({
'selecting a new DB': { 'selecting a new DB': {
topic: function () { topic: function () {
var client = redis.createClient(); var client = this.client = redis.createClient();
client.select(6, this.callback); client.select(6, this.callback);
}, },


'should return true': function (err, result) { 'should return true': function (err, result) {
assert.isTrue(result); assert.isTrue(result);
},
teardown: function () {
this.client.close();
delete this.client;
} }
}, },


Expand Down Expand Up @@ -157,7 +164,7 @@ vows.describe("Redis General Commands").addBatch({


'using * pattern matching': { 'using * pattern matching': {
topic: function (client) { topic: function (client) {
var client2 = redis.createClient(); var client2 = this.client2 = redis.createClient();
client2.select(7); client2.select(7);
client2.set("a", 1); client2.set("a", 1);
client2.set("b", 2); client2.set("b", 2);
Expand All @@ -171,12 +178,16 @@ vows.describe("Redis General Commands").addBatch({
["a", "b", "the 3rd key"].forEach( function (val) { ["a", "b", "the 3rd key"].forEach( function (val) {
assert.include(list, val); assert.include(list, val);
}); });
},
teardown: function () {
this.client2.close();
delete this.client2;
} }
}, },


'using ? pattern matching': { 'using ? pattern matching': {
topic: function (client) { topic: function (client) {
var client2 = redis.createClient(); var client2 = this.client2 = redis.createClient();
client2.select(8); client2.select(8);
client2.set("bar", 1); client2.set("bar", 1);
client2.set("car", 2); client2.set("car", 2);
Expand All @@ -191,13 +202,18 @@ vows.describe("Redis General Commands").addBatch({
["bar", "car", "dar", "far"].forEach( function (val) { ["bar", "car", "dar", "far"].forEach( function (val) {
assert.include(list, val); assert.include(list, val);
}); });
},

teardown: function () {
this.client2.close();
delete this.client2;
} }
} }
}), }),


'the command RANDOMKEY': usingClient({ 'the command RANDOMKEY': usingClient({
topic: function (client) { topic: function (client) {
var client2 = redis.createClient(); var client2 = this.client2 = redis.createClient();
client2.select(9); client2.select(9);
client2.set("foo", "bar"); client2.set("foo", "bar");
client2.set("hello", "world"); client2.set("hello", "world");
Expand All @@ -207,6 +223,11 @@ vows.describe("Redis General Commands").addBatch({


'should return a random key': function (err, key) { 'should return a random key': function (err, key) {
assert.match(key, /^(foo|hello)$/); assert.match(key, /^(foo|hello)$/);
},

teardown: function () {
this.client2.close();
delete this.client2;
} }
}), }),


Expand Down Expand Up @@ -310,13 +331,17 @@ vows.describe("Redis General Commands").addBatch({
}, },
'after moving, when in the destination database': { 'after moving, when in the destination database': {
topic: function (_, _, client) { topic: function (_, _, client) {
var client2 = redis.createClient(); var client2 = this.client2 = redis.createClient();
client2.select(5); client2.select(5);
client2.lrange("db-moving-key", 0, -1, this.callback); client2.lrange("db-moving-key", 0, -1, this.callback);
client2.flushdb(); client2.flushdb();
}, },
'should appear in the destination database': function (err, list) { 'should appear in the destination database': function (err, list) {
assert.deepEqual(list, ["a"]); assert.deepEqual(list, ["a"]);
},
teardown: function () {
this.client2.close();
delete this.client2;
} }
}, },
} }
Expand Down Expand Up @@ -345,7 +370,7 @@ vows.describe("Redis General Commands").addBatch({
}) })
} }
}).addBatch({ }).addBatch({
'the command DBSIZE': usingClient({ 'the command DBSIZE': usingClient2({
topic: function (client) { topic: function (client) {
client.flushdb(); client.flushdb();
client.set("foo", "bar"); client.set("foo", "bar");
Expand All @@ -358,7 +383,7 @@ vows.describe("Redis General Commands").addBatch({
} }
}) })
}).addBatch({ }).addBatch({
'the command EXPIRE': usingClient({ 'the command EXPIRE': usingClient3({
'on a key without a current expiry': { 'on a key without a current expiry': {
topic: function (client) { topic: function (client) {
client.set("to-expire", "foo"); client.set("to-expire", "foo");
Expand Down Expand Up @@ -416,7 +441,7 @@ vows.describe("Redis General Commands").addBatch({


// TODO PERSIST // TODO PERSIST
// TODO Allow passing a date object to EXPIREAT // TODO Allow passing a date object to EXPIREAT
'the command EXPIREAT': usingClient({ 'the command EXPIREAT': usingClient3({
'on a key without a current expiry': { 'on a key without a current expiry': {
topic: function (client) { topic: function (client) {
client.set("to-expireat", "foo"); client.set("to-expireat", "foo");
Expand Down Expand Up @@ -472,7 +497,7 @@ vows.describe("Redis General Commands").addBatch({
} }
}), }),


'the command TTL': usingClient({ 'the command TTL': usingClient3({
'for a key with no expiry': { 'for a key with no expiry': {
topic: function (client) { topic: function (client) {
client.set("ttl-1", "foo"); client.set("ttl-1", "foo");
Expand Down
2 changes: 1 addition & 1 deletion test/hash_commands.vows.js
@@ -1,5 +1,5 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


Expand Down
8 changes: 6 additions & 2 deletions test/list_commands.vows.js
@@ -1,5 +1,5 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


Expand Down Expand Up @@ -366,13 +366,17 @@ vows.describe("Redis List Commands").addBatch({


'and then an element is pushed onto that list by another client': { 'and then an element is pushed onto that list by another client': {
topic: function (_, client) { topic: function (_, client) {
var client2 = redis.createClient(); var client2 = this.client2 = redis.createClient();
client2.select(6); client2.select(6);
client.blpop("list-to-add-1-to", 2, this.callback); client.blpop("list-to-add-1-to", 2, this.callback);
client2.rpush("list-to-add-1-to", "just-in-time"); client2.rpush("list-to-add-1-to", "just-in-time");
}, },
'should pop off the newly pushed element and return [key, elt]': function (err, result) { 'should pop off the newly pushed element and return [key, elt]': function (err, result) {
assert.deepEqual(result, ["list-to-add-1-to", "just-in-time"]); assert.deepEqual(result, ["list-to-add-1-to", "just-in-time"]);
},
teardown: function () {
this.client2.close();
delete this.client2;
} }
} }
}, },
Expand Down
41 changes: 18 additions & 23 deletions test/pubsub.vows.js
Expand Up @@ -5,40 +5,40 @@ var vows = require("vows"),
sys = require("sys"); sys = require("sys");


vows.describe("Redis PubSub Commands").addBatch({ vows.describe("Redis PubSub Commands").addBatch({
'publishing': { 'publishing': usingClient.gen()({
topic: function () { topic: function (client) {
var client = redis.createClient();
client.select(6);
client.publish("channel-2", "sending this to no-one", this.callback); client.publish("channel-2", "sending this to no-one", this.callback);
}, },
'should return the number of clients who received the message': function (err, numReceiving) { 'should return the number of clients who received the message': function (err, numReceiving) {
assert.equal(numReceiving, 0); assert.equal(numReceiving, 0);
} }
}, }),

'publishing to a subscribed channel': { 'publishing to a subscribed channel': {
topic: function () { topic: function () {
var subClient = redis.createClient(), var subClient = this.subClient = redis.createClient(),
pubClient = redis.createClient(); pubClient = this.pubClient = redis.createClient();
subClient.select(6); subClient.select(6);
pubClient.select(6); pubClient.select(6);
subClient.subscribeTo("channel-1", this.callback); subClient.subscribeTo("channel-1", this.callback);
subClient.addListener("connected", function () { setTimeout( function () {
pubClient.publish("channel-1", JSON.stringify({a: 1})); pubClient.publish("channel-1", JSON.stringify({a: 1}));
}); }, 1000);
}, },


'should send the message and channel to the subscriber': function (channel, msg) { 'should send the message and channel to the subscriber': function (channel, msg) {
assert.equal(channel, "channel-1"); assert.equal(channel, "channel-1");
assert.deepEqual(JSON.parse(msg), {a: 1}); assert.deepEqual(JSON.parse(msg), {a: 1});
},
teardown: function () {
this.subClient.close();
this.pubClient.close();
delete this.subClient;
delete this.pubClient;
} }
}, },


'subscribe and unsubscribe': { 'subscribe and unsubscribe': usingClient.gen()({
topic: function () {
var client = redis.createClient();
client.select(6);
return client;
},
'subscribing': { 'subscribing': {
topic: function (client) { topic: function (client) {
client.subscribe("channel-3"); client.subscribe("channel-3");
Expand All @@ -57,14 +57,9 @@ vows.describe("Redis PubSub Commands").addBatch({
} }
} }
} }
}, }),


'psubscribe and punsubscribe': { 'psubscribe and punsubscribe': usingClient.gen()({
topic: function () {
var client = redis.createClient();
client.select(6);
return client;
},
'psubscribing': { 'psubscribing': {
topic: function (client) { topic: function (client) {
client.psubscribe("channel-5.*", this.callback); client.psubscribe("channel-5.*", this.callback);
Expand All @@ -81,5 +76,5 @@ vows.describe("Redis PubSub Commands").addBatch({
} }
} }
} }
} })
}).export(module, {error: false}); }).export(module, {error: false});
2 changes: 1 addition & 1 deletion test/set_commands.vows.js
@@ -1,5 +1,5 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


Expand Down
2 changes: 1 addition & 1 deletion test/sort_command.vows.js
@@ -1,5 +1,5 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


Expand Down
5 changes: 3 additions & 2 deletions test/string_commands.vows.js
@@ -1,5 +1,6 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
usingClient2 = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"), redis = require("../lib/redis"),
fs = require("fs"), fs = require("fs"),
Expand Down Expand Up @@ -350,7 +351,7 @@ vows.describe("Redis String Commands").addBatch({
} }
}) })
}).addBatch({ }).addBatch({
'the command SETEX': usingClient({ 'the command SETEX': usingClient2({
topic: function (client) { topic: function (client) {
client.setex("to-expire-1", 2, "foo", this.callback); client.setex("to-expire-1", 2, "foo", this.callback);
}, },
Expand Down
8 changes: 6 additions & 2 deletions test/transactions.vows.js
@@ -1,13 +1,13 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");
var sys = require("sys"); var sys = require("sys");


vows.describe("Redis Transactions").addBatch({ vows.describe("Redis Transactions").addBatch({
'with proper syntax': usingClient({ 'with proper syntax': usingClient({
topic: function (client) { topic: function (client) {
var simultClient = redis.createClient(); var simultClient = this.simultClient = redis.createClient();
simultClient.select(6); simultClient.select(6);
var self = this; var self = this;
client.transaction( function () { client.transaction( function () {
Expand All @@ -18,6 +18,10 @@ vows.describe("Redis Transactions").addBatch({
}, },
'should result in changes': function (err, count) { 'should result in changes': function (err, count) {
assert.equal(count, 3); assert.equal(count, 3);
},
teardown: function () {
this.simultClient.close();
delete this.simultClient;
} }
}), }),
'with proper syntax with multibulk': usingClient({ 'with proper syntax with multibulk': usingClient({
Expand Down
48 changes: 31 additions & 17 deletions test/utils.js
@@ -1,21 +1,35 @@
var redis = require("../lib/redis"); var redis = require("../lib/redis");
var client = redis.createClient();
client.select(6);
client.flushdb();
var setupClient = function () {
client.select(6);
return client;
};


exports.usingClient = function (subContexts) {
var context = {topic: setupClient}; var usingClient = exports.usingClient = function (client, subContexts) {
var currSubContext; return function (subContexts) {
if (subContexts.hasOwnProperty("topic")) { function setupClient () {
context[""] = subContexts; client.select(6);
} else { client.remainingTests++;
for (var subContextName in subContexts) { return client;
context[subContextName] = subContexts[subContextName]; }
function teardown () {
if (--client.remainingTests === 0) {
client.close();
}
} }
} var context = {topic: setupClient, teardown: teardown};
return context; var currSubContext;
if (subContexts.hasOwnProperty("topic")) {
context[""] = subContexts;
} else {
for (var subContextName in subContexts) {
context[subContextName] = subContexts[subContextName];
}
}
return context;
};
};

usingClient.gen = function (subContexts) {
var client = redis.createClient();
client.select(6);
client.flushdb();
client.remainingTests = 0;
return usingClient(client, subContexts);
}; };
2 changes: 1 addition & 1 deletion test/zset_commands.vows.js
@@ -1,5 +1,5 @@
var vows = require("vows"), var vows = require("vows"),
usingClient = require("./utils").usingClient, usingClient = require("./utils").usingClient.gen(),
assert = require("assert"), assert = require("assert"),
redis = require("../lib/redis"); redis = require("../lib/redis");


Expand Down

0 comments on commit abd929b

Please sign in to comment.