From abd929b2438124c0c6b346dce581a3f1747bacc8 Mon Sep 17 00:00:00 2001 From: bnoguchi Date: Fri, 19 Nov 2010 01:31:20 -0800 Subject: [PATCH] Got vows tests working with teardown, so now the tests exit cleanly by closing the Redis connections from any open Clients. Added a Makefile to make running tests less verbose to invoke. --- Makefile | 9 +++++++ test/general_commands.vows.js | 45 ++++++++++++++++++++++++-------- test/hash_commands.vows.js | 2 +- test/list_commands.vows.js | 8 ++++-- test/pubsub.vows.js | 41 +++++++++++++----------------- test/set_commands.vows.js | 2 +- test/sort_command.vows.js | 2 +- test/string_commands.vows.js | 5 ++-- test/transactions.vows.js | 8 ++++-- test/utils.js | 48 ++++++++++++++++++++++------------- test/zset_commands.vows.js | 2 +- 11 files changed, 112 insertions(+), 60 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..702e6c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ + +VOWS = vows --spec + +TESTS = test/*.vows.js + +test: + @$(VOWS) $(TESTS) + +.PHONY: test diff --git a/test/general_commands.vows.js b/test/general_commands.vows.js index 0e56b66..0448a95 100644 --- a/test/general_commands.vows.js +++ b/test/general_commands.vows.js @@ -1,5 +1,8 @@ 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"); @@ -7,12 +10,16 @@ var vows = require("vows"), 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"); diff --git a/test/hash_commands.vows.js b/test/hash_commands.vows.js index d227ab3..a6166e6 100644 --- a/test/hash_commands.vows.js +++ b/test/hash_commands.vows.js @@ -1,5 +1,5 @@ var vows = require("vows"), - usingClient = require("./utils").usingClient, + usingClient = require("./utils").usingClient.gen(), assert = require("assert"), redis = require("../lib/redis"); diff --git a/test/list_commands.vows.js b/test/list_commands.vows.js index a9cdc53..32280bc 100644 --- a/test/list_commands.vows.js +++ b/test/list_commands.vows.js @@ -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; } } }, diff --git a/test/pubsub.vows.js b/test/pubsub.vows.js index 27f0b15..6e40526 100644 --- a/test/pubsub.vows.js +++ b/test/pubsub.vows.js @@ -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}); diff --git a/test/set_commands.vows.js b/test/set_commands.vows.js index 2672a85..d0712a9 100644 --- a/test/set_commands.vows.js +++ b/test/set_commands.vows.js @@ -1,5 +1,5 @@ var vows = require("vows"), - usingClient = require("./utils").usingClient, + usingClient = require("./utils").usingClient.gen(), assert = require("assert"), redis = require("../lib/redis"); diff --git a/test/sort_command.vows.js b/test/sort_command.vows.js index 4c34bf0..7219331 100644 --- a/test/sort_command.vows.js +++ b/test/sort_command.vows.js @@ -1,5 +1,5 @@ var vows = require("vows"), - usingClient = require("./utils").usingClient, + usingClient = require("./utils").usingClient.gen(), assert = require("assert"), redis = require("../lib/redis"); diff --git a/test/string_commands.vows.js b/test/string_commands.vows.js index 2fbb16a..85b73fe 100644 --- a/test/string_commands.vows.js +++ b/test/string_commands.vows.js @@ -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); }, diff --git a/test/transactions.vows.js b/test/transactions.vows.js index 93df69c..dd0b03b 100644 --- a/test/transactions.vows.js +++ b/test/transactions.vows.js @@ -1,5 +1,5 @@ var vows = require("vows"), - usingClient = require("./utils").usingClient, + usingClient = require("./utils").usingClient.gen(), assert = require("assert"), redis = require("../lib/redis"); var sys = require("sys"); @@ -7,7 +7,7 @@ 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({ diff --git a/test/utils.js b/test/utils.js index 355fe1a..8edd18e 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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); }; diff --git a/test/zset_commands.vows.js b/test/zset_commands.vows.js index 2d52025..8a56f20 100644 --- a/test/zset_commands.vows.js +++ b/test/zset_commands.vows.js @@ -1,5 +1,5 @@ var vows = require("vows"), - usingClient = require("./utils").usingClient, + usingClient = require("./utils").usingClient.gen(), assert = require("assert"), redis = require("../lib/redis");