forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
193 lines (185 loc) · 6.19 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
var assert = require("assert");
var path = require('path');
var config = require("./lib/config");
var RedisProcess = require("./lib/redis-process");
var StunnelProcess = require("./lib/stunnel-process");
var rp;
var stunnel_process;
function startRedis (conf, done) {
RedisProcess.start(function (err, _rp) {
rp = _rp;
return done(err);
}, path.resolve(__dirname, conf));
}
function startStunnel(done) {
StunnelProcess.start(function (err, _stunnel_process) {
stunnel_process = _stunnel_process;
return done(err);
}, path.resolve(__dirname, './conf'));
}
function stopStunnel(done) {
if (stunnel_process) {
StunnelProcess.stop(stunnel_process, done);
} else {
done();
}
}
// don't start redis every time we
// include this helper file!
if (!process.env.REDIS_TESTS_STARTED) {
process.env.REDIS_TESTS_STARTED = true;
before(function (done) {
startRedis('./conf/redis.conf', done);
});
after(function (done) {
if (rp) rp.stop(done);
});
}
module.exports = {
redisProcess: function () {
return rp;
},
stopRedis: function(done) {
rp.stop(done);
},
startRedis: startRedis,
stopStunnel: stopStunnel,
startStunnel: startStunnel,
isNumber: function (expected, done) {
return function (err, results) {
assert.strictEqual(null, err, "expected " + expected + ", got error: " + err);
assert.strictEqual(expected, results, expected + " !== " + results);
assert.strictEqual(typeof results, "number", "expected a number, got " + typeof results);
if (done) return done();
};
},
isString: function (str, done) {
return function (err, results) {
assert.strictEqual(null, err, "expected string '" + str + "', got error: " + err);
assert.equal(str, results, str + " does not match " + results);
if (done) return done();
};
},
isNull: function (done) {
return function (err, results) {
assert.strictEqual(null, err, "expected null, got error: " + err);
assert.strictEqual(null, results, results + " is not null");
if (done) return done();
};
},
isError: function (done) {
return function (err, results) {
assert(err instanceof Error, "err is not instance of 'Error', but an error is expected here.");
if (done) return done();
};
},
isNotError: function (done) {
return function (err, results) {
assert.strictEqual(err, null, "expected success, got an error: " + err);
if (done) return done();
};
},
isType: {
number: function (done) {
return function (err, results) {
assert.strictEqual(null, err, "expected any number, got error: " + err);
assert.strictEqual(typeof results, "number", results + " is not a number");
if (done) return done();
};
},
positiveNumber: function (done) {
return function (err, results) {
assert.strictEqual(null, err, "expected positive number, got error: " + err);
assert.strictEqual(true, (results > 0), results + " is not a positive number");
if (done) return done();
};
}
},
match: function (pattern, done) {
return function (err, results) {
assert.strictEqual(null, err, "expected " + pattern.toString() + ", got error: " + err);
assert(pattern.test(results), "expected string '" + results + "' to match " + pattern.toString());
if (done) return done();
};
},
serverVersionAtLeast: function (connection, desired_version) {
// Return true if the server version >= desired_version
var version = connection.server_info.versions;
for (var i = 0; i < 3; i++) {
if (version[i] > desired_version[i]) {
return true;
}
if (version[i] < desired_version[i]) {
if (this.skip) this.skip();
return false;
}
}
return true;
},
allTests: function (opts, cb) {
if (!cb) {
cb = opts;
opts = {};
}
var parsers = ['javascript'];
var protocols = ['IPv4'];
try {
require('hiredis');
parsers.push('hiredis');
} catch (e) {}
if (process.platform !== 'win32') {
protocols.push('IPv6', '/tmp/redis.sock');
}
var options = [{
detect_buffers: true
}, {
detect_buffers: false
}];
options.forEach(function (options) {
var strOptions = '';
var key;
for (key in options) {
if (options.hasOwnProperty(key)) {
strOptions += key + ': ' + options[key] + '; ';
}
}
describe('using options: ' + strOptions, function() {
parsers.forEach(function (parser) {
protocols.forEach(function (ip, i) {
if (i !== 0 && !opts.allConnections) {
return;
}
cb(parser, ip, config.configureClient(parser, ip, options));
});
});
});
});
},
removeMochaListener: function () {
var mochaListener = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', mochaListener);
return mochaListener;
},
callFuncAfter: function (func, max) {
var i = 0;
return function () {
i++;
if (i === max) {
func();
return true;
}
return false;
};
},
killConnection: function (client) {
// Change the connection option to a non existing one and destroy the stream
client.connection_options = {
port: 65535,
host: '127.0.0.1',
family: 4
};
client.address = '127.0.0.1:65535';
client.stream.destroy();
}
};