tests: Add ConnectionMonitorTest

This commit is contained in:
lganzzzo 2021-09-23 00:21:49 +03:00
parent 964a68253e
commit 0ed9ce14dc
6 changed files with 122 additions and 9 deletions

View File

@ -136,6 +136,13 @@ void ConnectionMonitor::Monitor::monitorTask(std::shared_ptr<Monitor> monitor) {
}
{
std::lock_guard<std::mutex>(monitor->m_runMutex);
monitor->m_stopped = true;
}
monitor->m_runCondition.notify_all();
}
void* ConnectionMonitor::Monitor::createOrGetMetricData(ConnectionStats& stats, const std::shared_ptr<StatCollector>& collector) {
@ -155,6 +162,7 @@ std::shared_ptr<ConnectionMonitor::Monitor> ConnectionMonitor::Monitor::createSh
std::thread t([monitor](){
ConnectionMonitor::Monitor::monitorTask(monitor);
});
t.detach();
return monitor;
}
@ -246,6 +254,10 @@ void ConnectionMonitor::Monitor::onConnectionWrite(ConnectionStats& stats, v_io_
void ConnectionMonitor::Monitor::stop() {
m_running = false;
std::unique_lock<std::mutex> runLock(m_runMutex);
while(!m_stopped) {
m_runCondition.wait(runLock);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -268,6 +280,10 @@ void ConnectionMonitor::addStatCollector(const std::shared_ptr<StatCollector>& c
m_monitor->addStatCollector(collector);
}
void ConnectionMonitor::addAnalyser(const std::shared_ptr<MetricAnalyser>& analyser) {
m_monitor->addAnalyser(analyser);
}
void ConnectionMonitor::stop() {
m_monitor->stop();
}

View File

@ -28,7 +28,8 @@
#include "./ConnectionProvider.hpp"
#include "oatpp/core/data/stream/Stream.hpp"
#include "unordered_map"
#include <unordered_map>
#include <condition_variable>
namespace oatpp { namespace network {
@ -54,7 +55,7 @@ public:
public:
class StatCollector {
class StatCollector : public oatpp::base::Countable {
public:
virtual ~StatCollector() = default;
@ -67,7 +68,7 @@ public:
public:
class MetricAnalyser {
class MetricAnalyser : public oatpp::base::Countable {
public:
virtual ~MetricAnalyser() = default;
@ -116,10 +117,13 @@ private:
private:
class Monitor {
class Monitor : public oatpp::base::Countable {
private:
std::mutex m_runMutex;
std::condition_variable m_runCondition;
std::atomic<bool> m_running {true};
bool m_stopped {false};
std::mutex m_connectionsMutex;
std::unordered_map<v_uint64, std::weak_ptr<ConnectionProxy>> m_connections;
@ -155,7 +159,7 @@ private:
private:
std::shared_ptr<Monitor> m_monitor;
std::shared_ptr<ConnectionProvider> m_connectionProvider;
protected:
public:
ConnectionMonitor(const std::shared_ptr<ConnectionProvider>& connectionProvider);
@ -163,6 +167,8 @@ protected:
void addStatCollector(const std::shared_ptr<StatCollector>& collector);
void addAnalyser(const std::shared_ptr<MetricAnalyser>& analyser);
void stop();
};

View File

@ -61,6 +61,8 @@ add_executable(oatppAllTests
oatpp/encoding/Base64Test.hpp
oatpp/encoding/UnicodeTest.cpp
oatpp/encoding/UnicodeTest.hpp
oatpp/network/ConnectionMonitorTest.cpp
oatpp/network/ConnectionMonitorTest.hpp
oatpp/network/ConnectionPoolTest.cpp
oatpp/network/ConnectionPoolTest.hpp
oatpp/network/UrlTest.cpp

View File

@ -15,6 +15,7 @@
#include "oatpp/network/virtual_/InterfaceTest.hpp"
#include "oatpp/network/UrlTest.hpp"
#include "oatpp/network/ConnectionPoolTest.hpp"
#include "oatpp/network/ConnectionMonitorTest.hpp"
#include "oatpp/parser/json/mapping/DeserializerTest.hpp"
#include "oatpp/parser/json/mapping/DTOMapperPerfTest.hpp"
@ -75,7 +76,7 @@ void runTests() {
OATPP_LOGD("Tests", "coroutine size=%d", sizeof(oatpp::async::AbstractCoroutine));
OATPP_LOGD("Tests", "action size=%d", sizeof(oatpp::async::Action));
OATPP_LOGD("Tests", "class count=%d", oatpp::data::mapping::type::ClassId::getClassCount());
/*
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
OATPP_RUN_TEST(oatpp::test::base::LoggerTest);
@ -125,9 +126,10 @@ void runTests() {
OATPP_RUN_TEST(oatpp::test::encoding::UnicodeTest);
OATPP_RUN_TEST(oatpp::test::network::UrlTest);
OATPP_RUN_TEST(oatpp::test::network::ConnectionPoolTest);
*/
OATPP_RUN_TEST(oatpp::test::network::ConnectionMonitorTest);
/*
OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest);
OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest);
@ -198,7 +200,7 @@ void runTests() {
test_port.run();
}
*/
}
}

View File

@ -0,0 +1,44 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#include "ConnectionMonitorTest.hpp"
#include "oatpp/network/ConnectionMonitor.hpp"
#include "oatpp/network/tcp/client/ConnectionProvider.hpp"
#include <thread>
namespace oatpp { namespace test { namespace network {
void ConnectionMonitorTest::onRun() {
auto connectionProvider = oatpp::network::tcp::client::ConnectionProvider::createShared({"oatpp.io", 80});
oatpp::network::ConnectionMonitor monitor(connectionProvider);
monitor.stop();
}
}}}

View File

@ -0,0 +1,43 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#ifndef oatpp_test_network_ConnectionMonitorTest_hpp
#define oatpp_test_network_ConnectionMonitorTest_hpp
#include "oatpp-test/UnitTest.hpp"
namespace oatpp { namespace test { namespace network {
class ConnectionMonitorTest : public UnitTest {
public:
ConnectionMonitorTest():UnitTest("TEST[network::ConnectionMonitorTest]"){}
void onRun() override;
};
}}}
#endif // oatpp_test_network_ConnectionMonitorTest_hpp