mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-07 07:19:08 +01:00
Work on pubsub sample
This commit is contained in:
parent
cbb45f5b25
commit
dfc89d09de
@ -10,11 +10,12 @@
|
|||||||
//
|
//
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "Poco/Net/NetException.h"
|
#include "Poco/Exception.h"
|
||||||
#include "Poco/Delegate.h"
|
#include "Poco/Delegate.h"
|
||||||
#include "Poco/Thread.h"
|
#include "Poco/Thread.h"
|
||||||
|
|
||||||
#include "RedisTest.h"
|
#include "RedisTest.h"
|
||||||
|
#include "Poco/Redis/AsyncReader.h"
|
||||||
|
|
||||||
#include "CppUnit/TestCaller.h"
|
#include "CppUnit/TestCaller.h"
|
||||||
#include "CppUnit/TestSuite.h"
|
#include "CppUnit/TestSuite.h"
|
||||||
@ -23,7 +24,7 @@ using namespace Poco::Redis;
|
|||||||
|
|
||||||
|
|
||||||
bool RedisTest::_connected = false;
|
bool RedisTest::_connected = false;
|
||||||
Poco::Redis::AsyncClient RedisTest::_redis;
|
Poco::Redis::Client RedisTest::_redis;
|
||||||
|
|
||||||
|
|
||||||
RedisTest::RedisTest(const std::string& name):
|
RedisTest::RedisTest(const std::string& name):
|
||||||
@ -35,14 +36,15 @@ RedisTest::RedisTest(const std::string& name):
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Poco::Timespan t(30, 0); // 30 seconds
|
Poco::Timespan t(10, 0); // Connect within 10 seconds
|
||||||
_redis.connect(_host, _port, t);
|
_redis.connect(_host, _port, t);
|
||||||
|
_redis.setReceiveTimeout(t); // Receive answers within 10 seconds
|
||||||
_connected = true;
|
_connected = true;
|
||||||
std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl;
|
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);
|
||||||
commands.push_back(ping);
|
commands.push_back(ping);
|
||||||
|
|
||||||
Array result = _redis.sendCommands(commands);
|
Array result = _redis.sendCommands(commands);
|
||||||
|
|
||||||
// We expect 2 results
|
// We expect 2 results
|
||||||
assert(result.size() == 2);
|
assert(result.size() == 2);
|
||||||
@ -673,7 +675,7 @@ void RedisTest::testPipeliningWithSendCommands()
|
|||||||
// The 2 results must be simple PONG strings
|
// The 2 results must be simple PONG strings
|
||||||
for(size_t i = 0; i < 2; ++i)
|
for(size_t i = 0; i < 2; ++i)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string pong = result.get<std::string>(i);
|
std::string pong = result.get<std::string>(i);
|
||||||
assert(pong.compare("PONG") == 0);
|
assert(pong.compare("PONG") == 0);
|
||||||
@ -703,7 +705,7 @@ void RedisTest::testPipeliningWithWriteCommand()
|
|||||||
for(int i = 0; i < 2; ++i)
|
for(int i = 0; i < 2; ++i)
|
||||||
{
|
{
|
||||||
std::string pong;
|
std::string pong;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_redis.readReply<std::string>(pong);
|
_redis.readReply<std::string>(pong);
|
||||||
assert(pong.compare("PONG") == 0);
|
assert(pong.compare("PONG") == 0);
|
||||||
@ -728,26 +730,30 @@ public:
|
|||||||
|
|
||||||
void RedisTest::testPubSub()
|
void RedisTest::testPubSub()
|
||||||
{
|
{
|
||||||
|
if (!_connected)
|
||||||
|
{
|
||||||
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
RedisSubscriber subscriber;
|
RedisSubscriber subscriber;
|
||||||
|
|
||||||
Array subscribe;
|
Array subscribe;
|
||||||
subscribe.add("SUBSCRIBE")
|
subscribe.add("SUBSCRIBE")
|
||||||
.add("test");
|
.add("test");
|
||||||
|
|
||||||
Array subscribeReply = _redis.execute<Array>(subscribe);
|
_redis.execute<void>(subscribe);
|
||||||
|
|
||||||
_redis.redisResponse += Poco::delegate(&subscriber, &RedisSubscriber::onMessage);
|
AsyncReader reader(_redis);
|
||||||
_redis.start();
|
reader.redisResponse += Poco::delegate(&subscriber, &RedisSubscriber::onMessage);
|
||||||
|
reader.start();
|
||||||
|
|
||||||
Poco::Thread::sleep(30000);
|
Poco::Thread::sleep(30000);
|
||||||
|
|
||||||
Array unsubscribe;
|
Array unsubscribe;
|
||||||
unsubscribe.add("UNSUBSCRIBE");
|
unsubscribe.add("UNSUBSCRIBE");
|
||||||
|
|
||||||
Array unsubscribeReply = _redis.execute<Array>(unsubscribe);
|
_redis.execute<void>(unsubscribe);
|
||||||
std::cout << "SUBS: " << unsubscribeReply.toString() << std::endl;
|
|
||||||
|
|
||||||
_redis.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CppUnit::Test* RedisTest::suite()
|
CppUnit::Test* RedisTest::suite()
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Redis/Redis.h"
|
#include "Poco/Redis/Redis.h"
|
||||||
#include "Poco/Redis/AsyncClient.h"
|
#include "Poco/Redis/Client.h"
|
||||||
|
|
||||||
#include "CppUnit/TestCase.h"
|
#include "CppUnit/TestCase.h"
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ private:
|
|||||||
std::string _host;
|
std::string _host;
|
||||||
unsigned _port;
|
unsigned _port;
|
||||||
static bool _connected;
|
static bool _connected;
|
||||||
static Poco::Redis::AsyncClient _redis;
|
static Poco::Redis::Client _redis;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user