1 //
2 // RedisTest.cpp
3 //
4 // Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
5 // and Contributors.
6 //
7 // SPDX-License-Identifier: BSL-1.0
8 //
9
10
11 #include "Poco/Exception.h"
12 #include "Poco/Delegate.h"
13 #include "Poco/Thread.h"
14 #include "RedisTest.h"
15 #include "Poco/Redis/AsyncReader.h"
16 #include "Poco/Redis/Command.h"
17 #include "Poco/Redis/PoolableConnectionFactory.h"
18 #include "CppUnit/TestCaller.h"
19 #include "CppUnit/TestSuite.h"
20 #include <iostream>
21
22
23 using namespace Poco::Redis;
24
25
26 bool RedisTest::_connected = false;
27 Poco::Redis::Client RedisTest::_redis;
28
29
RedisTest(const std::string & name)30 RedisTest::RedisTest(const std::string& name):
31 CppUnit::TestCase("Redis"),
32 _host("localhost"),
33 _port(6379)
34 {
35 #if POCO_OS == POCO_OS_ANDROID
36 _host = "10.0.2.2";
37 #endif
38 if (!_connected)
39 {
40 try
41 {
42 Poco::Timespan t(10, 0); // Connect within 10 seconds
43 _redis.connect(_host, _port, t);
44 _connected = true;
45 std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl;
46 }
47 catch (Poco::Exception& e)
48 {
49 std::cout << "Couldn't connect to [" << _host << ':' << _port << ']' << e.message() << ". " << std::endl;
50 }
51 }
52 }
53
54
~RedisTest()55 RedisTest::~RedisTest()
56 {
57 if (_connected)
58 {
59 _redis.disconnect();
60 _connected = false;
61 std::cout << "Disconnected from [" << _host << ':' << _port << ']' << std::endl;
62 }
63 }
64
65
setUp()66 void RedisTest::setUp()
67 {
68 }
69
70
tearDown()71 void RedisTest::tearDown()
72 {
73 }
74
75
testAPPEND()76 void RedisTest::testAPPEND()
77 {
78 if (!_connected)
79 {
80 std::cout << "Not connected, test skipped." << std::endl;
81 return;
82 }
83
84 delKey("mykey");
85
86 Command setCommand = Command::set("mykey", "Hello");
87 try
88 {
89 std::string result = _redis.execute<std::string>(setCommand);
90 assertTrue (result.compare("OK") == 0);
91 }
92 catch (RedisException& e)
93 {
94 fail(e.message());
95 }
96 catch (Poco::BadCastException& e)
97 {
98 fail(e.message());
99 }
100
101 Command appendCommand = Command::append("mykey", " World");
102 try
103 {
104 Poco::Int64 result = _redis.execute<Poco::Int64>(appendCommand);
105 assertTrue (result == 11);
106 }
107 catch (RedisException& e)
108 {
109 fail(e.message());
110 }
111 catch (Poco::BadCastException& e)
112 {
113 fail(e.message());
114 }
115
116 Command getCommand = Command::get("mykey");
117 try
118 {
119 BulkString result = _redis.execute<BulkString>(getCommand);
120 assertTrue (result.value().compare("Hello World") == 0);
121 }
122 catch (RedisException& e)
123 {
124 fail(e.message());
125 }
126 catch (Poco::BadCastException& e)
127 {
128 fail(e.message());
129 }
130 }
131
132
testBLPOP()133 void RedisTest::testBLPOP()
134 {
135 if (!_connected)
136 {
137 std::cout << "Not connected, test skipped." << std::endl;
138 return;
139 }
140
141 // Make sure the lists are not there yet ...
142 std::vector<std::string> lists;
143 lists.push_back("list1");
144 lists.push_back("list2");
145 Command delCommand = Command::del(lists);
146 try
147 {
148 _redis.execute<Poco::Int64>(delCommand);
149 }
150 catch (RedisException& e)
151 {
152 fail(e.message());
153 }
154 catch (Poco::BadCastException& e)
155 {
156 fail(e.message());
157 }
158
159 std::vector<std::string> values;
160 values.push_back("a");
161 values.push_back("b");
162 values.push_back("c");
163
164 try
165 {
166 Command rpush = Command::rpush("list1", values);
167 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
168 assertTrue (result == 3);
169 }
170 catch (RedisException& e)
171 {
172 fail(e.message());
173 }
174
175 Command blpop = Command::blpop(lists);
176 try
177 {
178 Array result = _redis.execute<Array>(blpop);
179 assertTrue (result.size() == 2);
180 assertTrue (result.get<BulkString>(0).value().compare("list1") == 0);
181 assertTrue (result.get<BulkString>(1).value().compare("a") == 0);
182 }
183 catch (RedisException& e)
184 {
185 fail(e.message());
186 }
187 }
188
189
testBRPOP()190 void RedisTest::testBRPOP()
191 {
192 if (!_connected)
193 {
194 std::cout << "Not connected, test skipped." << std::endl;
195 return;
196 }
197
198 // Make sure the lists are not there yet ...
199 std::vector<std::string> lists;
200 lists.push_back("list1");
201 lists.push_back("list2");
202 Command delCommand = Command::del(lists);
203 try
204 {
205 _redis.execute<Poco::Int64>(delCommand);
206 }
207 catch (RedisException& e)
208 {
209 fail(e.message());
210 }
211 catch (Poco::BadCastException& e)
212 {
213 fail(e.message());
214 }
215
216 std::vector<std::string> values;
217 values.push_back("a");
218 values.push_back("b");
219 values.push_back("c");
220
221 try
222 {
223 Command rpush = Command::rpush("list1", values);
224 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
225 assertTrue (result == 3);
226 }
227 catch (RedisException& e)
228 {
229 fail(e.message());
230 }
231
232 Command brpop = Command::brpop(lists);
233 try
234 {
235 Array result = _redis.execute<Array>(brpop);
236 assertTrue (result.size() == 2);
237 assertTrue (result.get<BulkString>(0).value().compare("list1") == 0);
238 assertTrue (result.get<BulkString>(1).value().compare("c") == 0);
239 }
240 catch (RedisException& e)
241 {
242 fail(e.message());
243 }
244 }
245
246
testDECR()247 void RedisTest::testDECR()
248 {
249 if (!_connected)
250 {
251 std::cout << "Not connected, test skipped." << std::endl;
252 return;
253 }
254
255 Command set = Command::set("mykey", 10);
256 try
257 {
258 std::string result = _redis.execute<std::string>(set);
259 assertTrue (result.compare("OK") == 0);
260 }
261 catch (RedisException& e)
262 {
263 fail(e.message());
264 }
265
266 Command decr = Command::decr("mykey");
267 try
268 {
269 Poco::Int64 result = _redis.execute<Poco::Int64>(decr);
270 assertTrue (result == 9);
271 }
272 catch (RedisException& e)
273 {
274 fail(e.message());
275 }
276
277 set = Command::set("mykey", "234293482390480948029348230948");
278 try
279 {
280 std::string result = _redis.execute<std::string>(set);
281 assertTrue (result.compare("OK") == 0);
282 }
283 catch (RedisException& e)
284 {
285 fail(e.message());
286 }
287
288 try
289 {
290 Poco::Int64 result = _redis.execute<Poco::Int64>(decr);
291 fail("This must fail");
292 }
293 catch (RedisException& e)
294 {
295 // ERR value is not an integer or out of range
296 }
297
298 }
299
300
testECHO()301 void RedisTest::testECHO()
302 {
303 if (!_connected)
304 {
305 std::cout << "Not connected, test skipped." << std::endl;
306 return;
307 }
308
309 Array command;
310 command.add("ECHO")
311 .add("Hello World");
312
313 try
314 {
315 BulkString result = _redis.execute<BulkString>(command);
316 assertTrue (!result.isNull());
317 assertTrue (result.value().compare("Hello World") == 0);
318 }
319 catch (RedisException& e)
320 {
321 fail(e.message());
322 }
323 }
324
325
testError()326 void RedisTest::testError()
327 {
328 if (!_connected)
329 {
330 std::cout << "Not connected, test skipped." << std::endl;
331 return;
332 }
333
334 Array command;
335 command.add("Wrong Command");
336
337 try
338 {
339 BulkString result = _redis.execute<BulkString>(command);
340 fail("Invalid command must throw RedisException");
341 }
342 catch (RedisException& e)
343 {
344 // Must fail
345 }
346 }
347
348
testEVAL()349 void RedisTest::testEVAL()
350 {
351 if (!_connected)
352 {
353 std::cout << "Not connected, test skipped." << std::endl;
354 return;
355 }
356
357 Command cmd("EVAL");
358 cmd << "return {1, 2, {3, 'Hello World!'}}" << Poco::NumberFormatter::format(0);
359
360 try
361 {
362 Array value = _redis.execute<Array>(cmd);
363 assertTrue (value.size() == 3);
364
365 Poco::Int64 i = value.get<Poco::Int64>(0);
366 assertTrue (i == 1);
367 i = value.get<Poco::Int64>(1);
368 assertTrue (i == 2);
369
370 Array a = value.get<Array>(2);
371 assertTrue (a.size() == 2);
372 i = a.get<Poco::Int64>(0);
373 assertTrue (i == 3);
374 BulkString s = a.get<BulkString>(1);
375 assertTrue (s.value().compare("Hello World!") == 0);
376 }
377 catch (RedisException& e)
378 {
379 fail(e.message());
380 }
381
382 }
383
384
testHDEL()385 void RedisTest::testHDEL()
386 {
387 if (!_connected)
388 {
389 std::cout << "Not connected, test skipped." << std::endl;
390 return;
391 }
392
393 delKey("myhash");
394
395 Command hset = Command::hset("myhash", "field1", "foo");
396 try
397 {
398 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
399 assertTrue (value == 1);
400 }
401 catch (RedisException& e)
402 {
403 fail(e.message());
404 }
405
406 Command hdel = Command::hdel("myhash", "field1");
407 try
408 {
409 Poco::Int64 result = _redis.execute<Poco::Int64>(hdel);
410 assertTrue (result == 1);
411 }
412 catch (RedisException& e)
413 {
414 fail(e.message());
415 }
416
417 hdel = Command::hdel("myhash", "field2");
418 try
419 {
420 Poco::Int64 result = _redis.execute<Poco::Int64>(hdel);
421 assertTrue (result == 0);
422 }
423 catch (RedisException& e)
424 {
425 fail(e.message());
426 }
427 }
428
429
testHEXISTS()430 void RedisTest::testHEXISTS()
431 {
432 if (!_connected)
433 {
434 std::cout << "Not connected, test skipped." << std::endl;
435 return;
436 }
437
438 delKey("myhash");
439
440 Command hset = Command::hset("myhash", "field1", "foo");
441 try
442 {
443 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
444 assertTrue (value == 1);
445 }
446 catch (RedisException& e)
447 {
448 fail(e.message());
449 }
450
451 Command hexists = Command::hexists("myhash", "field1");
452 try
453 {
454 Poco::Int64 result = _redis.execute<Poco::Int64>(hexists);
455 assertTrue (result == 1);
456 }
457 catch (RedisException& e)
458 {
459 fail(e.message());
460 }
461
462 hexists = Command::hexists("myhash", "field2");
463 try
464 {
465 Poco::Int64 result = _redis.execute<Poco::Int64>(hexists);
466 assertTrue (result == 0);
467 }
468 catch (RedisException& e)
469 {
470 fail(e.message());
471 }
472 }
473
474
testHGETALL()475 void RedisTest::testHGETALL()
476 {
477 if (!_connected)
478 {
479 std::cout << "Not connected, test skipped." << std::endl;
480 return;
481 }
482
483 delKey("myhash");
484
485 Command hset = Command::hset("myhash", "field1", "Hello");
486 try
487 {
488 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
489 assertTrue (value == 1);
490 }
491 catch (RedisException& e)
492 {
493 fail(e.message());
494 }
495
496 hset = Command::hset("myhash", "field2", "World");
497 try
498 {
499 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
500 assertTrue (value == 1);
501 }
502 catch (RedisException& e)
503 {
504 fail(e.message());
505 }
506
507 Command hgetall = Command::hgetall("myhash");
508 try
509 {
510 Array result = _redis.execute<Array>(hgetall);
511 assertTrue (result.size() == 4);
512 }
513 catch (RedisException& e)
514 {
515 fail(e.message());
516 }
517 }
518
519
testHINCRBY()520 void RedisTest::testHINCRBY()
521 {
522 if (!_connected)
523 {
524 std::cout << "Not connected, test skipped." << std::endl;
525 return;
526 }
527
528 delKey("myhash");
529
530 Command hset = Command::hset("myhash", "field", 5);
531 try
532 {
533 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
534 assertTrue (value == 1);
535 }
536 catch (RedisException& e)
537 {
538 fail(e.message());
539 }
540
541 Command hincrby = Command::hincrby("myhash", "field");
542 try
543 {
544 Poco::Int64 n = _redis.execute<Poco::Int64>(hincrby);
545 assertTrue (n == 6);
546 }
547 catch (RedisException& e)
548 {
549 fail(e.message());
550 }
551
552 hincrby = Command::hincrby("myhash", "field", -1);
553 try
554 {
555 Poco::Int64 n = _redis.execute<Poco::Int64>(hincrby);
556 assertTrue (n == 5);
557 }
558 catch (RedisException& e)
559 {
560 fail(e.message());
561 }
562
563 hincrby = Command::hincrby("myhash", "field", -10);
564 try
565 {
566 Poco::Int64 n = _redis.execute<Poco::Int64>(hincrby);
567 assertTrue (n == -5);
568 }
569 catch (RedisException& e)
570 {
571 fail(e.message());
572 }
573 }
574
575
testHKEYS()576 void RedisTest::testHKEYS()
577 {
578 if (!_connected)
579 {
580 std::cout << "Not connected, test skipped." << std::endl;
581 return;
582 }
583
584 delKey("myhash");
585
586 Command hset = Command::hset("myhash", "field1", "Hello");
587 try
588 {
589 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
590 assertTrue (value == 1);
591 }
592 catch (RedisException& e)
593 {
594 fail(e.message());
595 }
596
597 hset = Command::hset("myhash", "field2", "World");
598 try
599 {
600 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
601 assertTrue (value == 1);
602 }
603 catch (RedisException& e)
604 {
605 fail(e.message());
606 }
607
608 Command hlen = Command::hlen("myhash");
609 try
610 {
611 Poco::Int64 value = _redis.execute<Poco::Int64>(hlen);
612 assertTrue (value == 2);
613 }
614 catch (RedisException& e)
615 {
616 fail(e.message());
617 }
618
619 Command hkeys = Command::hkeys("myhash");
620 try
621 {
622 Array result = _redis.execute<Array>(hkeys);
623 assertTrue (result.size() == 2);
624 assertTrue (result.get<BulkString>(0).value().compare("field1") == 0);
625 assertTrue (result.get<BulkString>(1).value().compare("field2") == 0);
626 }
627 catch (RedisException& e)
628 {
629 fail(e.message());
630 }
631 }
632
633
testHMGET()634 void RedisTest::testHMGET()
635 {
636 if (!_connected)
637 {
638 std::cout << "Not connected, test skipped." << std::endl;
639 return;
640 }
641
642 delKey("myhash");
643
644 Command hset = Command::hset("myhash", "field1", "Hello");
645 try
646 {
647 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
648 assertTrue (value == 1);
649 }
650 catch (RedisException& e)
651 {
652 fail(e.message());
653 }
654
655 hset = Command::hset("myhash", "field2", "World");
656 try
657 {
658 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
659 assertTrue (value == 1);
660 }
661 catch (RedisException& e)
662 {
663 fail(e.message());
664 }
665
666 std::vector<std::string> fields;
667 fields.push_back("field1");
668 fields.push_back("field2");
669 fields.push_back("field3");
670 Command hmget = Command::hmget("myhash", fields);
671 try
672 {
673 Array result = _redis.execute<Array>(hmget);
674 assertTrue (result.size() == 3);
675
676 assertTrue (result.get<BulkString>(0).value().compare("Hello") == 0);
677 assertTrue (result.get<BulkString>(1).value().compare("World") == 0);
678 assertTrue (result.get<BulkString>(2).isNull());
679 }
680 catch (RedisException& e)
681 {
682 fail(e.message());
683 }
684 }
685
686
testHSET()687 void RedisTest::testHSET()
688 {
689 if (!_connected)
690 {
691 std::cout << "Not connected, test skipped." << std::endl;
692 return;
693 }
694
695 delKey("myhash");
696
697 Command hset = Command::hset("myhash", "field1", "Hello");
698 try
699 {
700 Poco::Int64 value = _redis.execute<Poco::Int64>(hset);
701 assertTrue (value == 1);
702 }
703 catch (RedisException& e)
704 {
705 fail(e.message());
706 }
707
708 Command hget = Command::hget("myhash", "field1");
709 try
710 {
711 BulkString s = _redis.execute<BulkString>(hget);
712 assertTrue (s.value().compare("Hello") == 0);
713 }
714 catch (RedisException& e)
715 {
716 fail(e.message());
717 }
718 }
719
720
testHMSET()721 void RedisTest::testHMSET()
722 {
723 if (!_connected)
724 {
725 std::cout << "Not connected, test skipped." << std::endl;
726 return;
727 }
728
729 delKey("myhash");
730
731 std::map<std::string, std::string> fields;
732 fields.insert(std::make_pair<std::string, std::string>("field1", "Hello"));
733 fields.insert(std::make_pair<std::string, std::string>("field2", "World"));
734
735 Command hmset = Command::hmset("myhash", fields);
736 try
737 {
738 std::string result = _redis.execute<std::string>(hmset);
739 assertTrue (result.compare("OK") == 0);
740 }
741 catch (RedisException& e)
742 {
743 fail(e.message());
744 }
745
746 Command hget = Command::hget("myhash", "field1");
747 try
748 {
749 BulkString s = _redis.execute<BulkString>(hget);
750 assertTrue (s.value().compare("Hello") == 0);
751 }
752 catch (RedisException& e)
753 {
754 fail(e.message());
755 }
756
757 hget = Command::hget("myhash", "field2");
758 try
759 {
760 BulkString s = _redis.execute<BulkString>(hget);
761 assertTrue (s.value().compare("World") == 0);
762 }
763 catch (RedisException& e)
764 {
765 fail(e.message());
766 }
767
768 }
769
770
testHSTRLEN()771 void RedisTest::testHSTRLEN()
772 {
773 if (!_connected)
774 {
775 std::cout << "Not connected, test skipped." << std::endl;
776 return;
777 }
778
779 delKey("myhash");
780
781 std::map<std::string, std::string> fields;
782 fields.insert(std::make_pair<std::string, std::string>("f1", "HelloWorld"));
783 fields.insert(std::make_pair<std::string, std::string>("f2", "99"));
784 fields.insert(std::make_pair<std::string, std::string>("f3", "-256"));
785
786 Command hmset = Command::hmset("myhash", fields);
787 try
788 {
789 std::string result = _redis.execute<std::string>(hmset);
790 assertTrue (result.compare("OK") == 0);
791 }
792 catch (RedisException& e)
793 {
794 fail(e.message());
795 }
796
797 Command hstrlen = Command::hstrlen("myhash", "f1");
798 try
799 {
800 Poco::Int64 len = _redis.execute<Poco::Int64>(hstrlen);
801 assertTrue (len == 10);
802 }
803 catch (RedisException& e)
804 {
805 fail(e.message());
806 }
807
808 hstrlen = Command::hstrlen("myhash", "f2");
809 try
810 {
811 Poco::Int64 len = _redis.execute<Poco::Int64>(hstrlen);
812 assertTrue (len == 2);
813 }
814 catch (RedisException& e)
815 {
816 fail(e.message());
817 }
818
819 hstrlen = Command::hstrlen("myhash", "f3");
820 try
821 {
822 Poco::Int64 len = _redis.execute<Poco::Int64>(hstrlen);
823 assertTrue (len == 4);
824 }
825 catch (RedisException& e)
826 {
827 fail(e.message());
828 }
829 }
830
831
testHVALS()832 void RedisTest::testHVALS()
833 {
834 if (!_connected)
835 {
836 std::cout << "Not connected, test skipped." << std::endl;
837 return;
838 }
839
840 delKey("myhash");
841
842 std::map<std::string, std::string> fields;
843 fields.insert(std::make_pair<std::string, std::string>("field1", "Hello"));
844 fields.insert(std::make_pair<std::string, std::string>("field2", "World"));
845
846 Command hmset = Command::hmset("myhash", fields);
847 try
848 {
849 std::string result = _redis.execute<std::string>(hmset);
850 assertTrue (result.compare("OK") == 0);
851 }
852 catch (RedisException& e)
853 {
854 fail(e.message());
855 }
856
857 Command hvals = Command::hvals("myhash");
858 try
859 {
860 Array result = _redis.execute<Array>(hvals);
861 assertTrue (result.size() == 2);
862 assertTrue (result.get<BulkString>(0).value().compare("Hello") == 0);
863 assertTrue (result.get<BulkString>(1).value().compare("World") == 0);
864 }
865 catch (RedisException& e)
866 {
867 fail(e.message());
868 }
869 }
870
871
testINCR()872 void RedisTest::testINCR()
873 {
874 if (!_connected)
875 {
876 std::cout << "Not connected, test skipped." << std::endl;
877 return;
878 }
879
880 Command command = Command::set("mykey", "10");
881 // A set responds with a simple OK string
882 try
883 {
884 std::string result = _redis.execute<std::string>(command);
885 assertTrue (result.compare("OK") == 0);
886 }
887 catch (RedisException& e)
888 {
889 fail(e.message());
890 }
891
892 command = Command::incr("mykey");
893 try
894 {
895 Poco::Int64 value = _redis.execute<Poco::Int64>(command);
896 assertTrue (value == 11);
897 }
898 catch (RedisException& e)
899 {
900 fail(e.message());
901 }
902 }
903
904
testINCRBY()905 void RedisTest::testINCRBY()
906 {
907 if (!_connected)
908 {
909 std::cout << "Not connected, test skipped." << std::endl;
910 return;
911 }
912
913 Command command = Command::set("mykey", "10");
914 // A set responds with a simple OK string
915 try
916 {
917 std::string result = _redis.execute<std::string>(command);
918 assertTrue (result.compare("OK") == 0);
919 }
920 catch (RedisException& e)
921 {
922 fail(e.message());
923 }
924
925 command = Command::incr("mykey", 5);
926 try
927 {
928 Poco::Int64 value = _redis.execute<Poco::Int64>(command);
929 assertTrue (value == 15);
930 }
931 catch (RedisException& e)
932 {
933 fail(e.message());
934 }
935 }
936
937
testPING()938 void RedisTest::testPING()
939 {
940 if (!_connected)
941 {
942 std::cout << "Not connected, test skipped." << std::endl;
943 return;
944 }
945
946 Array command;
947 command.add("PING");
948
949 // A PING without a custom strings, responds with a simple "PONG" string
950 try
951 {
952 std::string result = _redis.execute<std::string>(command);
953 assertTrue (result.compare("PONG") == 0);
954 }
955 catch (RedisException& e)
956 {
957 fail(e.message());
958 }
959
960 #ifndef OLD_REDIS_VERSION
961 // A PING with a custom string responds with a bulk string
962 command.add("Hello");
963 try
964 {
965 BulkString result = _redis.execute<BulkString>(command);
966 assertTrue (!result.isNull());
967 assertTrue (result.value().compare("Hello") == 0);
968 }
969 catch (RedisException& e)
970 {
971 fail(e.message());
972 }
973 #endif
974 }
975
976
testLPOP()977 void RedisTest::testLPOP()
978 {
979 if (!_connected)
980 {
981 std::cout << "Not connected, test skipped." << std::endl;
982 return;
983 }
984
985 // Make sure the list is not there yet ...
986 delKey("mylist");
987
988 try
989 {
990 Command rpush = Command::rpush("mylist", "one");
991 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
992 assertTrue (result == 1);
993
994 rpush = Command::rpush("mylist", "two");
995 result = _redis.execute<Poco::Int64>(rpush);
996 assertTrue (result == 2);
997
998 rpush = Command::rpush("mylist", "three");
999 result = _redis.execute<Poco::Int64>(rpush);
1000 assertTrue (result == 3);
1001 }
1002 catch (RedisException& e)
1003 {
1004 fail(e.message());
1005 }
1006
1007 Command lpop = Command::lpop("mylist");
1008 try
1009 {
1010 BulkString result = _redis.execute<BulkString>(lpop);
1011 assertTrue (result.value().compare("one") == 0);
1012 }
1013 catch (RedisException& e)
1014 {
1015 fail(e.message());
1016 }
1017
1018 Command lrange = Command::lrange("mylist");
1019 try
1020 {
1021 Array result = _redis.execute<Array>(lrange);
1022
1023 assertTrue (result.size() == 2);
1024 assertTrue (result.get<BulkString>(0).value().compare("two") == 0);
1025 assertTrue (result.get<BulkString>(1).value().compare("three") == 0);
1026 }
1027 catch (RedisException& e)
1028 {
1029 fail(e.message());
1030 }
1031 catch (Poco::NullValueException& e)
1032 {
1033 fail(e.message());
1034 }
1035
1036 }
1037
1038
testLSET()1039 void RedisTest::testLSET()
1040 {
1041 if (!_connected)
1042 {
1043 std::cout << "Not connected, test skipped." << std::endl;
1044 return;
1045 }
1046
1047 // Make sure the list is not there yet ...
1048 delKey("mylist");
1049
1050 try
1051 {
1052 Command rpush = Command::rpush("mylist", "one");
1053 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
1054 assertTrue (result == 1);
1055
1056 rpush = Command::rpush("mylist", "two");
1057 result = _redis.execute<Poco::Int64>(rpush);
1058 assertTrue (result == 2);
1059
1060 rpush = Command::rpush("mylist", "three");
1061 result = _redis.execute<Poco::Int64>(rpush);
1062 assertTrue (result == 3);
1063 }
1064 catch (RedisException& e)
1065 {
1066 fail(e.message());
1067 }
1068
1069 Command lset = Command::lset("mylist", 0, "four");
1070 try
1071 {
1072 std::string result = _redis.execute<std::string>(lset);
1073 }
1074 catch (RedisException& e)
1075 {
1076 fail(e.message());
1077 }
1078
1079 lset = Command::lset("mylist", -2, "five");
1080 try
1081 {
1082 std::string result = _redis.execute<std::string>(lset);
1083 }
1084 catch (RedisException& e)
1085 {
1086 fail(e.message());
1087 }
1088
1089 Command lrange = Command::lrange("mylist");
1090 try
1091 {
1092 Array result = _redis.execute<Array>(lrange);
1093
1094 assertTrue (result.size() == 3);
1095 assertTrue (result.get<BulkString>(0).value().compare("four") == 0);
1096 assertTrue (result.get<BulkString>(1).value().compare("five") == 0);
1097 assertTrue (result.get<BulkString>(2).value().compare("three") == 0);
1098 }
1099 catch (RedisException& e)
1100 {
1101 fail(e.message());
1102 }
1103 catch (Poco::NullValueException& e)
1104 {
1105 fail(e.message());
1106 }
1107
1108 }
1109
1110
testLINDEX()1111 void RedisTest::testLINDEX()
1112 {
1113 if (!_connected)
1114 {
1115 std::cout << "Not connected, test skipped." << std::endl;
1116 return;
1117 }
1118
1119 // Make sure the list is not there yet ...
1120 delKey("mylist");
1121
1122 try
1123 {
1124 Command lpush = Command::lpush("mylist", "World");
1125 Poco::Int64 result = _redis.execute<Poco::Int64>(lpush);
1126 assertTrue (result == 1);
1127
1128 lpush = Command::lpush("mylist", "Hello");
1129 result = _redis.execute<Poco::Int64>(lpush);
1130 assertTrue (result == 2);
1131 }
1132 catch (RedisException& e)
1133 {
1134 fail(e.message());
1135 }
1136
1137 Command lindex = Command::lindex("mylist", 0);
1138 try
1139 {
1140 BulkString result = _redis.execute<BulkString>(lindex);
1141 assertTrue (result.value().compare("Hello") == 0);
1142 }
1143 catch (RedisException& e)
1144 {
1145 fail(e.message());
1146 }
1147 }
1148
1149
testLINSERT()1150 void RedisTest::testLINSERT()
1151 {
1152 if (!_connected)
1153 {
1154 std::cout << "Not connected, test skipped." << std::endl;
1155 return;
1156 }
1157
1158 // Make sure the list is not there yet ...
1159 delKey("mylist");
1160
1161 try
1162 {
1163 Command rpush = Command::rpush("mylist", "Hello");
1164 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
1165 assertTrue (result == 1);
1166
1167 rpush = Command::rpush("mylist", "World");
1168 result = _redis.execute<Poco::Int64>(rpush);
1169 assertTrue (result == 2);
1170
1171 Command linsert = Command::linsert("mylist", true, "World", "There");
1172 result = _redis.execute<Poco::Int64>(linsert);
1173 assertTrue (result == 3);
1174
1175 Command lrange = Command::lrange("mylist", 0, -1);
1176 Array range = _redis.execute<Array>(lrange);
1177 assertTrue (range.size() == 3);
1178
1179 assertTrue (range.get<BulkString>(0).value().compare("Hello") == 0);
1180 assertTrue (range.get<BulkString>(1).value().compare("There") == 0);
1181 assertTrue (range.get<BulkString>(2).value().compare("World") == 0);
1182 }
1183 catch (RedisException& e)
1184 {
1185 fail(e.message());
1186 }
1187 catch (Poco::BadCastException& e)
1188 {
1189 fail(e.message());
1190 }
1191 catch (Poco::NullValueException& e)
1192 {
1193 fail(e.message());
1194 }
1195 }
1196
1197
testLREM()1198 void RedisTest::testLREM()
1199 {
1200 if (!_connected)
1201 {
1202 std::cout << "Not connected, test skipped." << std::endl;
1203 return;
1204 }
1205
1206 // Make sure the list is not there yet ...
1207 delKey("mylist");
1208
1209 try
1210 {
1211 std::vector<std::string> list;
1212 list.push_back("hello");
1213 list.push_back("hello");
1214 list.push_back("foo");
1215 list.push_back("hello");
1216 Command rpush = Command::rpush("mylist", list);
1217 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
1218 assertTrue (result == 4);
1219 }
1220 catch (RedisException& e)
1221 {
1222 fail(e.message());
1223 }
1224
1225 Command lrem = Command::lrem("mylist", -2, "hello");
1226 try
1227 {
1228 Poco::Int64 n = _redis.execute<Poco::Int64>(lrem);
1229 assertTrue (n == 2);
1230 }
1231 catch (RedisException& e)
1232 {
1233 fail(e.message());
1234 }
1235 catch (Poco::BadCastException& e)
1236 {
1237 fail(e.message());
1238 }
1239
1240 Command lrange = Command::lrange("mylist");
1241 try
1242 {
1243 Array result = _redis.execute<Array>(lrange);
1244
1245 assertTrue (result.size() == 2);
1246 assertTrue (result.get<BulkString>(0).value().compare("hello") == 0);
1247 assertTrue (result.get<BulkString>(1).value().compare("foo") == 0);
1248 }
1249 catch (RedisException& e)
1250 {
1251 fail(e.message());
1252 }
1253 catch (Poco::NullValueException& e)
1254 {
1255 fail(e.message());
1256 }
1257 }
1258
1259
testLTRIM()1260 void RedisTest::testLTRIM()
1261 {
1262 if (!_connected)
1263 {
1264 std::cout << "Not connected, test skipped." << std::endl;
1265 return;
1266 }
1267
1268 // Make sure the list is not there yet ...
1269 delKey("mylist");
1270
1271 try
1272 {
1273 Command rpush = Command::rpush("mylist", "one");
1274 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
1275 assertTrue (result == 1);
1276
1277 rpush = Command::rpush("mylist", "two");
1278 result = _redis.execute<Poco::Int64>(rpush);
1279 assertTrue (result == 2);
1280
1281 rpush = Command::rpush("mylist", "three");
1282 result = _redis.execute<Poco::Int64>(rpush);
1283 assertTrue (result == 3);
1284 }
1285 catch (RedisException& e)
1286 {
1287 fail(e.message());
1288 }
1289
1290 Command ltrim = Command::ltrim("mylist", 1);
1291 try
1292 {
1293 std::string result = _redis.execute<std::string>(ltrim);
1294 assertTrue (result.compare("OK") == 0);
1295 }
1296 catch (RedisException& e)
1297 {
1298 fail(e.message());
1299 }
1300
1301 Command lrange = Command::lrange("mylist");
1302 try
1303 {
1304 Array result = _redis.execute<Array>(lrange);
1305
1306 assertTrue (result.size() == 2);
1307 assertTrue (result.get<BulkString>(0).value().compare("two") == 0);
1308 assertTrue (result.get<BulkString>(1).value().compare("three") == 0);
1309 }
1310 catch (RedisException& e)
1311 {
1312 fail(e.message());
1313 }
1314 catch (Poco::NullValueException& e)
1315 {
1316 fail(e.message());
1317 }
1318
1319 }
1320
1321
testMSET()1322 void RedisTest::testMSET()
1323 {
1324 if (!_connected)
1325 {
1326 std::cout << "Not connected, test skipped." << std::endl;
1327 return;
1328 }
1329
1330 Command command("MSET");
1331 command << "key1" << "Hello" << "key2" << "World";
1332
1333 // A MSET responds with a simple OK string
1334 try
1335 {
1336 std::string result = _redis.execute<std::string>(command);
1337 assertTrue (result.compare("OK") == 0);
1338 }
1339 catch (RedisException& e)
1340 {
1341 fail(e.message());
1342 }
1343
1344 command.clear();
1345 command.add("MGET")
1346 .add("key1")
1347 .add("key2")
1348 .add("nonexisting");
1349 try
1350 {
1351 Array result = _redis.execute<Array>(command);
1352
1353 assertTrue (result.size() == 3);
1354 BulkString value = result.get<BulkString>(0);
1355 assertTrue (value.value().compare("Hello") == 0);
1356
1357 value = result.get<BulkString>(1);
1358 assertTrue (value.value().compare("World") == 0);
1359
1360 value = result.get<BulkString>(2);
1361 assertTrue (value.isNull());
1362 }
1363 catch (RedisException& e)
1364 {
1365 fail(e.message());
1366 }
1367 catch (Poco::BadCastException& e)
1368 {
1369 fail(e.message());
1370 }
1371 }
1372
1373
testMSETWithMap()1374 void RedisTest::testMSETWithMap()
1375 {
1376 if (!_connected)
1377 {
1378 std::cout << "Not connected, test skipped." << std::endl;
1379 return;
1380 }
1381
1382 std::map<std::string, std::string> keyValuePairs;
1383 keyValuePairs.insert(std::make_pair<std::string, std::string>("key1", "Hello"));
1384 keyValuePairs.insert(std::make_pair<std::string, std::string>("key2", "World"));
1385
1386 Command mset = Command::mset(keyValuePairs);
1387
1388 // A MSET responds with a simple OK string
1389 try
1390 {
1391 std::string result = _redis.execute<std::string>(mset);
1392 assertTrue (result.compare("OK") == 0);
1393 }
1394 catch (RedisException& e)
1395 {
1396 fail(e.message());
1397 }
1398
1399 std::vector<std::string> keys;
1400 keys.push_back("key1");
1401 keys.push_back("key2");
1402 keys.push_back("nonexisting");
1403
1404 Command mget = Command::mget(keys);
1405 try
1406 {
1407 Array result = _redis.execute<Array>(mget);
1408
1409 assertTrue (result.size() == 3);
1410 BulkString value = result.get<BulkString>(0);
1411 assertTrue (value.value().compare("Hello") == 0);
1412
1413 value = result.get<BulkString>(1);
1414 assertTrue (value.value().compare("World") == 0);
1415
1416 value = result.get<BulkString>(2);
1417 assertTrue (value.isNull());
1418 }
1419 catch (RedisException& e)
1420 {
1421 fail(e.message());
1422 }
1423 catch (Poco::BadCastException& e)
1424 {
1425 fail(e.message());
1426 }
1427 }
1428
1429
testMULTI()1430 void RedisTest::testMULTI()
1431 {
1432 if (!_connected)
1433 {
1434 std::cout << "Not connected, test skipped." << std::endl;
1435 return;
1436 }
1437
1438 // Make sure keys are gone from a previous testrun ...
1439 delKey("foo");
1440 delKey("bar");
1441
1442 Array command;
1443 command.add("MULTI");
1444 try
1445 {
1446 std::string result = _redis.execute<std::string>(command);
1447 assertTrue (result.compare("OK") == 0);
1448 }
1449 catch (RedisException& e)
1450 {
1451 fail(e.message());
1452 }
1453 catch (Poco::BadCastException& e)
1454 {
1455 fail(e.message());
1456 }
1457
1458 command.clear();
1459 command.add("INCR")
1460 .add("foo");
1461 try
1462 {
1463 std::string result = _redis.execute<std::string>(command);
1464 assertTrue (result.compare("QUEUED") == 0);
1465 }
1466 catch (RedisException& e)
1467 {
1468 fail(e.message());
1469 }
1470 catch (Poco::BadCastException& e)
1471 {
1472 fail(e.message());
1473 }
1474
1475 command.clear();
1476 command.add("INCR")
1477 .add("bar");
1478 try
1479 {
1480 std::string result = _redis.execute<std::string>(command);
1481 assertTrue (result.compare("QUEUED") == 0);
1482 }
1483 catch (RedisException& e)
1484 {
1485 fail(e.message());
1486 }
1487 catch (Poco::BadCastException& e)
1488 {
1489 fail(e.message());
1490 }
1491
1492 command.clear();
1493 command.add("EXEC");
1494 try
1495 {
1496 Array result = _redis.execute<Array>(command);
1497 assertTrue (result.size() == 2);
1498
1499 Poco::Int64 v = result.get<Poco::Int64>(0);
1500 assertTrue (v == 1);
1501 v = result.get<Poco::Int64>(1);
1502 assertTrue (v == 1);
1503 }
1504 catch (RedisException& e)
1505 {
1506 fail(e.message());
1507 }
1508 catch (Poco::BadCastException& e)
1509 {
1510 fail(e.message());
1511 }
1512 }
1513
1514
testPipeliningWithSendCommands()1515 void RedisTest::testPipeliningWithSendCommands()
1516 {
1517 if (!_connected)
1518 {
1519 std::cout << "Not connected, test skipped." << std::endl;
1520 return;
1521 }
1522
1523 std::vector<Array> commands;
1524
1525 Array ping;
1526 ping.add("PING");
1527 commands.push_back(ping);
1528 commands.push_back(ping);
1529
1530 Array result = _redis.sendCommands(commands);
1531
1532 // We expect 2 results
1533 assertTrue (result.size() == 2);
1534
1535 // The 2 results must be simple PONG strings
1536 for (size_t i = 0; i < 2; ++i)
1537 {
1538 try
1539 {
1540 std::string pong = result.get<std::string>(i);
1541 assertTrue (pong.compare("PONG") == 0);
1542 }
1543 catch (...)
1544 {
1545 fail("An exception occurred");
1546 }
1547 }
1548 }
1549
1550
testPipeliningWithWriteCommand()1551 void RedisTest::testPipeliningWithWriteCommand()
1552 {
1553 if (!_connected)
1554 {
1555 std::cout << "Not connected, test skipped." << std::endl;
1556 return;
1557 }
1558
1559 Array ping;
1560 ping.add("PING");
1561
1562 _redis.execute<void>(ping);
1563 _redis.execute<void>(ping);
1564 _redis.flush();
1565
1566 // We expect 2 results with simple "PONG" strings
1567 for (int i = 0; i < 2; ++i)
1568 {
1569 std::string pong;
1570 try
1571 {
1572 _redis.readReply<std::string>(pong);
1573 assertTrue (pong.compare("PONG") == 0);
1574 }
1575 catch (RedisException& e)
1576 {
1577 fail(e.message());
1578 }
1579 }
1580 }
1581
1582
1583 class RedisSubscriber
1584 {
1585 public:
onMessage(const void * pSender,RedisEventArgs & args)1586 void onMessage(const void* pSender, RedisEventArgs& args)
1587 {
1588 if (!args.message().isNull())
1589 {
1590 Type<Array>* arrayType = dynamic_cast<Type<Array>*>(args.message().get());
1591 if (arrayType != NULL)
1592 {
1593 Array& array = arrayType->value();
1594 if (array.size() == 3)
1595 {
1596 BulkString type = array.get<BulkString>(0);
1597 if (type.value().compare("unsubscribe") == 0)
1598 {
1599 Poco::Int64 n = array.get<Poco::Int64>(2);
1600 // When 0, no subscribers anymore, so stop reading ...
1601 if (n == 0) args.stop();
1602 }
1603 }
1604 else
1605 {
1606 // Wrong array received. Stop the reader
1607 args.stop();
1608 }
1609 }
1610 else
1611 {
1612 // Invalid type of message received. Stop the reader ...
1613 args.stop();
1614 }
1615 }
1616 }
1617
onError(const void * pSender,RedisEventArgs & args)1618 void onError(const void* pSender, RedisEventArgs& args)
1619 {
1620 std::cout << args.exception()->className() << std::endl;
1621 // No need to call stop, AsyncReader stops automatically when an
1622 // exception is received.
1623 }
1624 };
1625
1626
testPubSub()1627 void RedisTest::testPubSub()
1628 {
1629 if (!_connected)
1630 {
1631 std::cout << "Not connected, test skipped." << std::endl;
1632 return;
1633 }
1634
1635 RedisSubscriber subscriber;
1636
1637 Array subscribe;
1638 subscribe.add("SUBSCRIBE").add("test");
1639
1640 _redis.execute<void>(subscribe);
1641 _redis.flush();
1642
1643 AsyncReader reader(_redis);
1644 reader.redisResponse += Poco::delegate(&subscriber, &RedisSubscriber::onMessage);
1645 reader.redisException += Poco::delegate(&subscriber, &RedisSubscriber::onError);
1646 reader.start();
1647
1648 std::cout << "Sleeping ..." << std::endl;
1649 Poco::Thread::sleep(10000);
1650
1651 Array unsubscribe;
1652 unsubscribe.add("UNSUBSCRIBE");
1653
1654 _redis.execute<void>(unsubscribe);
1655 _redis.flush();
1656 }
1657
1658
testSADD()1659 void RedisTest::testSADD()
1660 {
1661 if (!_connected)
1662 {
1663 std::cout << "Not connected, test skipped." << std::endl;
1664 return;
1665 }
1666
1667 delKey("myset");
1668
1669 Command sadd = Command::sadd("myset", "Hello");
1670 try
1671 {
1672 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1673 assertTrue (result == 1);
1674 }
1675 catch (RedisException& e)
1676 {
1677 fail(e.message());
1678 }
1679
1680 sadd = Command::sadd("myset", "World");
1681 try
1682 {
1683 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1684 assertTrue (result == 1);
1685 }
1686 catch (RedisException& e)
1687 {
1688 fail(e.message());
1689 }
1690
1691 sadd = Command::sadd("myset", "World");
1692 try
1693 {
1694 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1695 assertTrue (result == 0);
1696 }
1697 catch (RedisException& e)
1698 {
1699 fail(e.message());
1700 }
1701 }
1702
1703
testSCARD()1704 void RedisTest::testSCARD()
1705 {
1706 if (!_connected)
1707 {
1708 std::cout << "Not connected, test skipped." << std::endl;
1709 return;
1710 }
1711
1712 delKey("myset");
1713
1714 Command sadd = Command::sadd("myset", "Hello");
1715 try
1716 {
1717 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1718 assertTrue (result == 1);
1719 }
1720 catch (RedisException& e)
1721 {
1722 fail(e.message());
1723 }
1724
1725 sadd = Command::sadd("myset", "World");
1726 try
1727 {
1728 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1729 assertTrue (result == 1);
1730 }
1731 catch (RedisException& e)
1732 {
1733 fail(e.message());
1734 }
1735
1736 Command scard = Command::scard("myset");
1737 try
1738 {
1739 Poco::Int64 result = _redis.execute<Poco::Int64>(scard);
1740 assertTrue (result == 2);
1741 }
1742 catch (RedisException& e)
1743 {
1744 fail(e.message());
1745 }
1746 }
1747
1748
testSDIFF()1749 void RedisTest::testSDIFF()
1750 {
1751 if (!_connected)
1752 {
1753 std::cout << "Not connected, test skipped." << std::endl;
1754 return;
1755 }
1756
1757 delKey("key1");
1758 delKey("key2");
1759
1760 std::vector<std::string> values1;
1761 values1.push_back("a");
1762 values1.push_back("b");
1763 values1.push_back("c");
1764 Command sadd = Command::sadd("key1", values1);
1765 try
1766 {
1767 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1768 assertTrue (result == 3);
1769 }
1770 catch (RedisException& e)
1771 {
1772 fail(e.message());
1773 }
1774
1775 std::vector<std::string> values2;
1776 values2.push_back("c");
1777 values2.push_back("d");
1778 values2.push_back("e");
1779 sadd = Command::sadd("key2", values2);
1780 try
1781 {
1782 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1783 assertTrue (result == 3);
1784 }
1785 catch (RedisException& e)
1786 {
1787 fail(e.message());
1788 }
1789
1790 Command sdiff = Command::sdiff("key1", "key2");
1791 try
1792 {
1793 Array result = _redis.execute<Array>(sdiff);
1794 assertTrue (result.size() == 2);
1795 }
1796 catch (RedisException& e)
1797 {
1798 fail(e.message());
1799 }
1800 }
1801
1802
testSDIFFSTORE()1803 void RedisTest::testSDIFFSTORE()
1804 {
1805 if (!_connected)
1806 {
1807 std::cout << "Not connected, test skipped." << std::endl;
1808 return;
1809 }
1810
1811 delKey("key");
1812 delKey("key1");
1813 delKey("key2");
1814
1815 std::vector<std::string> values1;
1816 values1.push_back("a");
1817 values1.push_back("b");
1818 values1.push_back("c");
1819 Command sadd = Command::sadd("key1", values1);
1820 try
1821 {
1822 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1823 assertTrue (result == 3);
1824 }
1825 catch (RedisException& e)
1826 {
1827 fail(e.message());
1828 }
1829
1830 std::vector<std::string> values2;
1831 values2.push_back("c");
1832 values2.push_back("d");
1833 values2.push_back("e");
1834 sadd = Command::sadd("key2", values2);
1835 try
1836 {
1837 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1838 assertTrue (result == 3);
1839 }
1840 catch (RedisException& e)
1841 {
1842 fail(e.message());
1843 }
1844
1845 Command sdiffstore = Command::sdiffstore("key", "key1", "key2");
1846 try
1847 {
1848 Poco::Int64 result = _redis.execute<Poco::Int64>(sdiffstore);
1849 assertTrue (result == 2);
1850 }
1851 catch (RedisException& e)
1852 {
1853 fail(e.message());
1854 }
1855
1856 Command smembers = Command::smembers("key");
1857 try
1858 {
1859 Array result = _redis.execute<Array>(smembers);
1860 assertTrue (result.size() == 2);
1861 }
1862 catch (RedisException& e)
1863 {
1864 fail(e.message());
1865 }
1866 }
1867
1868
testSET()1869 void RedisTest::testSET()
1870 {
1871 if (!_connected)
1872 {
1873 std::cout << "Not connected, test skipped." << std::endl;
1874 return;
1875 }
1876
1877 Array command;
1878 command.add("SET").add("mykey").add("Hello");
1879
1880 // A set responds with a simple OK string
1881 try
1882 {
1883 std::string result = _redis.execute<std::string>(command);
1884 assertTrue (result.compare("OK") == 0);
1885 }
1886 catch (RedisException& e)
1887 {
1888 fail(e.message());
1889 }
1890
1891 command.add("NX");
1892 // A set NX responds with a Null bulk string
1893 // when the key is already set
1894 try
1895 {
1896 BulkString result = _redis.execute<BulkString>(command);
1897 assertTrue (result.isNull());
1898 }
1899 catch (RedisException& e)
1900 {
1901 fail(e.message());
1902 }
1903 }
1904
1905
testSINTER()1906 void RedisTest::testSINTER()
1907 {
1908 if (!_connected)
1909 {
1910 std::cout << "Not connected, test skipped." << std::endl;
1911 return;
1912 }
1913
1914 delKey("key1");
1915 delKey("key2");
1916
1917 std::vector<std::string> values1;
1918 values1.push_back("a");
1919 values1.push_back("b");
1920 values1.push_back("c");
1921 Command sadd = Command::sadd("key1", values1);
1922 try
1923 {
1924 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1925 assertTrue (result == 3);
1926 }
1927 catch (RedisException& e)
1928 {
1929 fail(e.message());
1930 }
1931
1932 std::vector<std::string> values2;
1933 values2.push_back("c");
1934 values2.push_back("d");
1935 values2.push_back("e");
1936 sadd = Command::sadd("key2", values2);
1937 try
1938 {
1939 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1940 assertTrue (result == 3);
1941 }
1942 catch (RedisException& e)
1943 {
1944 fail(e.message());
1945 }
1946
1947 Command sinter = Command::sinter("key1", "key2");
1948 try
1949 {
1950 Array result = _redis.execute<Array>(sinter);
1951 assertTrue (result.size() == 1);
1952 assertTrue (result.get<BulkString>(0).value().compare("c") == 0);
1953 }
1954 catch (RedisException& e)
1955 {
1956 fail(e.message());
1957 }
1958 }
1959
1960
testSINTERSTORE()1961 void RedisTest::testSINTERSTORE()
1962 {
1963 if (!_connected)
1964 {
1965 std::cout << "Not connected, test skipped." << std::endl;
1966 return;
1967 }
1968
1969 delKey("key");
1970 delKey("key1");
1971 delKey("key2");
1972
1973 std::vector<std::string> values1;
1974 values1.push_back("a");
1975 values1.push_back("b");
1976 values1.push_back("c");
1977 Command sadd = Command::sadd("key1", values1);
1978 try
1979 {
1980 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1981 assertTrue (result == 3);
1982 }
1983 catch (RedisException& e)
1984 {
1985 fail(e.message());
1986 }
1987
1988 std::vector<std::string> values2;
1989 values2.push_back("c");
1990 values2.push_back("d");
1991 values2.push_back("e");
1992 sadd = Command::sadd("key2", values2);
1993 try
1994 {
1995 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
1996 assertTrue (result == 3);
1997 }
1998 catch (RedisException& e)
1999 {
2000 fail(e.message());
2001 }
2002
2003 Command sinterstore = Command::sinterstore("key", "key1", "key2");
2004 try
2005 {
2006 Poco::Int64 result = _redis.execute<Poco::Int64>(sinterstore);
2007 assertTrue (result == 1);
2008 }
2009 catch (RedisException& e)
2010 {
2011 fail(e.message());
2012 }
2013
2014 Command smembers = Command::smembers("key");
2015 try
2016 {
2017 Array result = _redis.execute<Array>(smembers);
2018 assertTrue (result.size() == 1);
2019 assertTrue (result.get<BulkString>(0).value().compare("c") == 0);
2020 }
2021 catch (RedisException& e)
2022 {
2023 fail(e.message());
2024 }
2025 }
2026
2027
testSISMEMBER()2028 void RedisTest::testSISMEMBER()
2029 {
2030 if (!_connected)
2031 {
2032 std::cout << "Not connected, test skipped." << std::endl;
2033 return;
2034 }
2035
2036 delKey("myset");
2037
2038 Command sadd = Command::sadd("myset", "one");
2039 try
2040 {
2041 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2042 assertTrue (result == 1);
2043 }
2044 catch (RedisException& e)
2045 {
2046 fail(e.message());
2047 }
2048
2049 Command sismember = Command::sismember("myset", "one");
2050 try
2051 {
2052 Poco::Int64 result = _redis.execute<Poco::Int64>(sismember);
2053 assertTrue (result == 1);
2054 }
2055 catch (RedisException& e)
2056 {
2057 fail(e.message());
2058 }
2059
2060 sismember = Command::sismember("myset", "two");
2061 try
2062 {
2063 Poco::Int64 result = _redis.execute<Poco::Int64>(sismember);
2064 assertTrue (result == 0);
2065 }
2066 catch (RedisException& e)
2067 {
2068 fail(e.message());
2069 }
2070 }
2071
2072
testSMEMBERS()2073 void RedisTest::testSMEMBERS()
2074 {
2075 if (!_connected)
2076 {
2077 std::cout << "Not connected, test skipped." << std::endl;
2078 return;
2079 }
2080
2081 delKey("myset");
2082
2083 Command sadd = Command::sadd("myset", "Hello");
2084 try
2085 {
2086 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2087 assertTrue (result == 1);
2088 }
2089 catch (RedisException& e)
2090 {
2091 fail(e.message());
2092 }
2093
2094 sadd = Command::sadd("myset", "World");
2095 try
2096 {
2097 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2098 assertTrue (result == 1);
2099 }
2100 catch (RedisException& e)
2101 {
2102 fail(e.message());
2103 }
2104
2105 Command smembers = Command::smembers("myset");
2106 try
2107 {
2108 Array result = _redis.execute<Array>(smembers);
2109 assertTrue (result.size() == 2);
2110 }
2111 catch (RedisException& e)
2112 {
2113 fail(e.message());
2114 }
2115 }
2116
2117
testSMOVE()2118 void RedisTest::testSMOVE()
2119 {
2120 if (!_connected)
2121 {
2122 std::cout << "Not connected, test skipped." << std::endl;
2123 return;
2124 }
2125
2126 delKey("myset");
2127 delKey("myotherset");
2128
2129 Command sadd = Command::sadd("myset", "one");
2130 try
2131 {
2132 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2133 assertTrue (result == 1);
2134 }
2135 catch (RedisException& e)
2136 {
2137 fail(e.message());
2138 }
2139
2140 sadd = Command::sadd("myset", "two");
2141 try
2142 {
2143 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2144 assertTrue (result == 1);
2145 }
2146 catch (RedisException& e)
2147 {
2148 fail(e.message());
2149 }
2150
2151 sadd = Command::sadd("myotherset", "three");
2152 try
2153 {
2154 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2155 assertTrue (result == 1);
2156 }
2157 catch (RedisException& e)
2158 {
2159 fail(e.message());
2160 }
2161
2162 Command smove = Command::smove("myset", "myotherset", "two");
2163 try
2164 {
2165 Poco::Int64 result = _redis.execute<Poco::Int64>(smove);
2166 assertTrue (result == 1);
2167 }
2168 catch (RedisException& e)
2169 {
2170 fail(e.message());
2171 }
2172
2173 Command smembers = Command::smembers("myset");
2174 try
2175 {
2176 Array result = _redis.execute<Array>(smembers);
2177 assertTrue (result.size() == 1);
2178 assertTrue (result.get<BulkString>(0).value().compare("one") == 0);
2179 }
2180 catch (RedisException& e)
2181 {
2182 fail(e.message());
2183 }
2184
2185 smembers = Command::smembers("myotherset");
2186 try
2187 {
2188 Array result = _redis.execute<Array>(smembers);
2189 assertTrue (result.size() == 2);
2190 }
2191 catch (RedisException& e)
2192 {
2193 fail(e.message());
2194 }
2195 }
2196
2197
testSPOP()2198 void RedisTest::testSPOP()
2199 {
2200 if (!_connected)
2201 {
2202 std::cout << "Not connected, test skipped." << std::endl;
2203 return;
2204 }
2205
2206 delKey("myset");
2207
2208 Command sadd = Command::sadd("myset", "one");
2209 try
2210 {
2211 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2212 assertTrue (result == 1);
2213 }
2214 catch (RedisException& e)
2215 {
2216 fail(e.message());
2217 }
2218
2219 sadd = Command::sadd("myset", "two");
2220 try
2221 {
2222 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2223 assertTrue (result == 1);
2224 }
2225 catch (RedisException& e)
2226 {
2227 fail(e.message());
2228 }
2229
2230 sadd = Command::sadd("myset", "three");
2231 try
2232 {
2233 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2234 assertTrue (result == 1);
2235 }
2236 catch (RedisException& e)
2237 {
2238 fail(e.message());
2239 }
2240
2241 Command spop = Command::spop("myset");
2242 try
2243 {
2244 BulkString result = _redis.execute<BulkString>(spop);
2245 assertTrue (!result.isNull());
2246 }
2247 catch (RedisException& e)
2248 {
2249 fail(e.message());
2250 }
2251
2252 Command smembers = Command::smembers("myset");
2253 try
2254 {
2255 Array result = _redis.execute<Array>(smembers);
2256 assertTrue (result.size() == 2);
2257 }
2258 catch (RedisException& e)
2259 {
2260 fail(e.message());
2261 }
2262
2263 sadd = Command::sadd("myset", "four");
2264 try
2265 {
2266 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2267 assertTrue (result == 1);
2268 }
2269 catch (RedisException& e)
2270 {
2271 fail(e.message());
2272 }
2273
2274 sadd = Command::sadd("myset", "five");
2275 try
2276 {
2277 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2278 assertTrue (result == 1);
2279 }
2280 catch (RedisException& e)
2281 {
2282 fail(e.message());
2283 }
2284 // Redis server 3.0.5 doesn't support this yet ..
2285 /*
2286 spop = Command::spop("myset", 3);
2287 try
2288 {
2289 Array result = _redis.execute<Array>(spop);
2290 assertTrue (result.size() == 3);
2291 }
2292 catch (RedisException& e)
2293 {
2294 fail(e.message());
2295 }
2296 */
2297 }
2298
2299
testSRANDMEMBER()2300 void RedisTest::testSRANDMEMBER()
2301 {
2302 if (!_connected)
2303 {
2304 std::cout << "Not connected, test skipped." << std::endl;
2305 return;
2306 }
2307
2308 delKey("myset");
2309
2310 std::vector<std::string> members;
2311 members.push_back("one");
2312 members.push_back("two");
2313 members.push_back("three");
2314
2315 Command sadd = Command::sadd("myset", members);
2316 try
2317 {
2318 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2319 assertTrue (result == 3);
2320 }
2321 catch (RedisException& e)
2322 {
2323 fail(e.message());
2324 }
2325
2326 Command srandmember = Command::srandmember("myset");
2327 try
2328 {
2329 BulkString result = _redis.execute<BulkString>(srandmember);
2330 assertTrue (!result.isNull());
2331 }
2332 catch (RedisException& e)
2333 {
2334 fail(e.message());
2335 }
2336
2337 srandmember = Command::srandmember("myset", 2);
2338 try
2339 {
2340 Array result = _redis.execute<Array>(srandmember);
2341 assertTrue (result.size() == 2);
2342 }
2343 catch (RedisException& e)
2344 {
2345 fail(e.message());
2346 }
2347
2348 srandmember = Command::srandmember("myset", -5);
2349 try
2350 {
2351 Array result = _redis.execute<Array>(srandmember);
2352 assertTrue (result.size() == 5);
2353 }
2354 catch (RedisException& e)
2355 {
2356 fail(e.message());
2357 }
2358 }
2359
2360
testSTRLEN()2361 void RedisTest::testSTRLEN()
2362 {
2363 if (!_connected)
2364 {
2365 std::cout << "Not connected, test skipped." << std::endl;
2366 return;
2367 }
2368
2369 Array command;
2370 command.add("SET").add("mykey").add("Hello World");
2371
2372 // A set responds with a simple OK string
2373 try
2374 {
2375 std::string result = _redis.execute<std::string>(command);
2376 assertTrue (result.compare("OK") == 0);
2377 }
2378 catch (RedisException& e)
2379 {
2380 fail(e.message());
2381 }
2382
2383 command.clear();
2384 command.add("STRLEN")
2385 .add("mykey");
2386
2387 try
2388 {
2389 Poco::Int64 result = _redis.execute<Poco::Int64>(command);
2390
2391 assertTrue (result == 11);
2392 }
2393 catch (RedisException& e)
2394 {
2395 fail(e.message());
2396 }
2397 }
2398
2399
testSREM()2400 void RedisTest::testSREM()
2401 {
2402 if (!_connected)
2403 {
2404 std::cout << "Not connected, test skipped." << std::endl;
2405 return;
2406 }
2407
2408 delKey("myset");
2409
2410 Command sadd = Command::sadd("myset", "one");
2411 try
2412 {
2413 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2414 assertTrue (result == 1);
2415 }
2416 catch (RedisException& e)
2417 {
2418 fail(e.message());
2419 }
2420 sadd = Command::sadd("myset", "two");
2421 try
2422 {
2423 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2424 assertTrue (result == 1);
2425 }
2426 catch (RedisException& e)
2427 {
2428 fail(e.message());
2429 }
2430 sadd = Command::sadd("myset", "three");
2431 try
2432 {
2433 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2434 assertTrue (result == 1);
2435 }
2436 catch (RedisException& e)
2437 {
2438 fail(e.message());
2439 }
2440
2441 Command srem = Command::srem("myset", "one");
2442 try
2443 {
2444 Poco::Int64 result = _redis.execute<Poco::Int64>(srem);
2445 assertTrue (result == 1);
2446 }
2447 catch (RedisException& e)
2448 {
2449 fail(e.message());
2450 }
2451
2452 srem = Command::srem("myset", "four");
2453 try
2454 {
2455 Poco::Int64 result = _redis.execute<Poco::Int64>(srem);
2456 assertTrue (result == 0);
2457 }
2458 catch (RedisException& e)
2459 {
2460 fail(e.message());
2461 }
2462
2463 Command smembers = Command::smembers("myset");
2464 try
2465 {
2466 Array result = _redis.execute<Array>(smembers);
2467 assertTrue (result.size() == 2);
2468 }
2469 catch (RedisException& e)
2470 {
2471 fail(e.message());
2472 }
2473 }
2474
2475
testSUNION()2476 void RedisTest::testSUNION()
2477 {
2478 if (!_connected)
2479 {
2480 std::cout << "Not connected, test skipped." << std::endl;
2481 return;
2482 }
2483
2484 delKey("key1");
2485 delKey("key2");
2486
2487 std::vector<std::string> values1;
2488 values1.push_back("a");
2489 values1.push_back("b");
2490 values1.push_back("c");
2491 Command sadd = Command::sadd("key1", values1);
2492 try
2493 {
2494 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2495 assertTrue (result == 3);
2496 }
2497 catch (RedisException& e)
2498 {
2499 fail(e.message());
2500 }
2501
2502 std::vector<std::string> values2;
2503 values2.push_back("c");
2504 values2.push_back("d");
2505 values2.push_back("e");
2506 sadd = Command::sadd("key2", values2);
2507 try
2508 {
2509 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2510 assertTrue (result == 3);
2511 }
2512 catch (RedisException& e)
2513 {
2514 fail(e.message());
2515 }
2516
2517 Command sunion = Command::sunion("key1", "key2");
2518 try
2519 {
2520 Array result = _redis.execute<Array>(sunion);
2521 assertTrue (result.size() == 5);
2522 }
2523 catch (RedisException& e)
2524 {
2525 fail(e.message());
2526 }
2527 }
2528
2529
testSUNIONSTORE()2530 void RedisTest::testSUNIONSTORE()
2531 {
2532 if (!_connected)
2533 {
2534 std::cout << "Not connected, test skipped." << std::endl;
2535 return;
2536 }
2537
2538 delKey("key");
2539 delKey("key1");
2540 delKey("key2");
2541
2542 std::vector<std::string> values1;
2543 values1.push_back("a");
2544 values1.push_back("b");
2545 values1.push_back("c");
2546 Command sadd = Command::sadd("key1", values1);
2547 try
2548 {
2549 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2550 assertTrue (result == 3);
2551 }
2552 catch (RedisException& e)
2553 {
2554 fail(e.message());
2555 }
2556
2557 std::vector<std::string> values2;
2558 values2.push_back("c");
2559 values2.push_back("d");
2560 values2.push_back("e");
2561 sadd = Command::sadd("key2", values2);
2562 try
2563 {
2564 Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
2565 assertTrue (result == 3);
2566 }
2567 catch (RedisException& e)
2568 {
2569 fail(e.message());
2570 }
2571
2572 Command sunionstore = Command::sunionstore("key", "key1", "key2");
2573 try
2574 {
2575 Poco::Int64 result = _redis.execute<Poco::Int64>(sunionstore);
2576 assertTrue (result == 5);
2577 }
2578 catch (RedisException& e)
2579 {
2580 fail(e.message());
2581 }
2582
2583 Command smembers = Command::smembers("key");
2584 try
2585 {
2586 Array result = _redis.execute<Array>(smembers);
2587 assertTrue (result.size() == 5);
2588 }
2589 catch (RedisException& e)
2590 {
2591 fail(e.message());
2592 }
2593 }
2594
2595
testRENAME()2596 void RedisTest::testRENAME()
2597 {
2598 if (!_connected)
2599 {
2600 std::cout << "Not connected, test skipped." << std::endl;
2601 return;
2602 }
2603
2604 Command set = Command::set("mykey", "Hello");
2605 try
2606 {
2607 std::string result = _redis.execute<std::string>(set);
2608 assertTrue (result.compare("OK") == 0);
2609 }
2610 catch (RedisException& e)
2611 {
2612 fail(e.message());
2613 }
2614
2615 Command rename = Command::rename("mykey", "myotherkey");
2616 try
2617 {
2618 std::string result = _redis.execute<std::string>(rename);
2619 assertTrue (result.compare("OK") == 0);
2620 }
2621 catch (RedisException& e)
2622 {
2623 fail(e.message());
2624 }
2625
2626 Command get = Command::get("myotherkey");
2627 try
2628 {
2629 BulkString result = _redis.execute<BulkString>(get);
2630 assertTrue (result.value().compare("Hello") == 0);
2631 }
2632 catch (RedisException& e)
2633 {
2634 fail(e.message());
2635 }
2636 }
2637
2638
testRENAMENX()2639 void RedisTest::testRENAMENX()
2640 {
2641 if (!_connected)
2642 {
2643 std::cout << "Not connected, test skipped." << std::endl;
2644 return;
2645 }
2646
2647 Command set = Command::set("mykey", "Hello");
2648 try
2649 {
2650 std::string result = _redis.execute<std::string>(set);
2651 assertTrue (result.compare("OK") == 0);
2652 }
2653 catch (RedisException& e)
2654 {
2655 fail(e.message());
2656 }
2657
2658 set = Command::set("myotherkey", "World");
2659 try
2660 {
2661 std::string result = _redis.execute<std::string>(set);
2662 assertTrue (result.compare("OK") == 0);
2663 }
2664 catch (RedisException& e)
2665 {
2666 fail(e.message());
2667 }
2668
2669 Command rename = Command::rename("mykey", "myotherkey", false);
2670 try
2671 {
2672 Poco::Int64 result = _redis.execute<Poco::Int64>(rename);
2673 assertTrue (result == 0);
2674 }
2675 catch (RedisException& e)
2676 {
2677 fail(e.message());
2678 }
2679
2680 Command get = Command::get("myotherkey");
2681 try
2682 {
2683 BulkString result = _redis.execute<BulkString>(get);
2684 assertTrue (result.value().compare("World") == 0);
2685 }
2686 catch (RedisException& e)
2687 {
2688 fail(e.message());
2689 }
2690 }
2691
2692
testRPOP()2693 void RedisTest::testRPOP()
2694 {
2695 if (!_connected)
2696 {
2697 std::cout << "Not connected, test skipped." << std::endl;
2698 return;
2699 }
2700
2701 // Make sure the list is not there yet ...
2702 delKey("mylist");
2703
2704 try
2705 {
2706 Command rpush = Command::rpush("mylist", "one");
2707 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
2708 assertTrue (result == 1);
2709
2710 rpush = Command::rpush("mylist", "two");
2711 result = _redis.execute<Poco::Int64>(rpush);
2712 assertTrue (result == 2);
2713
2714 rpush = Command::rpush("mylist", "three");
2715 result = _redis.execute<Poco::Int64>(rpush);
2716 assertTrue (result == 3);
2717 }
2718 catch (RedisException& e)
2719 {
2720 fail(e.message());
2721 }
2722
2723 Command rpop = Command::rpop("mylist");
2724 try
2725 {
2726 BulkString result = _redis.execute<BulkString>(rpop);
2727 assertTrue (result.value().compare("three") == 0);
2728 }
2729 catch (RedisException& e)
2730 {
2731 fail(e.message());
2732 }
2733
2734 Command lrange = Command::lrange("mylist");
2735 try
2736 {
2737 Array result = _redis.execute<Array>(lrange);
2738
2739 assertTrue (result.size() == 2);
2740 assertTrue (result.get<BulkString>(0).value().compare("one") == 0);
2741 assertTrue (result.get<BulkString>(1).value().compare("two") == 0);
2742 }
2743 catch (RedisException& e)
2744 {
2745 fail(e.message());
2746 }
2747 catch (Poco::NullValueException& e)
2748 {
2749 fail(e.message());
2750 }
2751
2752 }
2753
2754
testRPOPLPUSH()2755 void RedisTest::testRPOPLPUSH()
2756 {
2757 if (!_connected)
2758 {
2759 std::cout << "Not connected, test skipped." << std::endl;
2760 return;
2761 }
2762
2763 // Make sure the lists are not there yet ...
2764 std::vector<std::string> lists;
2765 lists.push_back("mylist");
2766 lists.push_back("myotherlist");
2767 Command delCommand = Command::del(lists);
2768 try
2769 {
2770 _redis.execute<Poco::Int64>(delCommand);
2771 }
2772 catch (RedisException& e)
2773 {
2774 fail(e.message());
2775 }
2776 catch (Poco::BadCastException& e)
2777 {
2778 fail(e.message());
2779 }
2780
2781 try
2782 {
2783 Command rpush = Command::rpush("mylist", "one");
2784 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
2785 assertTrue (result == 1);
2786
2787 rpush = Command::rpush("mylist", "two");
2788 result = _redis.execute<Poco::Int64>(rpush);
2789 assertTrue (result == 2);
2790
2791 rpush = Command::rpush("mylist", "three");
2792 result = _redis.execute<Poco::Int64>(rpush);
2793 assertTrue (result == 3);
2794 }
2795 catch (RedisException& e)
2796 {
2797 fail(e.message());
2798 }
2799
2800 Command rpoplpush = Command::rpoplpush("mylist", "myotherlist");
2801 try
2802 {
2803 BulkString result = _redis.execute<BulkString>(rpoplpush);
2804 assertTrue (result.value().compare("three") == 0);
2805 }
2806 catch (RedisException& e)
2807 {
2808 fail(e.message());
2809 }
2810
2811 Command lrange = Command::lrange("mylist");
2812 try
2813 {
2814 Array result = _redis.execute<Array>(lrange);
2815
2816 assertTrue (result.size() == 2);
2817 assertTrue (result.get<BulkString>(0).value().compare("one") == 0);
2818 assertTrue (result.get<BulkString>(1).value().compare("two") == 0);
2819 }
2820 catch (RedisException& e)
2821 {
2822 fail(e.message());
2823 }
2824 catch (Poco::NullValueException& e)
2825 {
2826 fail(e.message());
2827 }
2828
2829 lrange = Command::lrange("myotherlist");
2830 try
2831 {
2832 Array result = _redis.execute<Array>(lrange);
2833
2834 assertTrue (result.size() == 1);
2835 assertTrue (result.get<BulkString>(0).value().compare("three") == 0);
2836 }
2837 catch (RedisException& e)
2838 {
2839 fail(e.message());
2840 }
2841 catch (Poco::NullValueException& e)
2842 {
2843 fail(e.message());
2844 }
2845 }
2846
2847
testRPUSH()2848 void RedisTest::testRPUSH()
2849 {
2850 if (!_connected)
2851 {
2852 std::cout << "Not connected, test skipped." << std::endl;
2853 return;
2854 }
2855
2856 // Make sure the list is not there yet ...
2857 delKey("mylist");
2858
2859 try
2860 {
2861 Command rpush = Command::rpush("mylist", "World");
2862 Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
2863 assertTrue (result == 1);
2864
2865 rpush = Command::rpush("mylist", "Hello");
2866 result = _redis.execute<Poco::Int64>(rpush);
2867 assertTrue (result == 2);
2868 }
2869 catch (RedisException& e)
2870 {
2871 fail(e.message());
2872 }
2873
2874 Command llen = Command::llen("mylist");
2875 try
2876 {
2877 Poco::Int64 n = _redis.execute<Poco::Int64>(llen);
2878 assertTrue (n == 2);
2879 }
2880 catch (RedisException& e)
2881 {
2882 fail(e.message());
2883 }
2884 catch (Poco::BadCastException& e)
2885 {
2886 fail(e.message());
2887 }
2888
2889 Command lrange = Command::lrange("mylist", 0, -1);
2890 try
2891 {
2892 Array result = _redis.execute<Array>(lrange);
2893
2894 assertTrue (result.size() == 2);
2895 assertTrue (result.get<BulkString>(0).value().compare("World") == 0);
2896 assertTrue (result.get<BulkString>(1).value().compare("Hello") == 0);
2897 }
2898 catch (RedisException& e)
2899 {
2900 fail(e.message());
2901 }
2902 catch (Poco::NullValueException& e)
2903 {
2904 fail(e.message());
2905 }
2906 }
2907
2908
testPool()2909 void RedisTest::testPool()
2910 {
2911 Poco::Net::SocketAddress sa(_host, _port);
2912 Poco::PoolableObjectFactory<Client, Client::Ptr> factory(sa);
2913 Poco::ObjectPool<Client, Client::Ptr> pool(factory, 10, 15);
2914
2915 delKey("mypoolkey");
2916
2917 PooledConnection pclient1(pool);
2918 PooledConnection pclient2(pool);
2919 assertTrue (pool.size() == 2);
2920
2921 Command set = Command::set("mypoolkey", "Hello");
2922 std::string result = ((Client::Ptr) pclient1)->execute<std::string>(set);
2923 assertTrue (result.compare("OK") == 0);
2924
2925 Array get;
2926 get << "GET" << "mypoolkey";
2927 BulkString keyValue = ((Client::Ptr) pclient2)->execute<BulkString>(get);
2928 assertTrue (keyValue.value().compare("Hello") == 0);
2929 }
2930
2931
delKey(const std::string & key)2932 void RedisTest::delKey(const std::string& key)
2933 {
2934 Command delCommand = Command::del(key);
2935 try
2936 {
2937 _redis.execute<Poco::Int64>(delCommand);
2938 }
2939 catch (RedisException& e)
2940 {
2941 fail(e.message());
2942 }
2943 catch (Poco::BadCastException& e)
2944 {
2945 fail(e.message());
2946 }
2947 }
2948
2949
suite()2950 CppUnit::Test* RedisTest::suite()
2951 {
2952 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RedisTest");
2953
2954 CppUnit_addTest(pSuite, RedisTest, testAPPEND);
2955 CppUnit_addTest(pSuite, RedisTest, testBLPOP);
2956 CppUnit_addTest(pSuite, RedisTest, testBRPOP);
2957 CppUnit_addTest(pSuite, RedisTest, testDECR);
2958 CppUnit_addTest(pSuite, RedisTest, testDECR);
2959 CppUnit_addTest(pSuite, RedisTest, testECHO);
2960 CppUnit_addTest(pSuite, RedisTest, testError);
2961 CppUnit_addTest(pSuite, RedisTest, testEVAL);
2962 CppUnit_addTest(pSuite, RedisTest, testHDEL);
2963 CppUnit_addTest(pSuite, RedisTest, testHEXISTS);
2964 CppUnit_addTest(pSuite, RedisTest, testHGETALL);
2965 CppUnit_addTest(pSuite, RedisTest, testHINCRBY);
2966 CppUnit_addTest(pSuite, RedisTest, testHKEYS);
2967 CppUnit_addTest(pSuite, RedisTest, testHMGET);
2968 CppUnit_addTest(pSuite, RedisTest, testHMSET);
2969 CppUnit_addTest(pSuite, RedisTest, testHSET);
2970 //CppUnit_addTest(pSuite, RedisTest, testHSTRLEN);
2971 CppUnit_addTest(pSuite, RedisTest, testHVALS);
2972 CppUnit_addTest(pSuite, RedisTest, testINCR);
2973 CppUnit_addTest(pSuite, RedisTest, testINCRBY);
2974 CppUnit_addTest(pSuite, RedisTest, testLINDEX);
2975 CppUnit_addTest(pSuite, RedisTest, testLINSERT);
2976 CppUnit_addTest(pSuite, RedisTest, testLPOP);
2977 CppUnit_addTest(pSuite, RedisTest, testLREM);
2978 CppUnit_addTest(pSuite, RedisTest, testLSET);
2979 CppUnit_addTest(pSuite, RedisTest, testLTRIM);
2980 CppUnit_addTest(pSuite, RedisTest, testMSET);
2981 CppUnit_addTest(pSuite, RedisTest, testMSETWithMap);
2982 CppUnit_addTest(pSuite, RedisTest, testMULTI);
2983 CppUnit_addTest(pSuite, RedisTest, testPING);
2984 CppUnit_addTest(pSuite, RedisTest, testPipeliningWithSendCommands);
2985 CppUnit_addTest(pSuite, RedisTest, testPipeliningWithWriteCommand);
2986 CppUnit_addTest(pSuite, RedisTest, testPubSub);
2987 CppUnit_addTest(pSuite, RedisTest, testSADD);
2988 CppUnit_addTest(pSuite, RedisTest, testSCARD);
2989 CppUnit_addTest(pSuite, RedisTest, testSDIFF);
2990 CppUnit_addTest(pSuite, RedisTest, testSDIFFSTORE);
2991 CppUnit_addTest(pSuite, RedisTest, testSET);
2992 CppUnit_addTest(pSuite, RedisTest, testSINTER);
2993 CppUnit_addTest(pSuite, RedisTest, testSINTERSTORE);
2994 CppUnit_addTest(pSuite, RedisTest, testSISMEMBER);
2995 CppUnit_addTest(pSuite, RedisTest, testSMEMBERS);
2996 CppUnit_addTest(pSuite, RedisTest, testSMOVE);
2997 CppUnit_addTest(pSuite, RedisTest, testSPOP);
2998 CppUnit_addTest(pSuite, RedisTest, testSRANDMEMBER);
2999 CppUnit_addTest(pSuite, RedisTest, testSREM);
3000 CppUnit_addTest(pSuite, RedisTest, testSTRLEN);
3001 CppUnit_addTest(pSuite, RedisTest, testSUNION);
3002 CppUnit_addTest(pSuite, RedisTest, testSUNIONSTORE);
3003 CppUnit_addTest(pSuite, RedisTest, testRENAME);
3004 CppUnit_addTest(pSuite, RedisTest, testRENAMENX);
3005 CppUnit_addTest(pSuite, RedisTest, testRPOP);
3006 CppUnit_addTest(pSuite, RedisTest, testRPOPLPUSH);
3007 CppUnit_addTest(pSuite, RedisTest, testRPUSH);
3008 CppUnit_addTest(pSuite, RedisTest, testPool);
3009 return pSuite;
3010 }
3011