RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* redis_async_test.cc 00002 Wolfgang Sourdeau, 11 December 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 Test commands supported by our Redis class 00006 */ 00007 00008 #define BOOST_TEST_MAIN 00009 #define BOOST_TEST_DYN_LINK 00010 00011 #include <unordered_map> 00012 00013 #include <boost/test/unit_test.hpp> 00014 00015 #include "soa/service/redis.h" 00016 #include "soa/service/testing/redis_temporary_server.h" 00017 00018 using namespace std; 00019 using namespace Redis; 00020 00021 00022 BOOST_AUTO_TEST_CASE( test_redis_commands ) 00023 { 00024 RedisTemporaryServer redis; 00025 AsyncConnection connection(redis); 00026 00027 /* MSET is supported */ 00028 Command mset(MSET); 00029 mset.addArg("testkey1"); mset.addArg(1234); 00030 mset.addArg("testkey2"); mset.addArg(9876); 00031 mset.addArg("testkey3"); mset.addArg("textvalue"); 00032 00033 Result result = connection.exec(mset); 00034 BOOST_CHECK_EQUAL(result.ok(), true); 00035 00036 /* MGET is supported and that all slots requested must be returned in the 00037 form of an array, including empty ones, without errors */ 00038 Command mget(MGET); 00039 mget.addArg("testkey1"); 00040 mget.addArg("testkey3"); 00041 mget.addArg("poil"); 00042 00043 result = connection.exec(mget); 00044 BOOST_CHECK_EQUAL(result.ok(), true); 00045 Reply reply = result.reply(); 00046 BOOST_CHECK_EQUAL(reply.type(), ARRAY); 00047 00048 Reply subreply = reply[0]; /* testkey1 */ 00049 BOOST_CHECK_EQUAL(subreply.type(), STRING); 00050 BOOST_CHECK_EQUAL(subreply.asString(), "1234"); 00051 /* FIXME: expected behaviour: 00052 BOOST_CHECK_EQUAL(subreply.type(), INTEGER); 00053 BOOST_CHECK_EQUAL(subreply.asInt(), 1234); 00054 */ 00055 00056 subreply = reply[1]; /* testkey3 */ 00057 BOOST_CHECK_EQUAL(subreply.type(), STRING); 00058 BOOST_CHECK_EQUAL(string(subreply), "textvalue"); 00059 00060 subreply = reply[2]; /* poil */ 00061 BOOST_CHECK_EQUAL(subreply.type(), NIL); 00062 00063 00064 /* SADD is supported and each slot is used once per key, even when 00065 specified multiple times */ 00066 Command sadd(SADD); 00067 sadd.addArg("my-new-set"); 00068 sadd.addArg("testkey1"); 00069 sadd.addArg("testkey2"); 00070 sadd.addArg("testkey2"); 00071 00072 result = connection.exec(sadd); 00073 BOOST_CHECK_EQUAL(result.ok(), true); 00074 00075 /* SMEMBERS is supported and does return the values registered above */ 00076 Command smembers(SMEMBERS); 00077 smembers.addArg("my-new-set"); 00078 00079 result = connection.exec(smembers); 00080 BOOST_CHECK_EQUAL(result.ok(), true); 00081 reply = result.reply(); 00082 BOOST_CHECK_EQUAL(reply.type(), ARRAY); 00083 BOOST_CHECK_EQUAL(reply.length(), 2); /* testkey1, testkey2 */ 00084 00085 /* members are not specifically returned in order */ 00086 unordered_map<string, bool> keyMap; 00087 subreply = reply[0]; 00088 BOOST_CHECK_EQUAL(subreply.type(), STRING); 00089 keyMap[subreply.asString()] = true; 00090 subreply = reply[1]; 00091 BOOST_CHECK_EQUAL(subreply.type(), STRING); 00092 keyMap[subreply.asString()] = true; 00093 00094 BOOST_CHECK_EQUAL(keyMap["testkey1"], true); 00095 BOOST_CHECK_EQUAL(keyMap["testkey2"], true); 00096 }