Permalink
Browse files

Got vows tests working with teardown, so now the tests exit cleanly b…

…y closing the Redis connections from any open Clients. Added a Makefile to make running tests less verbose to invoke.
  • Loading branch information...
1 parent 648d28e commit abd929b2438124c0c6b346dce581a3f1747bacc8 @bnoguchi committed Nov 19, 2010
View
@@ -0,0 +1,9 @@
+
+VOWS = vows --spec
+
+TESTS = test/*.vows.js
+
+test:
+ @$(VOWS) $(TESTS)
+
+.PHONY: test
@@ -1,18 +1,25 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClientFactory = require("./utils").usingClient,
+ usingClient = usingClientFactory.gen(),
+ usingClient2 = usingClientFactory.gen(),
+ usingClient3 = usingClientFactory.gen(),
assert = require("assert"),
redis = require("../lib/redis");
// TODO Test flushdb and flushall
vows.describe("Redis General Commands").addBatch({
'selecting a new DB': {
topic: function () {
- var client = redis.createClient();
+ var client = this.client = redis.createClient();
client.select(6, this.callback);
},
'should return true': function (err, result) {
assert.isTrue(result);
+ },
+ teardown: function () {
+ this.client.close();
+ delete this.client;
}
},
@@ -157,7 +164,7 @@ vows.describe("Redis General Commands").addBatch({
'using * pattern matching': {
topic: function (client) {
- var client2 = redis.createClient();
+ var client2 = this.client2 = redis.createClient();
client2.select(7);
client2.set("a", 1);
client2.set("b", 2);
@@ -171,12 +178,16 @@ vows.describe("Redis General Commands").addBatch({
["a", "b", "the 3rd key"].forEach( function (val) {
assert.include(list, val);
});
+ },
+ teardown: function () {
+ this.client2.close();
+ delete this.client2;
}
},
'using ? pattern matching': {
topic: function (client) {
- var client2 = redis.createClient();
+ var client2 = this.client2 = redis.createClient();
client2.select(8);
client2.set("bar", 1);
client2.set("car", 2);
@@ -191,13 +202,18 @@ vows.describe("Redis General Commands").addBatch({
["bar", "car", "dar", "far"].forEach( function (val) {
assert.include(list, val);
});
+ },
+
+ teardown: function () {
+ this.client2.close();
+ delete this.client2;
}
}
}),
'the command RANDOMKEY': usingClient({
topic: function (client) {
- var client2 = redis.createClient();
+ var client2 = this.client2 = redis.createClient();
client2.select(9);
client2.set("foo", "bar");
client2.set("hello", "world");
@@ -207,6 +223,11 @@ vows.describe("Redis General Commands").addBatch({
'should return a random key': function (err, key) {
assert.match(key, /^(foo|hello)$/);
+ },
+
+ teardown: function () {
+ this.client2.close();
+ delete this.client2;
}
}),
@@ -310,13 +331,17 @@ vows.describe("Redis General Commands").addBatch({
},
'after moving, when in the destination database': {
topic: function (_, _, client) {
- var client2 = redis.createClient();
+ var client2 = this.client2 = redis.createClient();
client2.select(5);
client2.lrange("db-moving-key", 0, -1, this.callback);
client2.flushdb();
},
'should appear in the destination database': function (err, list) {
assert.deepEqual(list, ["a"]);
+ },
+ teardown: function () {
+ this.client2.close();
+ delete this.client2;
}
},
}
@@ -345,7 +370,7 @@ vows.describe("Redis General Commands").addBatch({
})
}
}).addBatch({
- 'the command DBSIZE': usingClient({
+ 'the command DBSIZE': usingClient2({
topic: function (client) {
client.flushdb();
client.set("foo", "bar");
@@ -358,7 +383,7 @@ vows.describe("Redis General Commands").addBatch({
}
})
}).addBatch({
- 'the command EXPIRE': usingClient({
+ 'the command EXPIRE': usingClient3({
'on a key without a current expiry': {
topic: function (client) {
client.set("to-expire", "foo");
@@ -416,7 +441,7 @@ vows.describe("Redis General Commands").addBatch({
// TODO PERSIST
// TODO Allow passing a date object to EXPIREAT
- 'the command EXPIREAT': usingClient({
+ 'the command EXPIREAT': usingClient3({
'on a key without a current expiry': {
topic: function (client) {
client.set("to-expireat", "foo");
@@ -472,7 +497,7 @@ vows.describe("Redis General Commands").addBatch({
}
}),
- 'the command TTL': usingClient({
+ 'the command TTL': usingClient3({
'for a key with no expiry': {
topic: function (client) {
client.set("ttl-1", "foo");
@@ -1,5 +1,5 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");
@@ -1,5 +1,5 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");
@@ -366,13 +366,17 @@ vows.describe("Redis List Commands").addBatch({
'and then an element is pushed onto that list by another client': {
topic: function (_, client) {
- var client2 = redis.createClient();
+ var client2 = this.client2 = redis.createClient();
client2.select(6);
client.blpop("list-to-add-1-to", 2, this.callback);
client2.rpush("list-to-add-1-to", "just-in-time");
},
'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"]);
+ },
+ teardown: function () {
+ this.client2.close();
+ delete this.client2;
}
}
},
View
@@ -5,40 +5,40 @@ var vows = require("vows"),
sys = require("sys");
vows.describe("Redis PubSub Commands").addBatch({
- 'publishing': {
- topic: function () {
- var client = redis.createClient();
- client.select(6);
+ 'publishing': usingClient.gen()({
+ topic: function (client) {
client.publish("channel-2", "sending this to no-one", this.callback);
},
'should return the number of clients who received the message': function (err, numReceiving) {
assert.equal(numReceiving, 0);
}
- },
+ }),
+
'publishing to a subscribed channel': {
topic: function () {
- var subClient = redis.createClient(),
- pubClient = redis.createClient();
+ var subClient = this.subClient = redis.createClient(),
+ pubClient = this.pubClient = redis.createClient();
subClient.select(6);
pubClient.select(6);
subClient.subscribeTo("channel-1", this.callback);
- subClient.addListener("connected", function () {
+ setTimeout( function () {
pubClient.publish("channel-1", JSON.stringify({a: 1}));
- });
+ }, 1000);
},
'should send the message and channel to the subscriber': function (channel, msg) {
assert.equal(channel, "channel-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': {
- topic: function () {
- var client = redis.createClient();
- client.select(6);
- return client;
- },
+ 'subscribe and unsubscribe': usingClient.gen()({
'subscribing': {
topic: function (client) {
client.subscribe("channel-3");
@@ -57,14 +57,9 @@ vows.describe("Redis PubSub Commands").addBatch({
}
}
}
- },
+ }),
- 'psubscribe and punsubscribe': {
- topic: function () {
- var client = redis.createClient();
- client.select(6);
- return client;
- },
+ 'psubscribe and punsubscribe': usingClient.gen()({
'psubscribing': {
topic: function (client) {
client.psubscribe("channel-5.*", this.callback);
@@ -81,5 +76,5 @@ vows.describe("Redis PubSub Commands").addBatch({
}
}
}
- }
+ })
}).export(module, {error: false});
@@ -1,5 +1,5 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");
@@ -1,5 +1,5 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");
@@ -1,5 +1,6 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
+ usingClient2 = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis"),
fs = require("fs"),
@@ -350,7 +351,7 @@ vows.describe("Redis String Commands").addBatch({
}
})
}).addBatch({
- 'the command SETEX': usingClient({
+ 'the command SETEX': usingClient2({
topic: function (client) {
client.setex("to-expire-1", 2, "foo", this.callback);
},
@@ -1,13 +1,13 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");
var sys = require("sys");
vows.describe("Redis Transactions").addBatch({
'with proper syntax': usingClient({
topic: function (client) {
- var simultClient = redis.createClient();
+ var simultClient = this.simultClient = redis.createClient();
simultClient.select(6);
var self = this;
client.transaction( function () {
@@ -18,6 +18,10 @@ vows.describe("Redis Transactions").addBatch({
},
'should result in changes': function (err, count) {
assert.equal(count, 3);
+ },
+ teardown: function () {
+ this.simultClient.close();
+ delete this.simultClient;
}
}),
'with proper syntax with multibulk': usingClient({
View
@@ -1,21 +1,35 @@
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 currSubContext;
- if (subContexts.hasOwnProperty("topic")) {
- context[""] = subContexts;
- } else {
- for (var subContextName in subContexts) {
- context[subContextName] = subContexts[subContextName];
+
+var usingClient = exports.usingClient = function (client, subContexts) {
+ return function (subContexts) {
+ function setupClient () {
+ client.select(6);
+ client.remainingTests++;
+ return client;
+ }
+ function teardown () {
+ if (--client.remainingTests === 0) {
+ client.close();
+ }
}
- }
- return context;
+ var context = {topic: setupClient, teardown: teardown};
+ 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);
};
@@ -1,5 +1,5 @@
var vows = require("vows"),
- usingClient = require("./utils").usingClient,
+ usingClient = require("./utils").usingClient.gen(),
assert = require("assert"),
redis = require("../lib/redis");

0 comments on commit abd929b

Please sign in to comment.