Skip to content

Commit da7857c

Browse files
committed
Fix issue with hanging tests
Fix as per this comment: - #24 (comment)
1 parent ab8252d commit da7857c

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,65 @@ module.exports = RedisPubSub;
2626

2727
RedisPubSub.prototype = Object.create(PubSub.prototype);
2828

29-
RedisPubSub.prototype.close = function(callback) {
29+
RedisPubSub.prototype.close = function (callback) {
3030
if (!callback) {
31-
callback = function(err) {
31+
callback = function (err) {
3232
if (err) throw err;
3333
};
3434
}
3535
var pubsub = this;
36-
PubSub.prototype.close.call(this, function(err) {
36+
PubSub.prototype.close.call(this, function (err) {
3737
if (err) return callback(err);
38-
pubsub._close().then(function() {
38+
pubsub._close().then(function () {
3939
callback();
4040
}, callback);
4141
});
4242
};
4343

44-
RedisPubSub.prototype._close = function() {
45-
return this._closing = this._closing || this._connect().then(Promise.all([
46-
close(this.client),
47-
close(this.observer)
48-
]));
44+
RedisPubSub.prototype._close = function () {
45+
var pubsub = this;
46+
47+
if(!this._closing) {
48+
this._closing = this._connect()
49+
.then(function () {
50+
return Promise.all([
51+
close(pubsub.client),
52+
close(pubsub.observer)
53+
])
54+
});
55+
}
56+
57+
return this._closing;
4958
};
5059

51-
RedisPubSub.prototype._subscribe = function(channel, callback) {
60+
RedisPubSub.prototype._subscribe = function (channel, callback) {
5261
var pubsub = this;
5362
pubsub.observer
54-
.subscribe(channel, function(message) {
63+
.subscribe(channel, function (message) {
5564
var data = JSON.parse(message);
5665
pubsub._emit(channel, data);
5766
})
58-
.then(function() {
67+
.then(function () {
5968
callback();
6069
}, callback);
6170
};
6271

63-
RedisPubSub.prototype._unsubscribe = function(channel, callback) {
72+
RedisPubSub.prototype._unsubscribe = function (channel, callback) {
6473
this.observer.unsubscribe(channel)
65-
.then(function() {
74+
.then(function () {
6675
callback();
6776
}, callback);
6877
};
6978

70-
RedisPubSub.prototype._publish = function(channels, data, callback) {
79+
RedisPubSub.prototype._publish = function (channels, data, callback) {
7180
var message = JSON.stringify(data);
7281
var args = [message].concat(channels);
73-
this.client.eval(PUBLISH_SCRIPT, {arguments: args}).then(function() {
82+
this.client.eval(PUBLISH_SCRIPT, { arguments: args }).then(function () {
7483
callback();
7584
}, callback);
7685
};
7786

78-
RedisPubSub.prototype._connect = function() {
87+
RedisPubSub.prototype._connect = function () {
7988
this._clientConnection = this._clientConnection || connect(this.client);
8089
this._observerConnection = this._observerConnection || connect(this.observer);
8190
return Promise.all([
@@ -90,7 +99,7 @@ function connect(client) {
9099

91100
var PUBLISH_SCRIPT =
92101
'for i = 2, #ARGV do ' +
93-
'redis.call("publish", ARGV[i], ARGV[1]) ' +
102+
'redis.call("publish", ARGV[i], ARGV[1]) ' +
94103
'end';
95104

96105
function close(client) {

0 commit comments

Comments
 (0)