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