forked from hdachev/fakeredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.test.js
executable file
·2352 lines (1991 loc) · 79.1 KB
/
redis.test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is a slightly modified version of the [email protected] test suite.
// I'll mark all modifications with /*MODIF*/ to keep track of what I change.
function requireLibUtil() { /*MODIF*/
try {
return require("util");
} catch (err) {
return require("sys");
}
}
/*global require console setTimeout process Buffer */
var PORT = 6379;
var HOST = '127.0.0.1';
var redis = require("./main"), /*MODIF*/
client = redis.createClient(PORT, HOST),
client2 = redis.createClient(PORT, HOST),
client3 = redis.createClient(PORT, HOST),
bclient = redis.createClient(PORT, HOST, { return_buffers: true }),
assert = require("assert"),
crypto = require("crypto"),
util = requireLibUtil(), /*MODIF*/
fork = require("child_process").fork,
test_db_num = 15, // this DB will be flushed and used for testing
tests = {},
connected = false,
ended = false,
next, cur_start, run_next_test, all_tests, all_start, test_count;
// Set this to truthy to see the wire protocol and other debugging info
redis.debug_mode = process.argv[2];
function server_version_at_least(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]) return false;
}
return true;
}
function buffers_to_strings(arr) {
return arr.map(function (val) {
return val.toString();
});
}
function require_number(expected, label) {
return function (err, results) {
assert.strictEqual(null, err, label + " expected " + expected + ", got error: " + err);
assert.strictEqual(expected, results, label + " " + expected + " !== " + results);
assert.strictEqual(typeof results, "number", label);
return true;
};
}
function require_number_any(label) {
return function (err, results) {
assert.strictEqual(null, err, label + " expected any number, got error: " + err);
assert.strictEqual(typeof results, "number", label + " " + results + " is not a number");
return true;
};
}
function require_number_pos(label) {
return function (err, results) {
assert.strictEqual(null, err, label + " expected positive number, got error: " + err);
assert.strictEqual(true, (results > 0), label + " " + results + " is not a positive number");
return true;
};
}
function require_string(str, label) {
return function (err, results) {
assert.strictEqual(null, err, label + " expected string '" + str + "', got error: " + err);
assert.equal(str, results, label + " " + str + " does not match " + results);
return true;
};
}
function require_null(label) {
return function (err, results) {
assert.strictEqual(null, err, label + " expected null, got error: " + err);
assert.strictEqual(null, results, label + ": " + results + " is not null");
return true;
};
}
function require_error(label) {
return function (err, results) {
assert.notEqual(err, null, label + " err is null, but an error is expected here.");
return true;
};
}
function is_empty_array(obj) {
return Array.isArray(obj) && obj.length === 0;
}
function last(name, fn) {
return function (err, results) {
fn(err, results);
next(name);
};
}
// Wraps the given callback in a timeout. If the returned function
// is not called within the timeout period, we fail the named test.
function with_timeout(name, cb, millis) {
var timeoutId = setTimeout(function() {
assert.fail("Callback timed out!", name);
}, millis);
return function() {
clearTimeout(timeoutId);
cb.apply(this, arguments);
};
}
next = function next(name) {
console.log('- \x1b[1m' + name.toLowerCase() + '\x1b[0m: \x1b[33m' + (Date.now() - cur_start) + '\x1b[0m ms');
run_next_test();
};
// Tests are run in the order they are defined, so FLUSHDB should always be first.
tests.FLUSHDB = function () {
var name = "FLUSHDB";
client.select(test_db_num, require_string("OK", name));
client2.select(test_db_num, require_string("OK", name));
client3.select(test_db_num, require_string("OK", name));
client.mset("flush keys 1", "flush val 1", "flush keys 2", "flush val 2", require_string("OK", name));
client.FLUSHDB(require_string("OK", name));
client.dbsize(last(name, require_number(0, name)));
};
/*MODIF
tests.INCR = function () {
var name = "INCR";
if (bclient.reply_parser.name == "hiredis") {
console.log("Skipping INCR buffer test with hiredis");
return next(name);
}
// Test incr with the maximum JavaScript number value. Since we are
// returning buffers we should get back one more as a Buffer.
bclient.set("seq", "9007199254740992", function (err, result) {
assert.strictEqual(result.toString(), "OK");
bclient.incr("seq", function (err, result) {
assert.strictEqual("9007199254740993", result.toString());
next(name);
});
});
};
tests.MULTI_1 = function () {
var name = "MULTI_1", multi1, multi2;
// Provoke an error at queue time
multi1 = client.multi();
multi1.mset("multifoo", "10", "multibar", "20", require_string("OK", name));
multi1.set("foo2", require_error(name));
multi1.incr("multifoo", require_number(11, name));
multi1.incr("multibar", require_number(21, name));
multi1.exec(function () {
require_error(name);
// Redis 2.6.5+ will abort transactions with errors
// see: http://redis.io/topics/transactions
var multibar_expected = 22;
var multifoo_expected = 12;
if (server_version_at_least(client, [2, 6, 5])) {
multibar_expected = 1;
multifoo_expected = 1;
}
// Confirm that the previous command, while containing an error, still worked.
multi2 = client.multi();
multi2.incr("multibar", require_number(multibar_expected, name));
multi2.incr("multifoo", require_number(multifoo_expected, name));
multi2.exec(function (err, replies) {
assert.strictEqual(multibar_expected, replies[0]);
assert.strictEqual(multifoo_expected, replies[1]);
next(name);
});
});
};
tests.MULTI_2 = function () {
var name = "MULTI_2";
// test nested multi-bulk replies
client.multi([
["mget", "multifoo", "multibar", function (err, res) {
assert.strictEqual(2, res.length, name);
assert.strictEqual("12", res[0].toString(), name);
assert.strictEqual("22", res[1].toString(), name);
}],
["set", "foo2", require_error(name)],
["incr", "multifoo", require_number(13, name)],
["incr", "multibar", require_number(23, name)]
]).exec(function (err, replies) {
if (server_version_at_least(client, [2, 6, 5])) {
assert.notEqual(err, null, name);
assert.equal(replies, undefined, name);
} else {
assert.strictEqual(2, replies[0].length, name);
assert.strictEqual("12", replies[0][0].toString(), name);
assert.strictEqual("22", replies[0][1].toString(), name);
assert.strictEqual("13", replies[1].toString());
assert.strictEqual("23", replies[2].toString());
}
next(name);
});
};*/
tests.MULTI_3 = function () {
var name = "MULTI_3";
client.sadd("some set", "mem 1");
client.sadd("some set", "mem 2");
client.sadd("some set", "mem 3");
client.sadd("some set", "mem 4");
// make sure empty mb reply works
client.del("some missing set");
client.smembers("some missing set", function (err, reply) {
// make sure empty mb reply works
assert.strictEqual(true, is_empty_array(reply), name);
});
// test nested multi-bulk replies with empty mb elements.
client.multi([
["smembers", "some set"],
["del", "some set"],
["smembers", "some set"]
])
.scard("some set")
.exec(function (err, replies) {
assert.strictEqual(true, is_empty_array(replies[2]), name);
next(name);
});
};
tests.MULTI_4 = function () {
var name = "MULTI_4";
client.multi()
.mset('some', '10', 'keys', '20')
.incr('some')
.incr('keys')
.mget('some', 'keys')
.exec(function (err, replies) {
assert.strictEqual(null, err);
assert.equal('OK', replies[0]);
assert.equal(11, replies[1]);
assert.equal(21, replies[2]);
assert.equal(11, replies[3][0].toString());
assert.equal(21, replies[3][1].toString());
next(name);
});
};
tests.MULTI_5 = function () {
var name = "MULTI_5";
// test nested multi-bulk replies with nulls.
client.multi([
["mget", ["multifoo", "some", "random value", "keys"]],
["incr", "multifoo"]
])
.exec(function (err, replies) {
assert.strictEqual(replies.length, 2, name);
assert.strictEqual(replies[0].length, 4, name);
next(name);
});
};
tests.MULTI_6 = function () {
var name = "MULTI_6";
client.multi()
.hmset("multihash", "a", "foo", "b", 1)
.hmset("multihash", {
extra: "fancy",
things: "here"
})
.hgetall("multihash")
.exec(function (err, replies) {
assert.strictEqual(null, err);
assert.equal("OK", replies[0]);
assert.equal(Object.keys(replies[2]).length, 4);
assert.equal("foo", replies[2].a);
assert.equal("1", replies[2].b);
assert.equal("fancy", replies[2].extra);
assert.equal("here", replies[2].things);
next(name);
});
};
/*MODIF
tests.MULTI_7 = function () {
var name = "MULTI_7";
if (bclient.reply_parser.name != "javascript") {
console.log("Skipping wire-protocol test for 3rd-party parser");
return next(name);
}
var p = require("./lib/parser/javascript");
var parser = new p.Parser(false);
var reply_count = 0;
function check_reply(reply) {
assert.deepEqual(reply, [['a']], "Expecting multi-bulk reply of [['a']]");
reply_count++;
assert.notEqual(reply_count, 4, "Should only parse 3 replies");
}
parser.on("reply", check_reply);
parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na\r\n'));
parser.execute(new Buffer('*1\r\n*1\r'));
parser.execute(new Buffer('\n$1\r\na\r\n'));
parser.execute(new Buffer('*1\r\n*1\r\n'));
parser.execute(new Buffer('$1\r\na\r\n'));
next(name);
};
tests.MULTI_EXCEPTION_1 = function() {
var name = "MULTI_EXCEPTION_1";
if (!server_version_at_least(client, [2, 6, 5])) {
console.log("Skipping " + name + " for old Redis server version < 2.6.5");
return next(name);
}
client.multi().set("foo").exec(function (err, reply) {
assert(Array.isArray(err), "err should be an array");
assert.equal(2, err.length, "err should have 2 items");
assert(err[0].message.match(/ERR/), "First error message should contain ERR");
assert(err[1].message.match(/EXECABORT/), "First error message should contain EXECABORT");
next(name);
});
};
tests.MULTI_8 = function () {
var name = "MULTI_8", multi1, multi2;
// Provoke an error at queue time
multi1 = client.multi();
multi1.mset("multifoo_8", "10", "multibar_8", "20", require_string("OK", name));
multi1.set("foo2", require_error(name));
multi1.set("foo3", require_error(name));
multi1.incr("multifoo_8", require_number(11, name));
multi1.incr("multibar_8", require_number(21, name));
multi1.exec(function () {
require_error(name);
// Redis 2.6.5+ will abort transactions with errors
// see: http://redis.io/topics/transactions
var multibar_expected = 22;
var multifoo_expected = 12;
if (server_version_at_least(client, [2, 6, 5])) {
multibar_expected = 1;
multifoo_expected = 1;
}
// Confirm that the previous command, while containing an error, still worked.
multi2 = client.multi();
multi2.incr("multibar_8", require_number(multibar_expected, name));
multi2.incr("multifoo_8", require_number(multifoo_expected, name));
multi2.exec(function (err, replies) {
assert.strictEqual(multibar_expected, replies[0]);
assert.strictEqual(multifoo_expected, replies[1]);
next(name);
});
});
};
tests.FWD_ERRORS_1 = function () {
var name = "FWD_ERRORS_1";
var toThrow = new Error("Forced exception");
var recordedError = null;
var originalHandlers = client3.listeners("error");
client3.removeAllListeners("error");
client3.once("error", function (err) {
recordedError = err;
});
client3.on("message", function (channel, data) {
console.log("incoming");
if (channel == name) {
assert.equal(data, "Some message");
throw toThrow;
}
});
client3.subscribe(name);
client.publish(name, "Some message");
setTimeout(function () {
client3.listeners("error").push(originalHandlers);
assert.equal(recordedError, toThrow, "Should have caught our forced exception");
next(name);
}, 150);
};
tests.FWD_ERRORS_2 = function () {
var name = "FWD_ERRORS_2";
var toThrow = new Error("Forced exception");
var recordedError = null;
var originalHandler = client.listeners("error").pop();
client.removeAllListeners("error");
client.once("error", function (err) {
recordedError = err;
});
client.get("no_such_key", function (err, reply) {
throw toThrow;
});
setTimeout(function () {
client.listeners("error").push(originalHandler);
assert.equal(recordedError, toThrow, "Should have caught our forced exception");
next(name);
}, 150);
};
tests.FWD_ERRORS_3 = function () {
var name = "FWD_ERRORS_3";
var recordedError = null;
var originalHandler = client.listeners("error").pop();
client.removeAllListeners("error");
client.once("error", function (err) {
recordedError = err;
});
client.send_command("no_such_command", []);
setTimeout(function () {
client.listeners("error").push(originalHandler);
assert.ok(recordedError instanceof Error);
next(name);
}, 150);
};
tests.FWD_ERRORS_4 = function () {
var name = "FWD_ERRORS_4";
var toThrow = new Error("Forced exception");
var recordedError = null;
var originalHandler = client.listeners("error").pop();
client.removeAllListeners("error");
client.once("error", function (err) {
recordedError = err;
});
client.send_command("no_such_command", [], function () {
throw toThrow;
});
setTimeout(function () {
client.listeners("error").push(originalHandler);
assert.equal(recordedError, toThrow, "Should have caught our forced exception");
next(name);
}, 150);
};
tests.EVAL_1 = function () {
var name = "EVAL_1";
if (!server_version_at_least(client, [2, 5, 0])) {
console.log("Skipping " + name + " for old Redis server version < 2.5.x");
return next(name);
}
// test {EVAL - Lua integer -> Redis protocol type conversion}
client.eval("return 100.5", 0, require_number(100, name));
// test {EVAL - Lua string -> Redis protocol type conversion}
client.eval("return 'hello world'", 0, require_string("hello world", name));
// test {EVAL - Lua true boolean -> Redis protocol type conversion}
client.eval("return true", 0, require_number(1, name));
// test {EVAL - Lua false boolean -> Redis protocol type conversion}
client.eval("return false", 0, require_null(name));
// test {EVAL - Lua status code reply -> Redis protocol type conversion}
client.eval("return {ok='fine'}", 0, require_string("fine", name));
// test {EVAL - Lua error reply -> Redis protocol type conversion}
client.eval("return {err='this is an error'}", 0, require_error(name));
// test {EVAL - Lua table -> Redis protocol type conversion}
client.eval("return {1,2,3,'ciao',{1,2}}", 0, function (err, res) {
assert.strictEqual(5, res.length, name);
assert.strictEqual(1, res[0], name);
assert.strictEqual(2, res[1], name);
assert.strictEqual(3, res[2], name);
assert.strictEqual("ciao", res[3], name);
assert.strictEqual(2, res[4].length, name);
assert.strictEqual(1, res[4][0], name);
assert.strictEqual(2, res[4][1], name);
});
// test {EVAL - Are the KEYS and ARGS arrays populated correctly?}
client.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d", function (err, res) {
assert.strictEqual(4, res.length, name);
assert.strictEqual("a", res[0], name);
assert.strictEqual("b", res[1], name);
assert.strictEqual("c", res[2], name);
assert.strictEqual("d", res[3], name);
});
// test {EVAL - parameters in array format gives same result}
client.eval(["return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d"], function (err, res) {
assert.strictEqual(4, res.length, name);
assert.strictEqual("a", res[0], name);
assert.strictEqual("b", res[1], name);
assert.strictEqual("c", res[2], name);
assert.strictEqual("d", res[3], name);
});
// prepare sha sum for evalsha cache test
var source = "return redis.call('get', 'sha test')",
sha = crypto.createHash('sha1').update(source).digest('hex');
client.set("sha test", "eval get sha test", function (err, res) {
if (err) throw err;
// test {EVAL - is Lua able to call Redis API?}
client.eval(source, 0, function (err, res) {
require_string("eval get sha test", name)(err, res);
// test {EVALSHA - Can we call a SHA1 if already defined?}
client.evalsha(sha, 0, require_string("eval get sha test", name));
// test {EVALSHA - Do we get an error on non defined SHA1?}
client.evalsha("ffffffffffffffffffffffffffffffffffffffff", 0, require_error(name));
});
});
// test {EVAL - Redis integer -> Lua type conversion}
client.set("incr key", 0, function (err, reply) {
if (err) throw err;
client.eval("local foo = redis.call('incr','incr key')\n" + "return {type(foo),foo}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length, name);
assert.strictEqual("number", res[0], name);
assert.strictEqual(1, res[1], name);
});
});
client.set("bulk reply key", "bulk reply value", function (err, res) {
// test {EVAL - Redis bulk -> Lua type conversion}
client.eval("local foo = redis.call('get','bulk reply key'); return {type(foo),foo}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length, name);
assert.strictEqual("string", res[0], name);
assert.strictEqual("bulk reply value", res[1], name);
});
});
// test {EVAL - Redis multi bulk -> Lua type conversion}
client.multi()
.del("mylist")
.rpush("mylist", "a")
.rpush("mylist", "b")
.rpush("mylist", "c")
.exec(function (err, replies) {
if (err) throw err;
client.eval("local foo = redis.call('lrange','mylist',0,-1); return {type(foo),foo[1],foo[2],foo[3],# foo}", 0, function (err, res) {
assert.strictEqual(5, res.length, name);
assert.strictEqual("table", res[0], name);
assert.strictEqual("a", res[1], name);
assert.strictEqual("b", res[2], name);
assert.strictEqual("c", res[3], name);
assert.strictEqual(3, res[4], name);
});
});
// test {EVAL - Redis status reply -> Lua type conversion}
client.eval("local foo = redis.call('set','mykey','myval'); return {type(foo),foo['ok']}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length, name);
assert.strictEqual("table", res[0], name);
assert.strictEqual("OK", res[1], name);
});
// test {EVAL - Redis error reply -> Lua type conversion}
client.set("error reply key", "error reply value", function (err, res) {
if (err) throw err;
client.eval("local foo = redis.pcall('incr','error reply key'); return {type(foo),foo['err']}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length, name);
assert.strictEqual("table", res[0], name);
assert.strictEqual("ERR value is not an integer or out of range", res[1], name);
});
});
// test {EVAL - Redis nil bulk reply -> Lua type conversion}
client.del("nil reply key", function (err, res) {
if (err) throw err;
client.eval("local foo = redis.call('get','nil reply key'); return {type(foo),foo == false}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length, name);
assert.strictEqual("boolean", res[0], name);
assert.strictEqual(1, res[1], name);
next(name);
});
});
};
tests.SCRIPT_LOAD = function() {
var name = "SCRIPT_LOAD",
command = "return 1",
commandSha = crypto.createHash('sha1').update(command).digest('hex');
if (!server_version_at_least(client, [2, 6, 0])) {
console.log("Skipping " + name + " for old Redis server version < 2.6.x");
return next(name);
}
bclient.script("load", command, function(err, result) {
assert.strictEqual(result.toString(), commandSha);
client.multi().script("load", command).exec(function(err, result) {
assert.strictEqual(result[0].toString(), commandSha);
client.multi([['script', 'load', command]]).exec(function(err, result) {
assert.strictEqual(result[0].toString(), commandSha);
next(name);
});
});
});
};
tests.CLIENT_LIST = function() {
var name = "CLIENT_LIST";
if (!server_version_at_least(client, [2, 4, 0])) {
console.log("Skipping " + name + " for old Redis server version < 2.4.x");
return next(name);
}
function checkResult(result) {
var lines = result.toString().split('\n').slice(0, -1);
assert.strictEqual(lines.length, 4);
assert(lines.every(function(line) {
return line.match(/^addr=/);
}));
}
bclient.client("list", function(err, result) {
console.log(result.toString());
checkResult(result);
client.multi().client("list").exec(function(err, result) {
console.log(result.toString());
checkResult(result);
client.multi([['client', 'list']]).exec(function(err, result) {
console.log(result.toString());
checkResult(result);
next(name);
});
});
});
};
tests.WATCH_MULTI = function () {
var name = 'WATCH_MULTI', multi;
if (!server_version_at_least(client, [2, 2, 0])) {
console.log("Skipping " + name + " for old Redis server version < 2.2.x");
return next(name);
}
client.watch(name);
client.incr(name);
multi = client.multi();
multi.incr(name);
multi.exec(last(name, require_null(name)));
};
tests.WATCH_TRANSACTION = function () {
var name = "WATCH_TRANSACTION";
if (!server_version_at_least(client, [2, 1, 0])) {
console.log("Skipping " + name + " because server version isn't new enough.");
return next(name);
}
// Test WATCH command aborting transactions, look for parser offset errors.
client.set("unwatched", 200);
client.set(name, 0);
client.watch(name);
client.incr(name);
var multi = client.multi()
.incr(name)
.exec(function (err, replies) {
// Failure expected because of pre-multi incr
assert.strictEqual(replies, null, "Aborted transaction multi-bulk reply should be null.");
client.get("unwatched", function (err, reply) {
assert.equal(err, null, name);
assert.equal(reply, 200, "Expected 200, got " + reply);
next(name);
});
});
client.set("unrelated", 100, function (err, reply) {
assert.equal(err, null, name);
assert.equal(reply, "OK", "Expected 'OK', got " + reply);
});
};*/
tests.detect_buffers = function () {
var name = "detect_buffers", detect_client = redis.createClient(null, null, {detect_buffers: true});
detect_client.on("ready", function () {
// single Buffer or String
detect_client.set("string key 1", "string value");
detect_client.get("string key 1", require_string("string value", name));
detect_client.get(new Buffer("string key 1"), function (err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual(true, Buffer.isBuffer(reply), name);
assert.strictEqual("<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>", reply.inspect(), name);
});
detect_client.hmset("hash key 2", "key 1", "val 1", "key 2", "val 2");
// array of Buffers or Strings
detect_client.hmget("hash key 2", "key 1", "key 2", function (err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual(true, Array.isArray(reply), name);
assert.strictEqual(2, reply.length, name);
assert.strictEqual("val 1", reply[0], name);
assert.strictEqual("val 2", reply[1], name);
});
detect_client.hmget(new Buffer("hash key 2"), "key 1", "key 2", function (err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(2, reply.length, name);
assert.strictEqual(true, Buffer.isBuffer(reply[0]));
assert.strictEqual(true, Buffer.isBuffer(reply[1]));
assert.strictEqual("<Buffer 76 61 6c 20 31>", reply[0].inspect(), name);
assert.strictEqual("<Buffer 76 61 6c 20 32>", reply[1].inspect(), name);
});
// array of strings with undefined values (repro #344)
detect_client.hmget("hash key 2", "key 3", "key 4", function(err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual(true, Array.isArray(reply), name);
assert.strictEqual(2, reply.length, name);
assert.equal(null, reply[0], name);
assert.equal(null, reply[1], name);
});
// Object of Buffers or Strings
detect_client.hgetall("hash key 2", function (err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual("object", typeof reply, name);
assert.strictEqual(2, Object.keys(reply).length, name);
assert.strictEqual("val 1", reply["key 1"], name);
assert.strictEqual("val 2", reply["key 2"], name);
});
detect_client.hgetall(new Buffer("hash key 2"), function (err, reply) {
assert.strictEqual(null, err, name);
assert.strictEqual("object", typeof reply, name);
assert.strictEqual(2, Object.keys(reply).length, name);
assert.strictEqual(true, Buffer.isBuffer(reply["key 1"]));
assert.strictEqual(true, Buffer.isBuffer(reply["key 2"]));
assert.strictEqual("<Buffer 76 61 6c 20 31>", reply["key 1"].inspect(), name);
assert.strictEqual("<Buffer 76 61 6c 20 32>", reply["key 2"].inspect(), name);
});
detect_client.quit(function (err, res) {
next(name);
});
});
};
/*MODIF
tests.socket_nodelay = function () {
var name = "socket_nodelay", c1, c2, c3, ready_count = 0, quit_count = 0;
c1 = redis.createClient(null, null, {socket_nodelay: true});
c2 = redis.createClient(null, null, {socket_nodelay: false});
c3 = redis.createClient(null, null);
function quit_check() {
quit_count++;
if (quit_count === 3) {
next(name);
}
}
function run() {
assert.strictEqual(true, c1.options.socket_nodelay, name);
assert.strictEqual(false, c2.options.socket_nodelay, name);
assert.strictEqual(true, c3.options.socket_nodelay, name);
c1.set(["set key 1", "set val"], require_string("OK", name));
c1.set(["set key 2", "set val"], require_string("OK", name));
c1.get(["set key 1"], require_string("set val", name));
c1.get(["set key 2"], require_string("set val", name));
c2.set(["set key 3", "set val"], require_string("OK", name));
c2.set(["set key 4", "set val"], require_string("OK", name));
c2.get(["set key 3"], require_string("set val", name));
c2.get(["set key 4"], require_string("set val", name));
c3.set(["set key 5", "set val"], require_string("OK", name));
c3.set(["set key 6", "set val"], require_string("OK", name));
c3.get(["set key 5"], require_string("set val", name));
c3.get(["set key 6"], require_string("set val", name));
c1.quit(quit_check);
c2.quit(quit_check);
c3.quit(quit_check);
}
function ready_check() {
ready_count++;
if (ready_count === 3) {
run();
}
}
c1.on("ready", ready_check);
c2.on("ready", ready_check);
c3.on("ready", ready_check);
};
tests.reconnect = function () {
var name = "reconnect";
client.set("recon 1", "one");
client.set("recon 2", "two", function (err, res) {
// Do not do this in normal programs. This is to simulate the server closing on us.
// For orderly shutdown in normal programs, do client.quit()
client.stream.destroy();
});
client.on("reconnecting", function on_recon(params) {
client.on("connect", function on_connect() {
client.select(test_db_num, require_string("OK", name));
client.get("recon 1", require_string("one", name));
client.get("recon 1", require_string("one", name));
client.get("recon 2", require_string("two", name));
client.get("recon 2", require_string("two", name));
client.removeListener("connect", on_connect);
client.removeListener("reconnecting", on_recon);
next(name);
});
});
};
tests.reconnect_select_db_after_pubsub = function() {
var name = "reconnect_select_db_after_pubsub";
client.select(test_db_num);
client.set(name, "one");
client.subscribe('ChannelV', function (err, res) {
client.stream.destroy();
});
client.on("reconnecting", function on_recon(params) {
client.on("ready", function on_connect() {
client.unsubscribe('ChannelV', function (err, res) {
client.get(name, require_string("one", name));
client.removeListener("connect", on_connect);
client.removeListener("reconnecting", on_recon);
next(name);
});
});
});
};
tests.select_error_emits_if_no_callback = function () {
var name = "select_error_emits_if_no_callback";
client.on('error', with_timeout(name, function (err) {
require_error(name)(err);
next(name);
}, 500));
client.select(9999);
};
tests.idle = function () {
var name = "idle";
client.on("idle", function on_idle() {
client.removeListener("idle", on_idle);
next(name);
});
client.set("idle", "test");
};*/
tests.HSET = function () {
var key = "test hash",
field1 = new Buffer("0123456789"),
value1 = new Buffer("abcdefghij"),
field2 = new Buffer(0),
value2 = new Buffer(0),
name = "HSET";
client.HSET(key, field1, value1, require_number(1, name));
client.HGET(key, field1, require_string(value1.toString(), name));
// Empty value
client.HSET(key, field1, value2, require_number(0, name));
client.HGET([key, field1], require_string("", name));
// Empty key, empty value
client.HSET([key, field2, value1], require_number(1, name));
client.HSET(key, field2, value2, last(name, require_number(0, name)));
};
tests.HLEN = function () {
var key = "test hash",
field1 = new Buffer("0123456789"),
value1 = new Buffer("abcdefghij"),
field2 = new Buffer(0),
value2 = new Buffer(0),
name = "HSET",
timeout = 1000;
client.HSET(key, field1, value1, function (err, results) {
client.HLEN(key, function (err, len) {
assert.ok(2 === +len);
next(name);
});
});
};
tests.HMSET_BUFFER_AND_ARRAY = function () {
// Saving a buffer and an array to the same key should not error
var key = "test hash",
field1 = "buffer",
value1 = new Buffer("abcdefghij"),
field2 = "array",
value2 = ["array contents"],
name = "HSET";
client.HMSET(key, field1, value1, field2, value2, last(name, require_string("OK", name)));
};
// TODO - add test for HMSET with optional callbacks
tests.HMGET = function () {
var key1 = "test hash 1", key2 = "test hash 2", key3 = 123456789, name = "HMGET";
// redis-like hmset syntax
client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value", require_string("OK", name));
// fancy hmset syntax
client.HMSET(key2, {
"0123456789": "abcdefghij",
"some manner of key": "a type of value"
}, require_string("OK", name));
// test for numeric key
client.HMSET(key3, {
"0123456789": "abcdefghij",
"some manner of key": "a type of value"
}, require_string("OK", name));
client.HMGET(key1, "0123456789", "some manner of key", function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString(), name);
assert.strictEqual("a type of value", reply[1].toString(), name);
});
client.HMGET(key2, "0123456789", "some manner of key", function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString(), name);
assert.strictEqual("a type of value", reply[1].toString(), name);
});
client.HMGET(key3, "0123456789", "some manner of key", function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString(), name);
assert.strictEqual("a type of value", reply[1].toString(), name);
});
client.HMGET(key1, ["0123456789"], function (err, reply) {
assert.strictEqual("abcdefghij", reply[0], name);
});
client.HMGET(key1, ["0123456789", "some manner of key"], function (err, reply) {
assert.strictEqual("abcdefghij", reply[0], name);
assert.strictEqual("a type of value", reply[1], name);
});
client.HMGET(key1, "missing thing", "another missing thing", function (err, reply) {
assert.strictEqual(null, reply[0], name);
assert.strictEqual(null, reply[1], name);
next(name);
});