Commit e5096e35 authored by Pavel Vainerman's avatar Pavel Vainerman

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

parent a88afa45
#ifndef _TCPCheck_H_ #ifndef _TCPCheck_H_
#define _TCPCheck_H_ #define _TCPCheck_H_
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#include <atomic>
#include <cc++/socket.h> #include <cc++/socket.h>
#include "Mutex.h" #include "Mutex.h"
#include "ThreadCreator.h" #include "ThreadCreator.h"
...@@ -17,33 +18,29 @@ class TCPCheck ...@@ -17,33 +18,29 @@ class TCPCheck
TCPCheck(); 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' */ /*! \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: protected:
void check_thread(); 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; result = s;
{
UniSetTypes::uniset_rwmutex_rlock l(m);
res = result;
}
return res;
} }
bool result; std::atomic_bool result = {false};
std::string iaddr; std::string ip = {""};
int port = {0};
int tout_msec; int tout_msec;
UniSetTypes::uniset_rwmutex m;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#endif // _TCPCheck_H_ #endif // _TCPCheck_H_
......
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
#include "UniSetTypes.h" #include "UniSetTypes.h"
#include "PassiveTimer.h" #include "PassiveTimer.h"
#include "TCPCheck.h" #include "TCPCheck.h"
#include "UTCPStream.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using namespace std; using namespace std;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
TCPCheck::TCPCheck(): TCPCheck::TCPCheck():
iaddr(""), tout_msec(0) tout_msec(0)
{ {
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -16,19 +17,24 @@ TCPCheck::~TCPCheck() ...@@ -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; auto v = UniSetTypes::explode_str(_iaddr, ':');
s << ip << ":" << port;
return check(s.str(), tout, sleep_msec); 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; tout_msec = tout;
setResult(false); setResult(false);
ThreadCreator<TCPCheck> t(this, &TCPCheck::check_thread); ThreadCreator<TCPCheck> t(this, &TCPCheck::check_thread);
t.setCancel(ost::Thread::cancelDeferred); t.setCancel(ost::Thread::cancelDeferred);
t.start(); t.start();
...@@ -41,7 +47,7 @@ bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep ...@@ -41,7 +47,7 @@ bool TCPCheck::check( const std::string& _iaddr, timeout_t tout, timeout_t sleep
if( t.isRunning() ) // !getResult() ) if( t.isRunning() ) // !getResult() )
t.stop(); t.stop();
return getResult(); return result;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void TCPCheck::check_thread() void TCPCheck::check_thread()
...@@ -51,7 +57,8 @@ void TCPCheck::check_thread() ...@@ -51,7 +57,8 @@ void TCPCheck::check_thread()
try try
{ {
ost::Thread::setException(ost::Thread::throwException); 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); setResult(true);
t.disconnect(); t.disconnect();
} }
......
...@@ -25,7 +25,8 @@ test_pulse.cc \ ...@@ -25,7 +25,8 @@ test_pulse.cc \
test_modbustypes.cc \ test_modbustypes.cc \
test_utypes.cc \ test_utypes.cc \
test_mutex.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_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_with_conf_CPPFLAGS = -I$(top_builddir)/include 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 ...@@ -412,6 +412,7 @@ tests/test_ui.cc
tests/test_unixml.cc tests/test_unixml.cc
tests/test_utypes.cc tests/test_utypes.cc
tests/test_logserver.cc tests/test_logserver.cc
tests/test_tcpcheck.cc
tests/tests-junit.xml tests/tests-junit.xml
tests/tests.cc tests/tests.cc
tests/tests_bad_config.xml 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