From dfc89d09deb4008e230054c6faca57e9891a5d24 Mon Sep 17 00:00:00 2001 From: fbraem Date: Mon, 2 Nov 2015 22:09:35 +0100 Subject: [PATCH] Work on pubsub sample --- Redis/testsuite/src/RedisTest.cpp | 36 ++++++++++++++++++------------- Redis/testsuite/src/RedisTest.h | 4 ++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index f9798a6b6..b7dec0184 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -10,11 +10,12 @@ // #include -#include "Poco/Net/NetException.h" +#include "Poco/Exception.h" #include "Poco/Delegate.h" #include "Poco/Thread.h" #include "RedisTest.h" +#include "Poco/Redis/AsyncReader.h" #include "CppUnit/TestCaller.h" #include "CppUnit/TestSuite.h" @@ -23,7 +24,7 @@ using namespace Poco::Redis; bool RedisTest::_connected = false; -Poco::Redis::AsyncClient RedisTest::_redis; +Poco::Redis::Client RedisTest::_redis; RedisTest::RedisTest(const std::string& name): @@ -35,14 +36,15 @@ RedisTest::RedisTest(const std::string& name): { try { - Poco::Timespan t(30, 0); // 30 seconds + Poco::Timespan t(10, 0); // Connect within 10 seconds _redis.connect(_host, _port, t); + _redis.setReceiveTimeout(t); // Receive answers within 10 seconds _connected = true; std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl; } - catch (Poco::Net::ConnectionRefusedException& e) + catch (Poco::Exception& e) { - std::cout << "Couldn't connect to " << e.message() << ". " << std::endl; + std::cout << "Couldn't connect to [" << _host << ':' << _port << ']' << e.message() << ". " << std::endl; } } } @@ -665,7 +667,7 @@ void RedisTest::testPipeliningWithSendCommands() commands.push_back(ping); commands.push_back(ping); - Array result = _redis.sendCommands(commands); + Array result = _redis.sendCommands(commands); // We expect 2 results assert(result.size() == 2); @@ -673,7 +675,7 @@ void RedisTest::testPipeliningWithSendCommands() // The 2 results must be simple PONG strings for(size_t i = 0; i < 2; ++i) { - try + try { std::string pong = result.get(i); assert(pong.compare("PONG") == 0); @@ -703,7 +705,7 @@ void RedisTest::testPipeliningWithWriteCommand() for(int i = 0; i < 2; ++i) { std::string pong; - try + try { _redis.readReply(pong); assert(pong.compare("PONG") == 0); @@ -728,26 +730,30 @@ public: void RedisTest::testPubSub() { + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + RedisSubscriber subscriber; Array subscribe; subscribe.add("SUBSCRIBE") .add("test"); - Array subscribeReply = _redis.execute(subscribe); + _redis.execute(subscribe); - _redis.redisResponse += Poco::delegate(&subscriber, &RedisSubscriber::onMessage); - _redis.start(); + AsyncReader reader(_redis); + reader.redisResponse += Poco::delegate(&subscriber, &RedisSubscriber::onMessage); + reader.start(); Poco::Thread::sleep(30000); Array unsubscribe; unsubscribe.add("UNSUBSCRIBE"); - Array unsubscribeReply = _redis.execute(unsubscribe); - std::cout << "SUBS: " << unsubscribeReply.toString() << std::endl; - - _redis.stop(); + _redis.execute(unsubscribe); } CppUnit::Test* RedisTest::suite() diff --git a/Redis/testsuite/src/RedisTest.h b/Redis/testsuite/src/RedisTest.h index e2f46f672..369f6fb9f 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -17,7 +17,7 @@ #include "Poco/Redis/Redis.h" -#include "Poco/Redis/AsyncClient.h" +#include "Poco/Redis/Client.h" #include "CppUnit/TestCase.h" @@ -57,7 +57,7 @@ private: std::string _host; unsigned _port; static bool _connected; - static Poco::Redis::AsyncClient _redis; + static Poco::Redis::Client _redis; };