diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index 096499a04..1cfa879e6 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -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& lists, Int64 timeout = 0); + /// Returns a BLPOP command + + static Command brpop(const std::vector& 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. diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 6957d96f4..382e3b528 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -43,6 +43,41 @@ Command Command::append(const std::string& key, const std::string& value) return cmd; } +Command Command::blpop(const std::vector& lists, Int64 timeout) +{ + Command cmd("BLPOP"); + + for(std::vector::const_iterator it = lists.begin(); it != lists.end(); ++it) + { + cmd << *it; + } + cmd << NumberFormatter::format(timeout); + + return cmd; +} + +Command Command::brpop(const std::vector& lists, Int64 timeout) +{ + Command cmd("BRPOP"); + + for(std::vector::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"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index e008092d3..c27278950 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -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 lists; + lists.push_back("list1"); + lists.push_back("list2"); + Command delCommand = Command::del(lists); + try + { + _redis.execute(delCommand); + } + catch(RedisException& e) + { + fail(e.message()); + } + catch(Poco::BadCastException& e) + { + fail(e.message()); + } + + std::vector 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(rpush); + assert(result == 3); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command blpop = Command::blpop(lists); + try + { + Array result = _redis.execute(blpop); + assert(result.size() == 2); + assert(result.get(0).value().compare("list1") == 0); + assert(result.get(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 lists; + lists.push_back("list1"); + lists.push_back("list2"); + Command delCommand = Command::del(lists); + try + { + _redis.execute(delCommand); + } + catch(RedisException& e) + { + fail(e.message()); + } + catch(Poco::BadCastException& e) + { + fail(e.message()); + } + + std::vector 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(rpush); + assert(result == 3); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command brpop = Command::brpop(lists); + try + { + Array result = _redis.execute(brpop); + assert(result.size() == 2); + assert(result.get(0).value().compare("list1") == 0); + assert(result.get(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); diff --git a/Redis/testsuite/src/RedisTest.h b/Redis/testsuite/src/RedisTest.h index aedc50b13..155031a20 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -31,6 +31,8 @@ public: virtual ~RedisTest(); void testAppend(); + void testBLpop(); + void testBRpop(); void testDecr(); void testEcho(); void testError();