Commit e5096e35 authored by Pavel Vainerman's avatar Pavel Vainerman

Небольшой рефакторинг TCPCheck, сделал тест для него.

parent a88afa45
#ifndef _TCPCheck_H_
#define _TCPCheck_H_
// -----------------------------------------------------------------------------
#include <atomic>
#include <cc++/socket.h>
#include "Mutex.h"
#include "ThreadCreator.h"
......@@ -17,33 +18,29 @@ class TCPCheck
TCPCheck();
~TCPCheck();
bool check( const std::string& _ip, int _port, timeout_t tout, timeout_t sleep_msec );
/*! Проверка связи с узлом
* \param _ip - ip проверяемого узла
* \param _port - порт для проверяемого узла
* \param tout - таймаут на попытку
* \param sleep_msec - пауза между проверками результата
*/
bool check( const std::string& _ip, int _port, timeout_t tout, timeout_t sleep_msec = 50 );
/*! \param iaddr - 'ip:port' */
bool check( const std::string& iaddr, timeout_t tout, timeout_t sleep_msec );
bool check( const std::string& iaddr, timeout_t tout, timeout_t sleep_msec = 50);
protected:
void check_thread();
inline void setResult( bool res )
{
UniSetTypes::uniset_rwmutex_wrlock l(m);
result = res;
}
inline bool getResult()
void setResult( bool s )
{
bool res = false;
{
UniSetTypes::uniset_rwmutex_rlock l(m);
res = result;
}
return res;
result = s;
}
bool result;
std::string iaddr;
std::atomic_bool result = {false};
std::string ip = {""};
int port = {0};
int tout_msec;
UniSetTypes::uniset_rwmutex m;
};
// -----------------------------------------------------------------------------
#endif // _TCPCheck_H_
......
......@@ -3,11 +3,12 @@
#include "UniSetTypes.h"
#include "PassiveTimer.h"
#include "TCPCheck.h"
#include "UTCPStream.h"
// -----------------------------------------------------------------------------
using namespace std;
// -----------------------------------------------------------------------------
TCPCheck::TCPCheck():
iaddr(""), tout_msec(0)
tout_msec(0)
{
}
// -----------------------------------------------------------------------------
......@@ -16,19 +17,24 @@ TCPCheck::~TCPCheck()
}
// -----------------------------------------------------------------------------
bool TCPCheck::check( const std::string& ip, int port, timeout_t tout, timeout_t sleep_msec )
bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep_msec )
{
ostringstream s;
s << ip << ":" << port;
return check(s.str(), tout, sleep_msec);
auto v = UniSetTypes::explode_str(_iaddr, ':');
if( v.size() < 2 )
return false;
return check( v[0], UniSetTypes::uni_atoi(v[1]), tout, sleep_msec );
}
// -----------------------------------------------------------------------------
bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep_msec )
bool TCPCheck::check( const std::string& _ip, int _port, timeout_t tout, timeout_t sleep_msec )
{
iaddr = iaddr;
ip = _ip;
port = _port;
tout_msec = tout;
setResult(false);
ThreadCreator<TCPCheck> t(this, &TCPCheck::check_thread);
t.setCancel(ost::Thread::cancelDeferred);
t.start();
......@@ -41,7 +47,7 @@ bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep
if( t.isRunning() ) // !getResult() )
t.stop();
return getResult();
return result;
}
// -----------------------------------------------------------------------------
void TCPCheck::check_thread()
......@@ -51,7 +57,8 @@ void TCPCheck::check_thread()
try
{
ost::Thread::setException(ost::Thread::throwException);
ost::TCPStream t(iaddr.c_str(), ost::Socket::IPV4, 536, true, tout_msec);
UTCPStream t;
t.create(ip, port, true, tout_msec);
setResult(true);
t.disconnect();
}
......
......@@ -25,7 +25,8 @@ test_pulse.cc \
test_modbustypes.cc \
test_utypes.cc \
test_mutex.cc \
test_logserver.cc
test_logserver.cc \
test_tcpcheck.cc
tests_with_conf_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_with_conf_CPPFLAGS = -I$(top_builddir)/include
......
#include <catch.hpp>
#include <thread>
#include <atomic>
#include <future>
#include <ostream>
#include "TCPCheck.h"
#include "UniSetTypes.h"
using namespace std;
static int port = 2048;
static std::string host = "localhost";
static atomic_bool cancel = {false};
// --------------------------------------------------------
bool run_test_server()
{
ost::InetAddress addr = host.c_str();
ost::TCPSocket sock(addr, port);
while( !cancel )
{
if( sock.isPendingConnection(500) ) {}
}
return true;
}
// --------------------------------------------------------
TEST_CASE("TCPCheck", "[tcpcheck]" )
{
TCPCheck t;
auto res = std::async(std::launch::async, run_test_server);
ostringstream ia;
ia << host << ":" << port;
CHECK( t.check(host, port, 300) );
CHECK( t.check(ia.str(), 300) );
CHECK_FALSE( t.check("dummy_host_name", port, 300) );
CHECK_FALSE( t.check("dummy_host_name:2048", 300) );
cancel = true;
CHECK( res.get() );
}
// --------------------------------------------------------
......@@ -412,6 +412,7 @@ tests/test_ui.cc
tests/test_unixml.cc
tests/test_utypes.cc
tests/test_logserver.cc
tests/test_tcpcheck.cc
tests/tests-junit.xml
tests/tests.cc
tests/tests_bad_config.xml
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment