From ae4da000abaa376a3cfc1e6ff8df58e0b4be9efd Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 6 Nov 2019 04:45:27 +0200 Subject: [PATCH] Tests. web/ClientRetryTest. draft. --- test/oatpp/AllTestsMain.cpp | 16 +++- test/oatpp/web/ClientRetryTest.cpp | 120 ++++++++++++++++++++++++----- test/oatpp/web/app/Client.hpp | 1 + test/oatpp/web/app/Controller.hpp | 19 +++-- 4 files changed, 127 insertions(+), 29 deletions(-) diff --git a/test/oatpp/AllTestsMain.cpp b/test/oatpp/AllTestsMain.cpp index 799a6210..f3b48672 100644 --- a/test/oatpp/AllTestsMain.cpp +++ b/test/oatpp/AllTestsMain.cpp @@ -1,4 +1,6 @@ +#include "oatpp/web/ClientRetryTest.hpp" + #include "oatpp/web/FullTest.hpp" #include "oatpp/web/FullAsyncTest.hpp" #include "oatpp/web/FullAsyncClientTest.hpp" @@ -58,7 +60,7 @@ void runTests() { OATPP_LOGD("aaa", "coroutine size=%d", sizeof(oatpp::async::AbstractCoroutine)); OATPP_LOGD("aaa", "action size=%d", sizeof(oatpp::async::Action)); - +/* OATPP_RUN_TEST(oatpp::test::base::RegRuleTest); OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest); @@ -98,7 +100,7 @@ void runTests() { OATPP_RUN_TEST(oatpp::test::web::server::api::ApiControllerTest); OATPP_RUN_TEST(oatpp::test::web::server::handler::AuthorizationHandlerTest); - +*/ { oatpp::test::web::PipelineTest test_virtual(0, 3000); @@ -149,6 +151,16 @@ void runTests() { } + { + + oatpp::test::web::ClientRetryTest test_virtual(0); + test_virtual.run(); + + oatpp::test::web::ClientRetryTest test_port(8000); + test_port.run(); + + } + } } diff --git a/test/oatpp/web/ClientRetryTest.cpp b/test/oatpp/web/ClientRetryTest.cpp index 0362d798..d26aaf67 100644 --- a/test/oatpp/web/ClientRetryTest.cpp +++ b/test/oatpp/web/ClientRetryTest.cpp @@ -123,26 +123,32 @@ public: }; -void runServer(v_int32 port, const std::shared_ptr& controller) { +void runServer(v_int32 port, v_int32 delaySeconds, v_int32 iterations, bool stable, const std::shared_ptr& controller, bool wakeupServer) { TestServerComponent component(port); oatpp::test::web::ClientServerTestRunner runner; - runner.addController(app::Controller::createShared()); + runner.addController(controller); - runner.run([&runner] { + runner.run([&runner, delaySeconds, iterations, stable, controller, wakeupServer] { - std::this_thread::sleep_for(std::chrono::seconds(1)); + for(v_int32 i = 0; i < iterations; i ++) { + std::this_thread::sleep_for(std::chrono::seconds(delaySeconds)); + if(!stable) { + controller->available = !controller->available; + OATPP_LOGI("Server", "Available=%d", (v_int32)controller->available.load()); + } + } - /////////////////////////////////////////////////////////////////////////////////////////////////////// - // Stop server and unblock accepting thread + if(wakeupServer) { - runner.getServer()->stop(); - OATPP_COMPONENT(std::shared_ptr, connectionProvider); - connectionProvider->getConnection(); + runner.getServer()->stop(); + OATPP_COMPONENT(std::shared_ptr, connectionProvider); + connectionProvider->getConnection(); + + } - /////////////////////////////////////////////////////////////////////////////////////////////////////// }, std::chrono::minutes(10)); @@ -162,27 +168,99 @@ void ClientRetryTest::onRun() { OATPP_COMPONENT(std::shared_ptr, connectionProvider); - //auto connectionPool = std::make_shared(connectionProvider, 10, std::chrono::seconds(5)); +// { +// +// oatpp::test::PerformanceChecker checker("test: no server available"); +// +// auto retryPolicy = std::make_shared(2, std::chrono::seconds(1)); +// auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionProvider, retryPolicy); +// auto client = app::Client::createShared(requestExecutor, objectMapper); +// +// auto response = client->getRoot(); +// auto ticks = checker.getElapsedTicks(); +// +// OATPP_ASSERT(response.get() == nullptr); +// OATPP_ASSERT(ticks >= 2 * 1000 * 1000 /* 2s */); +// OATPP_ASSERT(ticks < 3 * 1000 * 1000 /* 3s */); +// +// } +// +// { +// +// oatpp::test::PerformanceChecker checker("test: server pops up"); +// +// auto retryPolicy = std::make_shared(10 * 10, std::chrono::milliseconds(100)); +// auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionProvider, retryPolicy); +// auto client = app::Client::createShared(requestExecutor, objectMapper); +// +// std::list threads; +// +// for(v_int32 i = 0; i < 100; i ++) { +// threads.push_back(std::thread([client]{ +// auto response = client->getRoot(); +// OATPP_ASSERT(response); +// OATPP_ASSERT(response->getStatusCode() == 200); +// auto data = response->readBodyToString(); +// OATPP_ASSERT(data == "Hello World!!!"); +// })); +// } +// +// OATPP_LOGD(TAG, "Waiting for server to start..."); +// std::this_thread::sleep_for(std::chrono::seconds(3)); +// +// runServer(m_port, 1, 1, true, controller, true); +// +// for(std::thread& thread : threads) { +// thread.join(); +// } +// +// auto ticks = checker.getElapsedTicks(); +// OATPP_ASSERT(ticks < 10 * 1000 * 1000 /* 10s */); +// +// } { - oatpp::test::PerformanceChecker checker("no server available"); + OATPP_LOGI(TAG, "Test: unstable server!"); - auto retryPolicy = std::make_shared(2, std::chrono::seconds(1)); - auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionProvider, retryPolicy); + auto retryPolicy = std::make_shared(-1, std::chrono::seconds(1)); + auto connectionPool = std::make_shared(connectionProvider, 10, std::chrono::seconds(1)); + auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionPool, retryPolicy); auto client = app::Client::createShared(requestExecutor, objectMapper); - auto response = client->getRoot(); - auto ticks = checker.getElapsedTicks(); + std::list threads; + std::atomic testIsRunning(true); - OATPP_ASSERT(response.get() == nullptr); - OATPP_ASSERT(ticks >= 2 * 1000 * 1000 /* 2s */); - OATPP_ASSERT(ticks < 3 * 1000 * 1000 /* 2s */); + std::thread clientThread([client, &testIsRunning]{ + + v_int64 counter = 0; + + v_int64 tick0 = oatpp::base::Environment::getMicroTickCount(); + + while(oatpp::base::Environment::getMicroTickCount() - tick0 < 10 * 1000 * 1000) { + + auto response = client->getAvailability(); + OATPP_ASSERT(response); + OATPP_ASSERT(response->getStatusCode() == 200); + auto data = response->readBodyToString(); + OATPP_ASSERT(data == "Hello World!!!"); + counter ++; + + if(counter % 1000 == 0) { + OATPP_LOGD("client", "requests=%d", counter); + } + + } + + }); + + runServer(m_port, 2, 6, false, controller, true); + + clientThread.join(); } - //runServer(m_port, controller); - //runServer(m_port, controller); + std::this_thread::sleep_for(std::chrono::seconds(2)); // wait connection pool. } diff --git a/test/oatpp/web/app/Client.hpp b/test/oatpp/web/app/Client.hpp index 2f3587da..ad6eebee 100644 --- a/test/oatpp/web/app/Client.hpp +++ b/test/oatpp/web/app/Client.hpp @@ -42,6 +42,7 @@ public: API_CLIENT_INIT(Client) API_CALL("GET", "/", getRoot) + API_CALL("GET", "/availability", getAvailability) API_CALL("GET", "/cors", getCors) API_CALL("OPTIONS", "/cors", optionsCors) API_CALL("GET", "/cors-origin", getCorsOrigin) diff --git a/test/oatpp/web/app/Controller.hpp b/test/oatpp/web/app/Controller.hpp index 7ea5839f..3fdf2d48 100644 --- a/test/oatpp/web/app/Controller.hpp +++ b/test/oatpp/web/app/Controller.hpp @@ -46,28 +46,35 @@ namespace oatpp { namespace test { namespace web { namespace app { namespace multipart = oatpp::web::mime::multipart; +#include OATPP_CODEGEN_BEGIN(ApiController) + class Controller : public oatpp::web::server::api::ApiController { private: static constexpr const char* TAG = "test::web::app::Controller"; public: Controller(const std::shared_ptr& objectMapper) : oatpp::web::server::api::ApiController(objectMapper) + , available(true) {} public: static std::shared_ptr createShared(const std::shared_ptr& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr)){ return std::make_shared(objectMapper); } - -#include OATPP_CODEGEN_BEGIN(ApiController) + + std::atomic available; ENDPOINT("GET", "/", root) { - //OATPP_LOGV(TAG, "GET '/'"); return createResponse(Status::CODE_200, "Hello World!!!"); } ENDPOINT("GET", "/availability", availability) { - return createResponse(Status::CODE_200, "Hello World!!!"); + //OATPP_LOGV(TAG, "GET '/availability'"); + if(available) { + return createResponse(Status::CODE_200, "Hello World!!!"); + } + OATPP_LOGE(TAG, "GET '/availability'. Service unavailable."); + return createResponse(Status::CODE_503, ""); } ADD_CORS(cors); @@ -254,11 +261,11 @@ public: return createResponse(Status::CODE_200, "OK"); } - -#include OATPP_CODEGEN_END(ApiController) }; +#include OATPP_CODEGEN_END(ApiController) + }}}} #endif /* oatpp_test_web_app_Controller_hpp */