forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.spec.js
178 lines (162 loc) · 6.52 KB
/
utils.spec.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
'use strict';
var assert = require('assert');
var Queue = require('double-ended-queue');
var utils = require('../lib/utils');
var intercept = require('intercept-stdout');
describe('utils.js', function () {
describe('clone', function () {
it('ignore the object prototype and clone a nested array / object', function () {
var obj = {
a: [null, 'foo', ['bar'], {
"i'm special": true
}],
number: 5,
fn: function noop () {}
};
var clone = utils.clone(obj);
assert.deepEqual(clone, obj);
assert.strictEqual(obj.fn, clone.fn);
assert(typeof clone.fn === 'function');
});
it('replace falsy values with an empty object as return value', function () {
var a = utils.clone();
var b = utils.clone(null);
assert.strictEqual(Object.keys(a).length, 0);
assert.strictEqual(Object.keys(b).length, 0);
});
it('transform camelCase options to snake_case and add the camel_case option', function () {
var a = utils.clone({
optionOneTwo: true,
retryStrategy: false,
nested: {
onlyContainCamelCaseOnce: true
},
tls: {
rejectUnauthorized: true
}
});
assert.strictEqual(Object.keys(a).length, 5);
assert.strictEqual(a.option_one_two, true);
assert.strictEqual(a.retry_strategy, false);
assert.strictEqual(a.camel_case, true);
assert.strictEqual(a.tls.rejectUnauthorized, true);
assert.strictEqual(Object.keys(a.nested).length, 1);
});
it('throws on circular data', function () {
try {
var a = {};
a.b = a;
utils.clone(a);
throw new Error('failed');
} catch (e) {
assert(e.message !== 'failed');
}
});
});
describe('print helper', function () {
it('callback with reply', function () {
var text = '';
var unhookIntercept = intercept(function (data) {
text += data;
return '';
});
utils.print(null, 'abc');
unhookIntercept();
assert.strictEqual(text, 'Reply: abc\n');
});
it('callback with error', function () {
var text = '';
var unhookIntercept = intercept(function (data) {
text += data;
return '';
});
utils.print(new Error('Wonderful exception'));
unhookIntercept();
assert.strictEqual(text, 'Error: Wonderful exception\n');
});
});
describe('reply_in_order', function () {
var err_count = 0;
var res_count = 0;
var emitted = false;
var clientMock = {
emit: function () { emitted = true; },
offline_queue: new Queue(),
command_queue: new Queue()
};
var create_command_obj = function () {
return {
callback: function (err, res) {
if (err) err_count++;
else res_count++;
}
};
};
beforeEach(function () {
clientMock.offline_queue.clear();
clientMock.command_queue.clear();
err_count = 0;
res_count = 0;
emitted = false;
});
it('no elements in either queue. Reply in the next tick with callback', function (done) {
var called = false;
utils.reply_in_order(clientMock, function () {
called = true;
done();
}, null, null);
assert(!called);
});
it('no elements in either queue. Reply in the next tick without callback', function (done) {
assert(!emitted);
utils.reply_in_order(clientMock, null, new Error('tada'));
assert(!emitted);
setTimeout(function () {
assert(emitted);
done();
}, 1);
});
it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj callback', function (done) {
clientMock.offline_queue.push(create_command_obj(), create_command_obj());
utils.reply_in_order(clientMock, function () {
assert.strictEqual(clientMock.offline_queue.length, 0);
assert.strictEqual(res_count, 2);
done();
}, null, null);
while (clientMock.offline_queue.length) clientMock.offline_queue.shift().callback(null, 'foo');
});
it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj error emit', function (done) {
clientMock.command_queue.push({}, create_command_obj(), {});
utils.reply_in_order(clientMock, function () {
assert.strictEqual(clientMock.command_queue.length, 0);
assert(emitted);
assert.strictEqual(err_count, 1);
assert.strictEqual(res_count, 0);
done();
}, null, null);
while (clientMock.command_queue.length) {
var command_obj = clientMock.command_queue.shift();
if (command_obj.callback) {
command_obj.callback(new Error('tada'));
}
}
});
it('elements in the offline queue and the command_queue. Reply all other commands got handled respect the command_obj', function (done) {
clientMock.command_queue.push(create_command_obj(), create_command_obj());
clientMock.offline_queue.push(create_command_obj(), {});
utils.reply_in_order(clientMock, function (err, res) {
assert.strictEqual(clientMock.command_queue.length, 0);
assert.strictEqual(clientMock.offline_queue.length, 0);
assert(!emitted);
assert.strictEqual(res_count, 3);
done();
}, null, null);
while (clientMock.offline_queue.length) {
clientMock.command_queue.push(clientMock.offline_queue.shift());
}
while (clientMock.command_queue.length) {
clientMock.command_queue.shift().callback(null, 'hello world');
}
});
});
});