Add blpop / brpop / brpoplpush

This commit is contained in:
fbraem 2015-11-14 19:39:16 +01:00
parent ed0b61c6e1
commit 4e5fe3f874
4 changed files with 161 additions and 0 deletions

View File

@ -44,6 +44,15 @@ public:
static Command append(const std::string& key, const std::string& value);
/// Returns an APPEND command
static Command blpop(const std::vector<std::string>& lists, Int64 timeout = 0);
/// Returns a BLPOP command
static Command brpop(const std::vector<std::string>& lists, Int64 timeout = 0);
/// Returns a BRPOP command
static Command brpoplpush(const std::string& sourceList, const std::string& destinationList, Int64 timeout = 0);
/// Returns a BRPOPLPUSH command
static Command decr(const std::string& key, Int64 by = 0);
/// Returns an DECR or DECRBY command. Calls DECR when by is omitted or zero.

View File

@ -43,6 +43,41 @@ Command Command::append(const std::string& key, const std::string& value)
return cmd;
}
Command Command::blpop(const std::vector<std::string>& lists, Int64 timeout)
{
Command cmd("BLPOP");
for(std::vector<std::string>::const_iterator it = lists.begin(); it != lists.end(); ++it)
{
cmd << *it;
}
cmd << NumberFormatter::format(timeout);
return cmd;
}
Command Command::brpop(const std::vector<std::string>& lists, Int64 timeout)
{
Command cmd("BRPOP");
for(std::vector<std::string>::const_iterator it = lists.begin(); it != lists.end(); ++it)
{
cmd << *it;
}
cmd << NumberFormatter::format(timeout);
return cmd;
}
Command Command::brpoplpush(const std::string& sourceList, const std::string& destinationList, Int64 timeout)
{
Command cmd("BRPOPLPUSH");
cmd << sourceList << destinationList << NumberFormatter::format(timeout);
return cmd;
}
Command Command::decr(const std::string& key, Int64 by)
{
Command cmd(by == 0 ? "DECR" : "DECRBY");

View File

@ -128,6 +128,118 @@ void RedisTest::testAppend()
}
}
void RedisTest::testBLpop()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
// Make sure the lists are not there yet ...
std::vector<std::string> lists;
lists.push_back("list1");
lists.push_back("list2");
Command delCommand = Command::del(lists);
try
{
_redis.execute<Poco::Int64>(delCommand);
}
catch(RedisException& e)
{
fail(e.message());
}
catch(Poco::BadCastException& e)
{
fail(e.message());
}
std::vector<std::string> values;
values.push_back("a");
values.push_back("b");
values.push_back("c");
try
{
Command rpush = Command::rpush("list1", values);
Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command blpop = Command::blpop(lists);
try
{
Array result = _redis.execute<Array>(blpop);
assert(result.size() == 2);
assert(result.get<BulkString>(0).value().compare("list1") == 0);
assert(result.get<BulkString>(1).value().compare("a") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testBRpop()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
// Make sure the lists are not there yet ...
std::vector<std::string> lists;
lists.push_back("list1");
lists.push_back("list2");
Command delCommand = Command::del(lists);
try
{
_redis.execute<Poco::Int64>(delCommand);
}
catch(RedisException& e)
{
fail(e.message());
}
catch(Poco::BadCastException& e)
{
fail(e.message());
}
std::vector<std::string> values;
values.push_back("a");
values.push_back("b");
values.push_back("c");
try
{
Command rpush = Command::rpush("list1", values);
Poco::Int64 result = _redis.execute<Poco::Int64>(rpush);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command brpop = Command::brpop(lists);
try
{
Array result = _redis.execute<Array>(brpop);
assert(result.size() == 2);
assert(result.get<BulkString>(0).value().compare("list1") == 0);
assert(result.get<BulkString>(1).value().compare("c") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testDecr()
{
if (!_connected)
@ -1309,6 +1421,9 @@ CppUnit::Test* RedisTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RedisTest");
CppUnit_addTest(pSuite, RedisTest, testAppend);
CppUnit_addTest(pSuite, RedisTest, testBLpop);
CppUnit_addTest(pSuite, RedisTest, testBRpop);
CppUnit_addTest(pSuite, RedisTest, testDecr);
CppUnit_addTest(pSuite, RedisTest, testDecr);
CppUnit_addTest(pSuite, RedisTest, testIncr);
CppUnit_addTest(pSuite, RedisTest, testIncrBy);

View File

@ -31,6 +31,8 @@ public:
virtual ~RedisTest();
void testAppend();
void testBLpop();
void testBRpop();
void testDecr();
void testEcho();
void testError();