forked from carlos8f/haredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-singlemode.js
1427 lines (1213 loc) · 50.6 KB
/
test-singlemode.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
/*global require console setTimeout process Buffer */
var redis = require("./index"),
client = redis.createClient(),
client2 = redis.createClient(),
client3 = redis.createClient(),
assert = require("assert"),
util = require("util"),
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 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, "result sent back unexpected 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, "result sent back unexpected 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, "result sent back unexpected 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, "result sent back unexpected 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, "result sent back unexpected 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);
};
}
next = function next(name) {
console.log(" \x1b[33m" + (Date.now() - cur_start) + "\x1b[0m ms");
run_next_test();
};
// Tests are run in the order they are defined. So FLUSHDB should be stay 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)));
};
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));
// newer server version cancels transaction with EXECABORT, so disable this test.
//multi1.set("foo2", require_error(name));
multi1.incr("multifoo", require_number(11, name));
multi1.incr("multibar", require_number(21, name));
multi1.exec();
// Confirm that the previous command, while containing an error, still worked.
multi2 = client.multi();
multi2.incr("multibar", require_number(22, name));
multi2.incr("multifoo", require_number(12, name));
multi2.exec(function (err, replies) {
assert.strictEqual(22, replies[0]);
assert.strictEqual(12, 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);
}],
// newer server version cancels transaction with EXECABORT, so disable this test.
//["set", "foo2", require_error(name)],
["incr", "multifoo", require_number(13, name)],
["incr", "multibar", require_number(23, name)]
]).exec(function (err, replies) {
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);
});
};
tests.EVAL_1 = function () {
var name = "EVAL_1";
if (client.server_info.versions[0] >= 2 && client.server_info.versions[1] >= 9) {
// 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 - is Lua able to call Redis API?}
client.set("mykey", "myval");
client.eval("return redis.call('get','mykey')", 0, require_string("myval", name));
// test {EVALSHA - Can we call a SHA1 if already defined?}
client.evalsha("9bd632c7d33e571e9f24556ebed26c3479a87129", 0, require_string("myval", 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("x", 0);
client.eval("local foo = redis.call('incr','x')\n" + "return {type(foo),foo}", 0, function (err, res) {
assert.strictEqual(2, res.length, name);
assert.strictEqual("number", res[0], name);
assert.strictEqual(1, res[1], name);
});
// test {EVAL - Redis bulk -> Lua type conversion}
client.eval("local foo = redis.call('get','mykey'); return {type(foo),foo}", 0, function (err, res) {
assert.strictEqual(2, res.length, name);
assert.strictEqual("string", res[0], name);
assert.strictEqual("myval", res[1], name);
});
// test {EVAL - Redis multi bulk -> Lua type conversion}
client.del("mylist");
client.rpush("mylist", "a");
client.rpush("mylist", "b");
client.rpush("mylist", "c");
client.eval("local foo = redis.call('lrange','mylist',0,-1)\n" + "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) {
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("mykey", "myval");
client.eval("local foo = redis.call('incr','mykey'); return {type(foo),foo['err']}", 0, function (err, res) {
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("mykey");
client.eval("local foo = redis.call('get','mykey'); return {type(foo),foo == false}", 0, function (err, res) {
assert.strictEqual(2, res.length, name);
assert.strictEqual("boolean", res[0], name);
assert.strictEqual(1, res[1], name);
});
// test {EVAL - Script can't run more than configured time limit} {
client.config("set", "lua-time-limit", 1);
client.eval("local i = 0; while true do i=i+1 end", 0, last("name", require_error(name)));
} else {
console.log("Skipping " + name + " because server version isn't new enough.");
next(name);
}
};
tests.WATCH_MULTI = function () {
var name = 'WATCH_MULTI', multi;
if (client.server_info.versions[0] >= 2 && client.server_info.versions[1] >= 1) {
client.watch(name);
client.incr(name);
multi = client.multi();
multi.incr(name);
multi.exec(last(name, require_null(name)));
} else {
console.log("Skipping " + name + " because server version isn't new enough.");
next(name);
}
};
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);
});
// 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);
});
});
};
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.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.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", 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));
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(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);
});
};
tests.HINCRBY = function () {
var name = "HINCRBY";
client.hset("hash incr", "value", 10, require_number(1, name));
client.HINCRBY("hash incr", "value", 1, require_number(11, name));
client.HINCRBY("hash incr", "value 2", 1, last(name, require_number(1, name)));
};
tests.SUBSCRIBE = function () {
var client1 = client, msg_count = 0, name = "SUBSCRIBE";
client1.on("subscribe", function (channel, count) {
if (channel === "chan1") {
client2.publish("chan1", "message 1", require_number(1, name));
client2.publish("chan2", "message 2", require_number(1, name));
client2.publish("chan1", "message 3", require_number(1, name));
}
});
client1.on("unsubscribe", function (channel, count) {
if (count === 0) {
// make sure this connection can go into and out of pub/sub mode
client1.incr("did a thing", last(name, require_number(2, name)));
}
});
client1.on("message", function (channel, message) {
msg_count += 1;
assert.strictEqual("message " + msg_count, message.toString());
if (msg_count === 3) {
client1.unsubscribe("chan1", "chan2");
}
});
client1.set("did a thing", 1, require_string("OK", name));
client1.subscribe("chan1", "chan2", function (err, results) {
assert.strictEqual(null, err, "result sent back unexpected error: " + err);
assert.strictEqual("chan1", results.toString(), name);
});
};
tests.SUBSCRIBE_QUIT = function () {
var name = "SUBSCRIBE_QUIT";
client3.on("end", function () {
next(name);
});
client3.on("subscribe", function (channel, count) {
client3.quit();
});
client3.subscribe("chan3");
};
tests.EXISTS = function () {
var name = "EXISTS";
client.del("foo", "foo2", require_number_any(name));
client.set("foo", "bar", require_string("OK", name));
client.EXISTS("foo", require_number(1, name));
client.EXISTS("foo2", last(name, require_number(0, name)));
};
tests.DEL = function () {
var name = "DEL";
client.DEL("delkey", require_number_any(name));
client.set("delkey", "delvalue", require_string("OK", name));
client.DEL("delkey", require_number(1, name));
client.exists("delkey", require_number(0, name));
client.DEL("delkey", require_number(0, name));
client.mset("delkey", "delvalue", "delkey2", "delvalue2", require_string("OK", name));
client.DEL("delkey", "delkey2", last(name, require_number(2, name)));
};
tests.TYPE = function () {
var name = "TYPE";
client.set(["string key", "should be a string"], require_string("OK", name));
client.rpush(["list key", "should be a list"], require_number_pos(name));
client.sadd(["set key", "should be a set"], require_number_any(name));
client.zadd(["zset key", "10.0", "should be a zset"], require_number_any(name));
client.hset(["hash key", "hashtest", "should be a hash"], require_number_any(0, name));
client.TYPE(["string key"], require_string("string", name));
client.TYPE(["list key"], require_string("list", name));
client.TYPE(["set key"], require_string("set", name));
client.TYPE(["zset key"], require_string("zset", name));
client.TYPE("not here yet", require_string("none", name));
client.TYPE(["hash key"], last(name, require_string("hash", name)));
};
tests.KEYS = function () {
var name = "KEYS";
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], require_string("OK", name));
client.KEYS(["test keys*"], function (err, results) {
assert.strictEqual(null, err, "result sent back unexpected error: " + err);
assert.strictEqual(2, results.length, name);
results.sort();
assert.strictEqual("test keys 1", results[0].toString(), name);
assert.strictEqual("test keys 2", results[1].toString(), name);
next(name);
});
};
tests.MULTIBULK_ZERO_LENGTH = function () {
var name = "MULTIBULK_ZERO_LENGTH";
client.KEYS(['users:*'], function (err, results) {
assert.strictEqual(null, err, 'error on empty multibulk reply');
assert.strictEqual(true, is_empty_array(results), "not an empty array");
next(name);
});
};
tests.RANDOMKEY = function () {
var name = "RANDOMKEY";
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], require_string("OK", name));
client.RANDOMKEY([], function (err, results) {
assert.strictEqual(null, err, name + " result sent back unexpected error: " + err);
assert.strictEqual(true, /\w+/.test(results), name);
next(name);
});
};
tests.RENAME = function () {
var name = "RENAME";
client.set(['foo', 'bar'], require_string("OK", name));
client.RENAME(["foo", "new foo"], require_string("OK", name));
client.exists(["foo"], require_number(0, name));
client.exists(["new foo"], last(name, require_number(1, name)));
};
tests.RENAMENX = function () {
var name = "RENAMENX";
client.set(['foo', 'bar'], require_string("OK", name));
client.set(['foo2', 'bar2'], require_string("OK", name));
client.RENAMENX(["foo", "foo2"], require_number(0, name));
client.exists(["foo"], require_number(1, name));
client.exists(["foo2"], require_number(1, name));
client.del(["foo2"], require_number(1, name));
client.RENAMENX(["foo", "foo2"], require_number(1, name));
client.exists(["foo"], require_number(0, name));
client.exists(["foo2"], last(name, require_number(1, name)));
};
tests.DBSIZE = function () {
var name = "DBSIZE";
client.set(['foo', 'bar'], require_string("OK", name));
client.DBSIZE([], last(name, require_number_pos("DBSIZE")));
};
tests.GET = function () {
var name = "GET";
client.set(["get key", "get val"], require_string("OK", name));
client.GET(["get key"], last(name, require_string("get val", name)));
};
tests.SET = function () {
var name = "SET";
client.SET(["set key", "set val"], require_string("OK", name));
client.get(["set key"], last(name, require_string("set val", name)));
};
tests.GETSET = function () {
var name = "GETSET";
client.set(["getset key", "getset val"], require_string("OK", name));
client.GETSET(["getset key", "new getset val"], require_string("getset val", name));
client.get(["getset key"], last(name, require_string("new getset val", name)));
};
tests.MGET = function () {
var name = "MGET";
client.mset(["mget keys 1", "mget val 1", "mget keys 2", "mget val 2", "mget keys 3", "mget val 3"], require_string("OK", name));
client.MGET("mget keys 1", "mget keys 2", "mget keys 3", function (err, results) {
assert.strictEqual(null, err, "result sent back unexpected error: " + err);
assert.strictEqual(3, results.length, name);
assert.strictEqual("mget val 1", results[0].toString(), name);
assert.strictEqual("mget val 2", results[1].toString(), name);
assert.strictEqual("mget val 3", results[2].toString(), name);
});
client.MGET(["mget keys 1", "mget keys 2", "mget keys 3"], function (err, results) {
assert.strictEqual(null, err, "result sent back unexpected error: " + err);
assert.strictEqual(3, results.length, name);
assert.strictEqual("mget val 1", results[0].toString(), name);
assert.strictEqual("mget val 2", results[1].toString(), name);
assert.strictEqual("mget val 3", results[2].toString(), name);
});
client.MGET(["mget keys 1", "some random shit", "mget keys 2", "mget keys 3"], function (err, results) {
assert.strictEqual(null, err, "result sent back unexpected error: " + err);
assert.strictEqual(4, results.length, name);
assert.strictEqual("mget val 1", results[0].toString(), name);
assert.strictEqual(null, results[1], name);
assert.strictEqual("mget val 2", results[2].toString(), name);
assert.strictEqual("mget val 3", results[3].toString(), name);
next(name);
});
};
tests.SETNX = function () {
var name = "SETNX";
client.set(["setnx key", "setnx value"], require_string("OK", name));
client.SETNX(["setnx key", "new setnx value"], require_number(0, name));
client.del(["setnx key"], require_number(1, name));
client.exists(["setnx key"], require_number(0, name));
client.SETNX(["setnx key", "new setnx value"], require_number(1, name));
client.exists(["setnx key"], last(name, require_number(1, name)));
};
tests.SETEX = function () {
var name = "SETEX";
client.SETEX(["setex key", "100", "setex val"], require_string("OK", name));
client.exists(["setex key"], require_number(1, name));
client.ttl(["setex key"], last(name, require_number_pos(name)));
};
tests.MSETNX = function () {
var name = "MSETNX";
client.mset(["mset1", "val1", "mset2", "val2", "mset3", "val3"], require_string("OK", name));
client.MSETNX(["mset3", "val3", "mset4", "val4"], require_number(0, name));
client.del(["mset3"], require_number(1, name));
client.MSETNX(["mset3", "val3", "mset4", "val4"], require_number(1, name));
client.exists(["mset3"], require_number(1, name));
client.exists(["mset4"], last(name, require_number(1, name)));
};
tests.HGETALL = function () {
var name = "HGETALL";
client.hmset(["hosts", "mjr", "1", "another", "23", "home", "1234"], require_string("OK", name));
client.HGETALL(["hosts"], function (err, obj) {
assert.strictEqual(null, err, name + " result sent back unexpected error: " + err);
assert.strictEqual(3, Object.keys(obj).length, name);
assert.strictEqual("1", obj.mjr.toString(), name);
assert.strictEqual("23", obj.another.toString(), name);
assert.strictEqual("1234", obj.home.toString(), name);
next(name);
});
};
tests.HGETALL_NULL = function () {
var name = "HGETALL_NULL";
client.hgetall("missing", function (err, obj) {
assert.strictEqual(null, err);
assert.strictEqual(null, obj);
next(name);
});
};
tests.UTF8 = function () {
var name = "UTF8",
utf8_sample = "ಠ_ಠ";
client.set(["utf8test", utf8_sample], require_string("OK", name));
client.get(["utf8test"], function (err, obj) {
assert.strictEqual(null, err);
assert.strictEqual(utf8_sample, obj);
next(name);
});
};
// Set tests were adapted from Brian Hammond's redis-node-client.js, which has a comprehensive test suite
tests.SADD = function () {
var name = "SADD";
client.del('set0');
client.sadd('set0', 'member0', require_number(1, name));
client.sadd('set0', 'member0', last(name, require_number(0, name)));
};
tests.SADD2 = function () {
var name = "SADD2";
client.del("set0");
client.sadd("set0", ["member0", "member1", "member2"], require_number(3, name));
client.smembers("set0", function (err, res) {
assert.strictEqual(res.length, 3);
res.sort();
assert.strictEqual(res[0], "member0");
assert.strictEqual(res[1], "member1");
assert.strictEqual(res[2], "member2");
next(name);
});
};
tests.SISMEMBER = function () {
var name = "SISMEMBER";
client.del('set0');
client.sadd('set0', 'member0', require_number(1, name));
client.sismember('set0', 'member0', require_number(1, name));
client.sismember('set0', 'member1', last(name, require_number(0, name)));
};
tests.SCARD = function () {
var name = "SCARD";
client.del('set0');
client.sadd('set0', 'member0', require_number(1, name));
client.scard('set0', require_number(1, name));
client.sadd('set0', 'member1', require_number(1, name));
client.scard('set0', last(name, require_number(2, name)));
};
tests.SREM = function () {
var name = "SREM";
client.del('set0');
client.sadd('set0', 'member0', require_number(1, name));
client.srem('set0', 'foobar', require_number(0, name));
client.srem('set0', 'member0', require_number(1, name));
client.scard('set0', last(name, require_number(0, name)));
};
tests.SPOP = function () {
var name = "SPOP";
client.del('zzz');
client.sadd('zzz', 'member0', require_number(1, name));
client.scard('zzz', require_number(1, name));
client.spop('zzz', function (err, value) {
if (err) {
assert.fail(err);
}
assert.equal(value, 'member0', name);
});
client.scard('zzz', last(name, require_number(0, name)));
};
tests.SDIFF = function () {
var name = "SDIFF";
client.del('foo');
client.sadd('foo', 'x', require_number(1, name));
client.sadd('foo', 'a', require_number(1, name));
client.sadd('foo', 'b', require_number(1, name));
client.sadd('foo', 'c', require_number(1, name));
client.sadd('bar', 'c', require_number(1, name));
client.sadd('baz', 'a', require_number(1, name));
client.sadd('baz', 'd', require_number(1, name));
client.sdiff('foo', 'bar', 'baz', function (err, values) {
if (err) {
assert.fail(err, name);
}
values.sort();
assert.equal(values.length, 2, name);
assert.equal(values[0], 'b', name);
assert.equal(values[1], 'x', name);
next(name);
});
};
tests.SDIFFSTORE = function () {
var name = "SDIFFSTORE";
client.del('foo');
client.del('bar');
client.del('baz');
client.del('quux');
client.sadd('foo', 'x', require_number(1, name));
client.sadd('foo', 'a', require_number(1, name));
client.sadd('foo', 'b', require_number(1, name));
client.sadd('foo', 'c', require_number(1, name));
client.sadd('bar', 'c', require_number(1, name));
client.sadd('baz', 'a', require_number(1, name));
client.sadd('baz', 'd', require_number(1, name));
// NB: SDIFFSTORE returns the number of elements in the dstkey
client.sdiffstore('quux', 'foo', 'bar', 'baz', require_number(2, name));
client.smembers('quux', function (err, values) {
if (err) {
assert.fail(err, name);
}
var members = buffers_to_strings(values).sort();
assert.deepEqual(members, [ 'b', 'x' ], name);
next(name);
});
};
tests.SMEMBERS = function () {
var name = "SMEMBERS";
client.del('foo');
client.sadd('foo', 'x', require_number(1, name));
client.smembers('foo', function (err, members) {
if (err) {
assert.fail(err, name);
}
assert.deepEqual(buffers_to_strings(members), [ 'x' ], name);
});
client.sadd('foo', 'y', require_number(1, name));
client.smembers('foo', function (err, values) {
if (err) {
assert.fail(err, name);
}
assert.equal(values.length, 2, name);
var members = buffers_to_strings(values).sort();
assert.deepEqual(members, [ 'x', 'y' ], name);
next(name);
});
};
tests.SMOVE = function () {
var name = "SMOVE";
client.del('foo');
client.del('bar');
client.sadd('foo', 'x', require_number(1, name));
client.smove('foo', 'bar', 'x', require_number(1, name));
client.sismember('foo', 'x', require_number(0, name));
client.sismember('bar', 'x', require_number(1, name));
client.smove('foo', 'bar', 'x', last(name, require_number(0, name)));
};
tests.SINTER = function () {
var name = "SINTER";
client.del('sa');
client.del('sb');
client.del('sc');
client.sadd('sa', 'a', require_number(1, name));
client.sadd('sa', 'b', require_number(1, name));
client.sadd('sa', 'c', require_number(1, name));
client.sadd('sb', 'b', require_number(1, name));
client.sadd('sb', 'c', require_number(1, name));
client.sadd('sb', 'd', require_number(1, name));
client.sadd('sc', 'c', require_number(1, name));
client.sadd('sc', 'd', require_number(1, name));
client.sadd('sc', 'e', require_number(1, name));
client.sinter('sa', 'sb', function (err, intersection) {
if (err) {
assert.fail(err, name);
}
assert.equal(intersection.length, 2, name);
assert.deepEqual(buffers_to_strings(intersection).sort(), [ 'b', 'c' ], name);
});
client.sinter('sb', 'sc', function (err, intersection) {
if (err) {
assert.fail(err, name);