From ba4d22e1afd247378dc510d3f67cd4ecd46f5ed0 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Fri, 22 Feb 2019 15:48:34 +0200 Subject: [PATCH 1/6] UnitTest::onRun return void --- src/oatpp-test/UnitTest.cpp | 37 ++++++------------- src/oatpp-test/UnitTest.hpp | 10 ++--- .../core/base/CommandLineArgumentsTest.cpp | 5 +-- .../core/base/CommandLineArgumentsTest.hpp | 2 +- test/oatpp/core/base/RegRuleTest.cpp | 3 +- test/oatpp/core/base/RegRuleTest.hpp | 2 +- .../core/base/collection/LinkedListTest.cpp | 5 +-- .../core/base/collection/LinkedListTest.hpp | 2 +- .../oatpp/core/base/memory/MemoryPoolTest.cpp | 4 +- .../oatpp/core/base/memory/MemoryPoolTest.hpp | 2 +- test/oatpp/core/base/memory/PerfTest.cpp | 5 +-- test/oatpp/core/base/memory/PerfTest.hpp | 2 +- .../oatpp/core/data/mapping/type/TypeTest.cpp | 3 +- .../oatpp/core/data/mapping/type/TypeTest.hpp | 2 +- .../oatpp/core/data/share/MemoryLabelTest.cpp | 10 ++--- .../oatpp/core/data/share/MemoryLabelTest.hpp | 2 +- test/oatpp/core/parser/CaretTest.cpp | 3 +- test/oatpp/core/parser/CaretTest.hpp | 2 +- test/oatpp/encoding/Base64Test.cpp | 5 +-- test/oatpp/encoding/Base64Test.hpp | 2 +- test/oatpp/encoding/UnicodeTest.cpp | 5 +-- test/oatpp/encoding/UnicodeTest.hpp | 2 +- test/oatpp/network/virtual_/InterfaceTest.cpp | 5 +-- test/oatpp/network/virtual_/InterfaceTest.hpp | 2 +- test/oatpp/network/virtual_/PipeTest.cpp | 5 +-- test/oatpp/network/virtual_/PipeTest.hpp | 2 +- .../parser/json/mapping/DTOMapperPerfTest.cpp | 4 +- .../parser/json/mapping/DTOMapperPerfTest.hpp | 2 +- .../parser/json/mapping/DTOMapperTest.cpp | 5 +-- .../parser/json/mapping/DTOMapperTest.hpp | 2 +- .../parser/json/mapping/DeserializerTest.cpp | 5 +-- .../parser/json/mapping/DeserializerTest.hpp | 2 +- test/oatpp/web/FullAsyncTest.cpp | 5 +-- test/oatpp/web/FullAsyncTest.hpp | 2 +- test/oatpp/web/FullTest.cpp | 5 +-- test/oatpp/web/FullTest.hpp | 2 +- 36 files changed, 63 insertions(+), 100 deletions(-) diff --git a/src/oatpp-test/UnitTest.cpp b/src/oatpp-test/UnitTest.cpp index 7a6577a9..3949f9ec 100644 --- a/src/oatpp-test/UnitTest.cpp +++ b/src/oatpp-test/UnitTest.cpp @@ -24,48 +24,36 @@ #include "UnitTest.hpp" -#include "oatpp/core/base/memory/MemoryPool.hpp" // delete -#include "oatpp/core/base/Controllable.hpp" // delete +#include "oatpp/core/base/memory/MemoryPool.hpp" #include namespace oatpp { namespace test { -v_int64 UnitTest::getTickCount(){ - std::chrono::microseconds ms = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch() - ); +void UnitTest::run(v_int32 times) { - return ms.count(); - -} - -bool UnitTest::run(v_int32 times) { - - OATPP_LOGD(TAG, "START..."); + OATPP_LOGD(TAG, "\033[1mSTART\033[0m..."); v_counter objectsCount = base::Environment::getObjectsCount(); v_counter objectsCreated = base::Environment::getObjectsCreated(); - v_int64 ticks = getTickCount(); - - bool result = true; + v_int64 ticks = base::Environment::getMicroTickCount(); for(v_int32 i = 0; i < times; i++){ - result = onRun(); + onRun(); } - v_int64 millis = getTickCount() - ticks; + v_int64 millis = base::Environment::getMicroTickCount() - ticks; v_counter leakingObjects = base::Environment::getObjectsCount() - objectsCount; v_counter objectsCreatedPerTest = base::Environment::getObjectsCreated() - objectsCreated; - if(leakingObjects == 0 && result == true){ - OATPP_LOGD(TAG, "FINISHED - success"); - OATPP_LOGD(TAG, "%d(micro), %d(objs)\n", millis, objectsCreatedPerTest); + if(leakingObjects == 0){ + OATPP_LOGD(TAG, "\033[1mFINISHED\033[0m - \033[1;32msuccess!\033[0m"); + OATPP_LOGD(TAG, "\033[33m%d(micro), %d(objs)\033[0m\n", millis, objectsCreatedPerTest); }else{ - result = false; - OATPP_LOGD(TAG, "FINISHED - failed, leakingObjects = %d", leakingObjects); + + OATPP_LOGD(TAG, "\033[1mFINISHED\033[0m - \033[1;31mfailed\033[0m, leakingObjects = %d", leakingObjects); auto POOLS = oatpp::base::memory::MemoryPool::POOLS; auto it = POOLS.begin(); @@ -76,9 +64,6 @@ bool UnitTest::run(v_int32 times) { } } - OATPP_ASSERT(result); - return result; - } }} diff --git a/src/oatpp-test/UnitTest.hpp b/src/oatpp-test/UnitTest.hpp index ed69a48f..d98ef180 100644 --- a/src/oatpp-test/UnitTest.hpp +++ b/src/oatpp-test/UnitTest.hpp @@ -41,15 +41,13 @@ public: virtual ~UnitTest(){ } - v_int64 getTickCount(); - - bool run(v_int32 times); + void run(v_int32 times); - bool run(){ - return run(1); + void run(){ + run(1); } - virtual bool onRun() = 0; + virtual void onRun() = 0; template static void runTest(v_int32 times){ diff --git a/test/oatpp/core/base/CommandLineArgumentsTest.cpp b/test/oatpp/core/base/CommandLineArgumentsTest.cpp index 9dd7b547..64bf00a6 100644 --- a/test/oatpp/core/base/CommandLineArgumentsTest.cpp +++ b/test/oatpp/core/base/CommandLineArgumentsTest.cpp @@ -30,7 +30,7 @@ namespace oatpp { namespace test { namespace base { -bool CommandLineArgumentsTest::onRun() { +void CommandLineArgumentsTest::onRun() { /* -k -c 100 -n 500000 "http://127.0.0.1:8000/" */ int argc = 6; @@ -57,8 +57,7 @@ bool CommandLineArgumentsTest::onRun() { OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("-n"), "500000") == 0); OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("--non-existing", "default"), "default") == 0); - - return true; + } }}} diff --git a/test/oatpp/core/base/CommandLineArgumentsTest.hpp b/test/oatpp/core/base/CommandLineArgumentsTest.hpp index 223617a0..4c080ef1 100644 --- a/test/oatpp/core/base/CommandLineArgumentsTest.hpp +++ b/test/oatpp/core/base/CommandLineArgumentsTest.hpp @@ -36,7 +36,7 @@ class CommandLineArgumentsTest : public UnitTest{ public: CommandLineArgumentsTest():UnitTest("TEST[base::CommandLineArgumentsTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/base/RegRuleTest.cpp b/test/oatpp/core/base/RegRuleTest.cpp index c917a28a..3293300a 100644 --- a/test/oatpp/core/base/RegRuleTest.cpp +++ b/test/oatpp/core/base/RegRuleTest.cpp @@ -82,7 +82,7 @@ namespace { } -bool RegRuleTest::onRun() { +void RegRuleTest::onRun() { { String reg1(""); @@ -153,7 +153,6 @@ bool RegRuleTest::onRun() { OATPP_ASSERT(map.find("str_2")->second == "val_2"); OATPP_ASSERT(map.find("str_3")->second == "val_3"); - return true; } }}} diff --git a/test/oatpp/core/base/RegRuleTest.hpp b/test/oatpp/core/base/RegRuleTest.hpp index 7875884c..918f67fe 100644 --- a/test/oatpp/core/base/RegRuleTest.hpp +++ b/test/oatpp/core/base/RegRuleTest.hpp @@ -33,7 +33,7 @@ class RegRuleTest : public UnitTest{ public: RegRuleTest():UnitTest("TEST[base::RegRuleTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/base/collection/LinkedListTest.cpp b/test/oatpp/core/base/collection/LinkedListTest.cpp index 6baa06c3..e954f2db 100644 --- a/test/oatpp/core/base/collection/LinkedListTest.cpp +++ b/test/oatpp/core/base/collection/LinkedListTest.cpp @@ -85,7 +85,7 @@ void testStdListPerformance(v_int32 iterationsCount){ } -bool LinkedListTest::onRun() { +void LinkedListTest::onRun() { v_int32 iterationsCount = 100000; @@ -103,8 +103,7 @@ bool LinkedListTest::onRun() { testStdListPerformance(iterationsCount); } } - - return true; + } }}} diff --git a/test/oatpp/core/base/collection/LinkedListTest.hpp b/test/oatpp/core/base/collection/LinkedListTest.hpp index 954a3a4d..769ebd91 100644 --- a/test/oatpp/core/base/collection/LinkedListTest.hpp +++ b/test/oatpp/core/base/collection/LinkedListTest.hpp @@ -33,7 +33,7 @@ class LinkedListTest : public UnitTest{ public: LinkedListTest():UnitTest("TEST[oatpp::collection::LinkedListTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/base/memory/MemoryPoolTest.cpp b/test/oatpp/core/base/memory/MemoryPoolTest.cpp index 6ae4eb8c..631e3489 100644 --- a/test/oatpp/core/base/memory/MemoryPoolTest.cpp +++ b/test/oatpp/core/base/memory/MemoryPoolTest.cpp @@ -151,7 +151,7 @@ void testPool(v_int32 objectsNumber, v_int32 garbageNumber, v_int32 chunkSize){ } -bool MemoryPoolTest::onRun() { +void MemoryPoolTest::onRun() { const v_int32 objectsNumber = 1000000; const v_int32 garbageNumber = 1000000; @@ -190,8 +190,6 @@ bool MemoryPoolTest::onRun() { } } - return true; - } }}} diff --git a/test/oatpp/core/base/memory/MemoryPoolTest.hpp b/test/oatpp/core/base/memory/MemoryPoolTest.hpp index eb6e6cb6..99738ecf 100644 --- a/test/oatpp/core/base/memory/MemoryPoolTest.hpp +++ b/test/oatpp/core/base/memory/MemoryPoolTest.hpp @@ -36,7 +36,7 @@ class MemoryPoolTest : public UnitTest{ public: MemoryPoolTest():UnitTest("TEST[base::memory::MemoryPoolTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/base/memory/PerfTest.cpp b/test/oatpp/core/base/memory/PerfTest.cpp index eedbb007..1af026d1 100644 --- a/test/oatpp/core/base/memory/PerfTest.cpp +++ b/test/oatpp/core/base/memory/PerfTest.cpp @@ -92,7 +92,7 @@ namespace { } -bool PerfTest::onRun() { +void PerfTest::onRun() { v_int32 iterations = 1; v_int32 threadCount = 100; @@ -122,8 +122,7 @@ bool PerfTest::onRun() { */ } - - return true; + } }}} diff --git a/test/oatpp/core/base/memory/PerfTest.hpp b/test/oatpp/core/base/memory/PerfTest.hpp index 28c2c24c..955cd6b5 100644 --- a/test/oatpp/core/base/memory/PerfTest.hpp +++ b/test/oatpp/core/base/memory/PerfTest.hpp @@ -33,7 +33,7 @@ class PerfTest : public UnitTest{ public: PerfTest():UnitTest("TEST[base::memory::PerfTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/data/mapping/type/TypeTest.cpp b/test/oatpp/core/data/mapping/type/TypeTest.cpp index 0990abad..8095f7e9 100644 --- a/test/oatpp/core/data/mapping/type/TypeTest.cpp +++ b/test/oatpp/core/data/mapping/type/TypeTest.cpp @@ -65,7 +65,7 @@ namespace { } -bool TypeTest::onRun() { +void TypeTest::onRun() { auto obj = TestDto::createShared(); @@ -117,7 +117,6 @@ bool TypeTest::onRun() { OATPP_LOGD(TAG, "type: '%s'", obj->obj1.valueType->name); OATPP_ASSERT(obj->obj1.valueType->name == oatpp::data::mapping::type::__class::AbstractObject::CLASS_NAME); - return true; } }}}}}} diff --git a/test/oatpp/core/data/mapping/type/TypeTest.hpp b/test/oatpp/core/data/mapping/type/TypeTest.hpp index 06f76119..60e5e0bc 100644 --- a/test/oatpp/core/data/mapping/type/TypeTest.hpp +++ b/test/oatpp/core/data/mapping/type/TypeTest.hpp @@ -33,7 +33,7 @@ class TypeTest : public UnitTest{ public: TypeTest():UnitTest("TEST[core::data::mapping::type::TypeTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/data/share/MemoryLabelTest.cpp b/test/oatpp/core/data/share/MemoryLabelTest.cpp index 905f3093..2df7008d 100644 --- a/test/oatpp/core/data/share/MemoryLabelTest.cpp +++ b/test/oatpp/core/data/share/MemoryLabelTest.cpp @@ -33,7 +33,7 @@ namespace oatpp { namespace test { namespace core { namespace data { namespace share { -bool MemoryLabelTest::onRun() { +void MemoryLabelTest::onRun() { oatpp::String sharedData = "big text goes here"; oatpp::String key1 = "key1"; @@ -140,9 +140,7 @@ bool MemoryLabelTest::onRun() { OATPP_ASSERT(headers["header7"].equals("value7", 6)); OATPP_ASSERT(headers["header8"].equals("value8", 6)); OATPP_ASSERT(headers["header9"].equals("value9", 6)); - - - /* + OATPP_ASSERT(headers["header0"].equals("value0")); OATPP_ASSERT(headers["header1"].equals("value1")); OATPP_ASSERT(headers["header2"].equals("value2")); @@ -153,15 +151,13 @@ bool MemoryLabelTest::onRun() { OATPP_ASSERT(headers["header7"].equals("value7")); OATPP_ASSERT(headers["header8"].equals("value8")); OATPP_ASSERT(headers["header9"].equals("value9")); - */ } } } - - return true; + } }}}}} diff --git a/test/oatpp/core/data/share/MemoryLabelTest.hpp b/test/oatpp/core/data/share/MemoryLabelTest.hpp index 7d8ebf2d..758e49ce 100644 --- a/test/oatpp/core/data/share/MemoryLabelTest.hpp +++ b/test/oatpp/core/data/share/MemoryLabelTest.hpp @@ -33,7 +33,7 @@ class MemoryLabelTest : public UnitTest{ public: MemoryLabelTest():UnitTest("TEST[core::data::share::MemoryLabelTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/core/parser/CaretTest.cpp b/test/oatpp/core/parser/CaretTest.cpp index 7900696b..0e4578bb 100644 --- a/test/oatpp/core/parser/CaretTest.cpp +++ b/test/oatpp/core/parser/CaretTest.cpp @@ -32,7 +32,7 @@ namespace oatpp { namespace test { namespace parser { typedef oatpp::parser::Caret Caret; } -bool CaretTest::onRun() { +void CaretTest::onRun() { { Caret caret(" \t\n\r\f \t\n\r\f \t\n\r\fhello!\t\n\r\f"); @@ -82,7 +82,6 @@ bool CaretTest::onRun() { OATPP_ASSERT(caret.getPosition() == caret.getDataSize()); } - return true; } }}} \ No newline at end of file diff --git a/test/oatpp/core/parser/CaretTest.hpp b/test/oatpp/core/parser/CaretTest.hpp index c98c0d8d..e02fdc13 100644 --- a/test/oatpp/core/parser/CaretTest.hpp +++ b/test/oatpp/core/parser/CaretTest.hpp @@ -33,7 +33,7 @@ class CaretTest : public UnitTest{ public: CaretTest():UnitTest("TEST[parser::CaretTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/encoding/Base64Test.cpp b/test/oatpp/encoding/Base64Test.cpp index c2632d9d..868a1586 100644 --- a/test/oatpp/encoding/Base64Test.cpp +++ b/test/oatpp/encoding/Base64Test.cpp @@ -28,7 +28,7 @@ namespace oatpp { namespace test { namespace encoding { -bool Base64Test::onRun() { +void Base64Test::onRun() { oatpp::String message = "oat++ web framework"; oatpp::String messageEncoded = "b2F0Kysgd2ViIGZyYW1ld29yaw=="; @@ -47,8 +47,7 @@ bool Base64Test::onRun() { oatpp::String decoded = oatpp::encoding::Base64::decode(encoded, oatpp::encoding::Base64::ALPHABET_BASE64_URL_SAFE_AUXILIARY_CHARS); OATPP_ASSERT(message->equals(decoded.get())); } - - return true; + } }}} diff --git a/test/oatpp/encoding/Base64Test.hpp b/test/oatpp/encoding/Base64Test.hpp index 92baed19..d3bfd6bf 100644 --- a/test/oatpp/encoding/Base64Test.hpp +++ b/test/oatpp/encoding/Base64Test.hpp @@ -32,7 +32,7 @@ namespace oatpp { namespace test { namespace encoding { class Base64Test : public UnitTest{ public: Base64Test():UnitTest("TEST[encoding::Base64Test]"){} - bool onRun() override; + void onRun() override; }; }}} diff --git a/test/oatpp/encoding/UnicodeTest.cpp b/test/oatpp/encoding/UnicodeTest.cpp index df4b9f1f..6a58ac84 100644 --- a/test/oatpp/encoding/UnicodeTest.cpp +++ b/test/oatpp/encoding/UnicodeTest.cpp @@ -58,7 +58,7 @@ void writeBinaryInt(v_int32 value){ // 38327 -bool UnicodeTest::onRun(){ +void UnicodeTest::onRun(){ v_char8 buff[128]; v_int32 cnt; @@ -130,8 +130,7 @@ bool UnicodeTest::onRun(){ check = oatpp::encoding::Unicode::utf16SurrogatePairToCode(high, low); OATPP_ASSERT(code == check); } - - return true; + } }}} diff --git a/test/oatpp/encoding/UnicodeTest.hpp b/test/oatpp/encoding/UnicodeTest.hpp index 5affd07c..2717e3d4 100644 --- a/test/oatpp/encoding/UnicodeTest.hpp +++ b/test/oatpp/encoding/UnicodeTest.hpp @@ -32,7 +32,7 @@ namespace oatpp { namespace test { namespace encoding { class UnicodeTest : public UnitTest{ public: UnicodeTest():UnitTest("TEST[encoding::UnicodeTest]"){} - bool onRun() override; + void onRun() override; }; }}} diff --git a/test/oatpp/network/virtual_/InterfaceTest.cpp b/test/oatpp/network/virtual_/InterfaceTest.cpp index e369b2c6..2a198b8b 100644 --- a/test/oatpp/network/virtual_/InterfaceTest.cpp +++ b/test/oatpp/network/virtual_/InterfaceTest.cpp @@ -131,7 +131,7 @@ namespace { } -bool InterfaceTest::onRun() { +void InterfaceTest::onRun() { oatpp::String dataSample = "1234567890-=][poiuytrewqasdfghjkl;'/.,mnbvcxzzxcvbnm,./';lkjhgfdsaqwertyuiop][=-0987654321"; @@ -154,8 +154,7 @@ bool InterfaceTest::onRun() { } server->join(); - - return true; + } }}}} diff --git a/test/oatpp/network/virtual_/InterfaceTest.hpp b/test/oatpp/network/virtual_/InterfaceTest.hpp index 5031227f..3f902a1a 100644 --- a/test/oatpp/network/virtual_/InterfaceTest.hpp +++ b/test/oatpp/network/virtual_/InterfaceTest.hpp @@ -32,7 +32,7 @@ namespace oatpp { namespace test { namespace network { namespace virtual_ { class InterfaceTest : public UnitTest { public: InterfaceTest():UnitTest("TEST[network::virtual_::InterfaceTest]"){} - bool onRun() override; + void onRun() override; }; }}}} diff --git a/test/oatpp/network/virtual_/PipeTest.cpp b/test/oatpp/network/virtual_/PipeTest.cpp index 3d818a6f..daef9c25 100644 --- a/test/oatpp/network/virtual_/PipeTest.cpp +++ b/test/oatpp/network/virtual_/PipeTest.cpp @@ -133,7 +133,7 @@ namespace { } -bool PipeTest::onRun() { +void PipeTest::onRun() { auto pipe = Pipe::createShared(); @@ -143,8 +143,7 @@ bool PipeTest::onRun() { runTransfer(pipe, chunkCount, true, false); runTransfer(pipe, chunkCount, false, true); runTransfer(pipe, chunkCount, true, true); - - return true; + } }}}} diff --git a/test/oatpp/network/virtual_/PipeTest.hpp b/test/oatpp/network/virtual_/PipeTest.hpp index 3ebc102e..8b5ccab3 100644 --- a/test/oatpp/network/virtual_/PipeTest.hpp +++ b/test/oatpp/network/virtual_/PipeTest.hpp @@ -32,7 +32,7 @@ namespace oatpp { namespace test { namespace network { namespace virtual_ { class PipeTest : public UnitTest { public: PipeTest():UnitTest("TEST[network::virtual_::PipeTest]"){} - bool onRun() override; + void onRun() override; }; }}}} diff --git a/test/oatpp/parser/json/mapping/DTOMapperPerfTest.cpp b/test/oatpp/parser/json/mapping/DTOMapperPerfTest.cpp index c3228513..ab05a999 100644 --- a/test/oatpp/parser/json/mapping/DTOMapperPerfTest.cpp +++ b/test/oatpp/parser/json/mapping/DTOMapperPerfTest.cpp @@ -65,7 +65,7 @@ typedef oatpp::parser::json::mapping::Deserializer Deserializer; } -bool DTOMapperPerfTest::onRun() { +void DTOMapperPerfTest::onRun() { v_int32 numIterations = 1000000; @@ -89,7 +89,7 @@ bool DTOMapperPerfTest::onRun() { mapper->readFromCaret(caret); } } - return true; + } }}}}} diff --git a/test/oatpp/parser/json/mapping/DTOMapperPerfTest.hpp b/test/oatpp/parser/json/mapping/DTOMapperPerfTest.hpp index 5bf8803e..399220bf 100644 --- a/test/oatpp/parser/json/mapping/DTOMapperPerfTest.hpp +++ b/test/oatpp/parser/json/mapping/DTOMapperPerfTest.hpp @@ -33,7 +33,7 @@ class DTOMapperPerfTest : public UnitTest{ public: DTOMapperPerfTest():UnitTest("TEST[parser::json::mapping::DTOMapperPerfTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/parser/json/mapping/DTOMapperTest.cpp b/test/oatpp/parser/json/mapping/DTOMapperTest.cpp index 566d4378..41ccb336 100644 --- a/test/oatpp/parser/json/mapping/DTOMapperTest.cpp +++ b/test/oatpp/parser/json/mapping/DTOMapperTest.cpp @@ -86,7 +86,7 @@ class Test : public DTO { } -bool DTOMapperTest::onRun(){ +void DTOMapperTest::onRun(){ auto mapper = oatpp::parser::json::mapping::ObjectMapper::createShared(); @@ -190,8 +190,7 @@ bool DTOMapperTest::onRun(){ result = mapper->writeToString(obj); OATPP_LOGD(TAG, "json='%s'", (const char*) result->getData()); - - return true; + } #include OATPP_CODEGEN_END(DTO) diff --git a/test/oatpp/parser/json/mapping/DTOMapperTest.hpp b/test/oatpp/parser/json/mapping/DTOMapperTest.hpp index bd270555..cd72508c 100644 --- a/test/oatpp/parser/json/mapping/DTOMapperTest.hpp +++ b/test/oatpp/parser/json/mapping/DTOMapperTest.hpp @@ -33,7 +33,7 @@ class DTOMapperTest : public UnitTest{ public: DTOMapperTest():UnitTest("TEST[parser::json::mapping::DTOMapperTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/parser/json/mapping/DeserializerTest.cpp b/test/oatpp/parser/json/mapping/DeserializerTest.cpp index 681b2d0d..2bb654c2 100644 --- a/test/oatpp/parser/json/mapping/DeserializerTest.cpp +++ b/test/oatpp/parser/json/mapping/DeserializerTest.cpp @@ -82,7 +82,7 @@ class Test4 : public DTO { } -bool DeserializerTest::onRun(){ +void DeserializerTest::onRun(){ auto mapper = oatpp::parser::json::mapping::ObjectMapper::createShared(); @@ -175,8 +175,7 @@ bool DeserializerTest::onRun(){ OATPP_ASSERT(obj4->list); OATPP_ASSERT(obj4->list->count() == 0); OATPP_ASSERT(obj4->map->count() == 0); - - return true; + } }}}}} diff --git a/test/oatpp/parser/json/mapping/DeserializerTest.hpp b/test/oatpp/parser/json/mapping/DeserializerTest.hpp index 0e7f03c4..b3d140c5 100644 --- a/test/oatpp/parser/json/mapping/DeserializerTest.hpp +++ b/test/oatpp/parser/json/mapping/DeserializerTest.hpp @@ -33,7 +33,7 @@ class DeserializerTest : public UnitTest{ public: DeserializerTest():UnitTest("TEST[parser::json::mapping::DeserializerTestTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/web/FullAsyncTest.cpp b/test/oatpp/web/FullAsyncTest.cpp index 929b0b0c..74352321 100644 --- a/test/oatpp/web/FullAsyncTest.cpp +++ b/test/oatpp/web/FullAsyncTest.cpp @@ -42,7 +42,7 @@ namespace oatpp { namespace test { namespace web { -bool FullAsyncTest::onRun() { +void FullAsyncTest::onRun() { auto interface = oatpp::network::virtual_::Interface::createShared("virtualhost"); @@ -136,8 +136,7 @@ bool FullAsyncTest::onRun() { serverThread.join(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - - return true; + } }}} diff --git a/test/oatpp/web/FullAsyncTest.hpp b/test/oatpp/web/FullAsyncTest.hpp index 368cb7eb..b220f5c3 100644 --- a/test/oatpp/web/FullAsyncTest.hpp +++ b/test/oatpp/web/FullAsyncTest.hpp @@ -33,7 +33,7 @@ class FullAsyncTest : public UnitTest { public: FullAsyncTest():UnitTest("TEST[web::FullAsyncTest]"){} - bool onRun() override; + void onRun() override; }; diff --git a/test/oatpp/web/FullTest.cpp b/test/oatpp/web/FullTest.cpp index fc8c8e59..882416bf 100644 --- a/test/oatpp/web/FullTest.cpp +++ b/test/oatpp/web/FullTest.cpp @@ -44,7 +44,7 @@ namespace oatpp { namespace test { namespace web { -bool FullTest::onRun() { +void FullTest::onRun() { auto interface = oatpp::network::virtual_::Interface::createShared("virtualhost"); @@ -131,8 +131,7 @@ bool FullTest::onRun() { clientThread.join(); serverThread.join(); - - return true; + } }}} diff --git a/test/oatpp/web/FullTest.hpp b/test/oatpp/web/FullTest.hpp index 7e7ccb39..b35c3a50 100644 --- a/test/oatpp/web/FullTest.hpp +++ b/test/oatpp/web/FullTest.hpp @@ -33,7 +33,7 @@ class FullTest : public UnitTest { public: FullTest():UnitTest("TEST[web::FullTest]"){} - bool onRun() override; + void onRun() override; }; From 1c4fb11296f210e54a484fb6a62281829e57ebc1 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Fri, 22 Feb 2019 19:18:59 +0200 Subject: [PATCH 2/6] ClientServerTestRunner draft --- src/CMakeLists.txt | 3 + src/oatpp-test/web/ClientServerTestRunner.cpp | 25 +++++ src/oatpp-test/web/ClientServerTestRunner.hpp | 92 +++++++++++++++++++ src/oatpp/network/ConnectionProvider.hpp | 6 ++ .../client/SimpleTCPConnectionProvider.hpp | 6 +- .../network/server/ConnectionHandler.hpp | 10 ++ .../server/SimpleTCPConnectionProvider.cpp | 8 +- .../server/SimpleTCPConnectionProvider.hpp | 2 + .../virtual_/client/ConnectionProvider.cpp | 6 +- .../virtual_/client/ConnectionProvider.hpp | 2 + .../virtual_/server/ConnectionProvider.cpp | 6 +- .../virtual_/server/ConnectionProvider.hpp | 4 +- .../web/server/AsyncHttpConnectionHandler.hpp | 2 +- .../web/server/HttpConnectionHandler.hpp | 4 + test/oatpp/web/FullAsyncTest.cpp | 2 +- 15 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 src/oatpp-test/web/ClientServerTestRunner.cpp create mode 100644 src/oatpp-test/web/ClientServerTestRunner.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b1be4c2d..a26da20d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -206,7 +206,10 @@ add_library(oatpp-test oatpp-test/Checker.hpp oatpp-test/UnitTest.cpp oatpp-test/UnitTest.hpp + oatpp-test/web/ClientServerTestRunner.cpp + oatpp-test/web/ClientServerTestRunner.hpp ) + set_target_properties(oatpp-test PROPERTIES CXX_STANDARD 11 CXX_EXTENSIONS OFF diff --git a/src/oatpp-test/web/ClientServerTestRunner.cpp b/src/oatpp-test/web/ClientServerTestRunner.cpp new file mode 100644 index 00000000..ff5fa638 --- /dev/null +++ b/src/oatpp-test/web/ClientServerTestRunner.cpp @@ -0,0 +1,25 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * 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 "ClientServerTestRunner.hpp" diff --git a/src/oatpp-test/web/ClientServerTestRunner.hpp b/src/oatpp-test/web/ClientServerTestRunner.hpp new file mode 100644 index 00000000..9ce4988e --- /dev/null +++ b/src/oatpp-test/web/ClientServerTestRunner.hpp @@ -0,0 +1,92 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * 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_web_ClientServerTestRunner_hpp +#define oatpp_test_web_ClientServerTestRunner_hpp + +#include "oatpp/web/server/api/ApiController.hpp" +#include "oatpp/web/server/HttpRouter.hpp" + +#include "oatpp/network/server/Server.hpp" +#include "oatpp/network/ConnectionProvider.hpp" + +#include "oatpp/core/macro/component.hpp" + +namespace oatpp { namespace test { namespace web { + +class ClientServerTestRunner { +public: + typedef oatpp::web::server::HttpRouter HttpRouter; + typedef oatpp::web::server::api::ApiController ApiController; +private: + OATPP_COMPONENT(std::shared_ptr, m_router); + OATPP_COMPONENT(std::shared_ptr, m_connectionProvider); + OATPP_COMPONENT(std::shared_ptr, m_connectionHandler); +public: + + std::shared_ptr getRouter() { + return m_router; + } + + void addController(const std::shared_ptr& controller) { + controller->addEndpointsToRouter(m_router); + } + + template + void run(const Lambda& lambda) { + + oatpp::network::server::Server server(m_connectionProvider, m_connectionHandler); + + OATPP_LOGD("Server", "Running on port %s...", m_connectionProvider->getProperty("port").toString()->c_str()); + + std::thread clientThread([this, &server, &lambda]{ + + lambda(); + + try { + server.stop(); + m_connectionHandler->stop(); + m_connectionProvider->close(); + } catch(std::runtime_error e) { + // DO NOTHING + } + + }); + + std::thread serverThread([&server]{ + server.run(); + }); + + clientThread.join(); + serverThread.join(); + + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + } + +}; + +}}} + +#endif //oatpp_test_web_ClientServerTestRunner_hpp diff --git a/src/oatpp/network/ConnectionProvider.hpp b/src/oatpp/network/ConnectionProvider.hpp index 7d1db0c5..83717ca7 100644 --- a/src/oatpp/network/ConnectionProvider.hpp +++ b/src/oatpp/network/ConnectionProvider.hpp @@ -55,9 +55,15 @@ protected: void setProperty(const oatpp::String& key, const oatpp::String& value); public: virtual ~ConnectionProvider() {} + virtual std::shared_ptr getConnection() = 0; virtual Action getConnectionAsync(oatpp::async::AbstractCoroutine* parentCoroutine, AsyncCallback callback) = 0; + + /** + * Should close all handles here. + */ + virtual void close() = 0; /** * Some optional properties that user might want to know. diff --git a/src/oatpp/network/client/SimpleTCPConnectionProvider.hpp b/src/oatpp/network/client/SimpleTCPConnectionProvider.hpp index bcf9bcba..bdc37964 100644 --- a/src/oatpp/network/client/SimpleTCPConnectionProvider.hpp +++ b/src/oatpp/network/client/SimpleTCPConnectionProvider.hpp @@ -43,7 +43,11 @@ public: static std::shared_ptr createShared(const oatpp::String& host, v_word16 port){ return std::make_shared(host, port); } - + + void close() override { + // DO NOTHING + } + std::shared_ptr getConnection() override; Action getConnectionAsync(oatpp::async::AbstractCoroutine* parentCoroutine, AsyncCallback callback) override; diff --git a/src/oatpp/network/server/ConnectionHandler.hpp b/src/oatpp/network/server/ConnectionHandler.hpp index 2be641ed..769f1a63 100644 --- a/src/oatpp/network/server/ConnectionHandler.hpp +++ b/src/oatpp/network/server/ConnectionHandler.hpp @@ -32,7 +32,17 @@ namespace oatpp { namespace network { namespace server { class ConnectionHandler { public: + + /** + * Handle provided connection here + * @param connection + */ virtual void handleConnection(const std::shared_ptr& connection) = 0; + + /** + * Stop all threads here + */ + virtual void stop() = 0; }; }}} diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp index e5dcebba..404e05ae 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp @@ -47,7 +47,13 @@ SimpleTCPConnectionProvider::SimpleTCPConnectionProvider(v_word16 port, bool non } SimpleTCPConnectionProvider::~SimpleTCPConnectionProvider() { - ::close(m_serverHandle); + close(); +} + +void SimpleTCPConnectionProvider::close() { + if(m_serverHandle != 0) { + ::close(m_serverHandle); + } } oatpp::data::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){ diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp index cd8af4e9..90ff6ade 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp @@ -48,6 +48,8 @@ public: } ~SimpleTCPConnectionProvider(); + + void close() override; std::shared_ptr getConnection() override; diff --git a/src/oatpp/network/virtual_/client/ConnectionProvider.cpp b/src/oatpp/network/virtual_/client/ConnectionProvider.cpp index ad032324..f47537c0 100644 --- a/src/oatpp/network/virtual_/client/ConnectionProvider.cpp +++ b/src/oatpp/network/virtual_/client/ConnectionProvider.cpp @@ -25,7 +25,11 @@ #include "ConnectionProvider.hpp" namespace oatpp { namespace network { namespace virtual_ { namespace client { - + +void ConnectionProvider::close() { + +} + std::shared_ptr ConnectionProvider::getConnection() { auto submission = m_interface->connect(); auto socket = submission->getSocket(); diff --git a/src/oatpp/network/virtual_/client/ConnectionProvider.hpp b/src/oatpp/network/virtual_/client/ConnectionProvider.hpp index b939bdbb..90525c2f 100644 --- a/src/oatpp/network/virtual_/client/ConnectionProvider.hpp +++ b/src/oatpp/network/virtual_/client/ConnectionProvider.hpp @@ -58,6 +58,8 @@ public: m_maxAvailableToRead = maxToRead; m_maxAvailableToWrite = maxToWrite; } + + void close() override; std::shared_ptr getConnection() override; diff --git a/src/oatpp/network/virtual_/server/ConnectionProvider.cpp b/src/oatpp/network/virtual_/server/ConnectionProvider.cpp index 51992540..10ca1cee 100644 --- a/src/oatpp/network/virtual_/server/ConnectionProvider.cpp +++ b/src/oatpp/network/virtual_/server/ConnectionProvider.cpp @@ -25,7 +25,11 @@ #include "ConnectionProvider.hpp" namespace oatpp { namespace network { namespace virtual_ { namespace server { - + +void ConnectionProvider::close() { + // +} + std::shared_ptr ConnectionProvider::getConnection() { auto socket = m_interface->accept(); socket->setNonBlocking(m_nonBlocking); diff --git a/src/oatpp/network/virtual_/server/ConnectionProvider.hpp b/src/oatpp/network/virtual_/server/ConnectionProvider.hpp index 211de7dd..99fef00b 100644 --- a/src/oatpp/network/virtual_/server/ConnectionProvider.hpp +++ b/src/oatpp/network/virtual_/server/ConnectionProvider.hpp @@ -51,7 +51,7 @@ public: static std::shared_ptr createShared(const std::shared_ptr& interface, bool nonBlocking = false) { return std::make_shared(interface, nonBlocking); } - + /** * this one used for testing purposes only * set to -1 in order to ignore this value @@ -60,6 +60,8 @@ public: m_maxAvailableToRead = maxToRead; m_maxAvailableToWrite = maxToWrite; } + + void close() override; std::shared_ptr getConnection() override; diff --git a/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp b/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp index 6fc655cc..1cfa9de7 100644 --- a/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp +++ b/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp @@ -94,7 +94,7 @@ public: void handleConnection(const std::shared_ptr& connection) override; - void stop() { + void stop() override { m_executor->stop(); } diff --git a/src/oatpp/web/server/HttpConnectionHandler.hpp b/src/oatpp/web/server/HttpConnectionHandler.hpp index b44d5cb3..b2347b82 100644 --- a/src/oatpp/web/server/HttpConnectionHandler.hpp +++ b/src/oatpp/web/server/HttpConnectionHandler.hpp @@ -107,6 +107,10 @@ public: } void handleConnection(const std::shared_ptr& connection) override; + + void stop() override { + // DO NOTHING + } }; diff --git a/test/oatpp/web/FullAsyncTest.cpp b/test/oatpp/web/FullAsyncTest.cpp index 74352321..5f219de3 100644 --- a/test/oatpp/web/FullAsyncTest.cpp +++ b/test/oatpp/web/FullAsyncTest.cpp @@ -121,7 +121,7 @@ void FullAsyncTest::onRun() { try { connectionHandler->stop(); server->stop(); - client->getRoot(); // wake blocking server accept + client->getConnection(); // wake blocking server accept } catch(std::runtime_error e) { // DO NOTHING } From f7ce8ea873d0d905b8fe999988be568f09bdd457 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Sat, 23 Feb 2019 02:43:15 +0200 Subject: [PATCH 3/6] better test framework. + ClientServerTestRunner --- src/oatpp-test/web/ClientServerTestRunner.hpp | 4 + src/oatpp/core/base/Environment.hpp | 2 +- .../client/SimpleTCPConnectionProvider.cpp | 16 +- .../server/SimpleTCPConnectionProvider.cpp | 12 +- .../server/SimpleTCPConnectionProvider.hpp | 2 +- src/oatpp/network/virtual_/Interface.cpp | 11 +- src/oatpp/network/virtual_/Interface.hpp | 16 +- .../virtual_/server/ConnectionProvider.cpp | 11 +- .../virtual_/server/ConnectionProvider.hpp | 4 +- test/oatpp/AllTestsMain.cpp | 1 + test/oatpp/web/FullAsyncTest.cpp | 138 ++++++++++------- test/oatpp/web/FullTest.cpp | 143 +++++++++++------- test/oatpp/web/app/Controller.hpp | 12 +- test/oatpp/web/app/ControllerAsync.hpp | 44 +++--- 14 files changed, 258 insertions(+), 158 deletions(-) diff --git a/src/oatpp-test/web/ClientServerTestRunner.hpp b/src/oatpp-test/web/ClientServerTestRunner.hpp index 9ce4988e..9c2f9e71 100644 --- a/src/oatpp-test/web/ClientServerTestRunner.hpp +++ b/src/oatpp-test/web/ClientServerTestRunner.hpp @@ -33,6 +33,8 @@ #include "oatpp/core/macro/component.hpp" +#include + namespace oatpp { namespace test { namespace web { class ClientServerTestRunner { @@ -40,6 +42,7 @@ public: typedef oatpp::web::server::HttpRouter HttpRouter; typedef oatpp::web::server::api::ApiController ApiController; private: + std::list> m_controllers; OATPP_COMPONENT(std::shared_ptr, m_router); OATPP_COMPONENT(std::shared_ptr, m_connectionProvider); OATPP_COMPONENT(std::shared_ptr, m_connectionHandler); @@ -51,6 +54,7 @@ public: void addController(const std::shared_ptr& controller) { controller->addEndpointsToRouter(m_router); + m_controllers.push_back(controller); } template diff --git a/src/oatpp/core/base/Environment.hpp b/src/oatpp/core/base/Environment.hpp index e0e4b851..04ed267e 100644 --- a/src/oatpp/core/base/Environment.hpp +++ b/src/oatpp/core/base/Environment.hpp @@ -156,7 +156,7 @@ public: #define OATPP_ASSERT(EXP) \ if(!(EXP)) { \ - OATPP_LOGE("ASSERT[FAILED]", #EXP); \ + OATPP_LOGE("\033[1mASSERT\033[0m[\033[1;31mFAILED\033[0m]", #EXP); \ exit(EXIT_FAILURE); \ } diff --git a/src/oatpp/network/client/SimpleTCPConnectionProvider.cpp b/src/oatpp/network/client/SimpleTCPConnectionProvider.cpp index 4442b264..32c9aa3a 100644 --- a/src/oatpp/network/client/SimpleTCPConnectionProvider.cpp +++ b/src/oatpp/network/client/SimpleTCPConnectionProvider.cpp @@ -50,7 +50,7 @@ std::shared_ptr SimpleTCPConnectionProvider::getC struct sockaddr_in client; if ((host == NULL) || (host->h_addr == NULL)) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Error retrieving DNS information."); + OATPP_LOGD("[oatpp::network::client::SimpleTCPConnectionProvider::getConnection()]", "Error. Can't retrieve DNS information."); return nullptr; } @@ -62,7 +62,7 @@ std::shared_ptr SimpleTCPConnectionProvider::getC oatpp::data::v_io_handle clientHandle = socket(AF_INET, SOCK_STREAM, 0); if (clientHandle < 0) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Error creating socket."); + OATPP_LOGD("[oatpp::network::client::SimpleTCPConnectionProvider::getConnection()]", "Error. Can't create socket."); return nullptr; } @@ -70,13 +70,13 @@ std::shared_ptr SimpleTCPConnectionProvider::getC int yes = 1; v_int32 ret = setsockopt(clientHandle, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(int)); if(ret < 0) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Warning failed to set %s for socket", "SO_NOSIGPIPE"); + OATPP_LOGD("[oatpp::network::client::SimpleTCPConnectionProvider::getConnection()]", "Warning. Failed to set %s for socket", "SO_NOSIGPIPE"); } #endif if (connect(clientHandle, (struct sockaddr *)&client, sizeof(client)) != 0 ) { ::close(clientHandle); - OATPP_LOGD("SimpleTCPConnectionProvider", "Could not connect"); + OATPP_LOGD("[oatpp::network::client::SimpleTCPConnectionProvider::getConnection()]", "Error. Could not connect."); return nullptr; } @@ -105,7 +105,7 @@ oatpp::async::Action SimpleTCPConnectionProvider::getConnectionAsync(oatpp::asyn struct hostent* host = gethostbyname((const char*) m_host->getData()); if ((host == NULL) || (host->h_addr == NULL)) { - return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Error retrieving DNS information."); + return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Error. Can't retrieve DNS information."); } bzero(&m_client, sizeof(m_client)); @@ -116,7 +116,7 @@ oatpp::async::Action SimpleTCPConnectionProvider::getConnectionAsync(oatpp::asyn m_clientHandle = socket(AF_INET, SOCK_STREAM, 0); if (m_clientHandle < 0) { - return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Error creating socket."); + return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Error. Can't create socket."); } fcntl(m_clientHandle, F_SETFL, O_NONBLOCK); @@ -125,7 +125,7 @@ oatpp::async::Action SimpleTCPConnectionProvider::getConnectionAsync(oatpp::asyn int yes = 1; v_int32 ret = setsockopt(m_clientHandle, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(int)); if(ret < 0) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Warning failed to set %s for socket", "SO_NOSIGPIPE"); + OATPP_LOGD("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]", "Warning. Failed to set %s for socket", "SO_NOSIGPIPE"); } #endif @@ -145,7 +145,7 @@ oatpp::async::Action SimpleTCPConnectionProvider::getConnectionAsync(oatpp::asyn return repeat(); } ::close(m_clientHandle); - return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Can't connect"); + return error("[oatpp::network::client::SimpleTCPConnectionProvider::getConnectionAsync()]: Error. Can't connect."); } }; diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp index 404e05ae..f4bb5097 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp @@ -76,14 +76,14 @@ oatpp::data::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){ ret = setsockopt(serverHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if(ret < 0) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Warning failed to set %s for accepting socket", "SO_REUSEADDR"); + OATPP_LOGD("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]", "Warning. Failed to set %s for accepting socket", "SO_REUSEADDR"); } ret = bind(serverHandle, (struct sockaddr *)&addr, sizeof(addr)); if(ret != 0) { ::close(serverHandle); - throw std::runtime_error("Can't bind to address"); + throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]: Error. Can't bind to address."); return -1 ; } @@ -100,9 +100,7 @@ oatpp::data::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){ } std::shared_ptr SimpleTCPConnectionProvider::getConnection(){ - - //oatpp::test::PerformanceChecker checker("Accept Checker"); - + oatpp::data::v_io_handle handle = accept(m_serverHandle, nullptr, nullptr); if (handle < 0) { @@ -110,7 +108,7 @@ std::shared_ptr SimpleTCPConnectionProvider::getC if(error == EAGAIN || error == EWOULDBLOCK){ return nullptr; } else { - OATPP_LOGD("Server", "Error: %d", error); + OATPP_LOGD("[oatpp::network::server::SimpleTCPConnectionProvider::getConnection()]", "Error. %d", error); return nullptr; } } @@ -119,7 +117,7 @@ std::shared_ptr SimpleTCPConnectionProvider::getC int yes = 1; v_int32 ret = setsockopt(handle, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(int)); if(ret < 0) { - OATPP_LOGD("SimpleTCPConnectionProvider", "Warning failed to set %s for socket", "SO_NOSIGPIPE"); + OATPP_LOGD("[oatpp::network::server::SimpleTCPConnectionProvider::getConnection()]", "Warning. Failed to set %s for socket", "SO_NOSIGPIPE"); } #endif diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp index 90ff6ade..4820fc73 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp @@ -63,7 +63,7 @@ public: * * It may be implemented later */ - throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::getConnectionAsync()] not implemented."); + throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::getConnectionAsync()]: Error. Not implemented."); } v_word16 getPort(){ diff --git a/src/oatpp/network/virtual_/Interface.cpp b/src/oatpp/network/virtual_/Interface.cpp index c1268658..4c0fb03f 100644 --- a/src/oatpp/network/virtual_/Interface.cpp +++ b/src/oatpp/network/virtual_/Interface.cpp @@ -89,11 +89,14 @@ std::shared_ptr Interface::connectNonBlocking() return submission; } -std::shared_ptr Interface::accept() { +std::shared_ptr Interface::accept(const bool& waitingHandle) { std::unique_lock lock(m_mutex); - while (m_submissions.getFirstNode() == nullptr) { + while (waitingHandle && m_submissions.getFirstNode() == nullptr) { m_condition.wait(lock); } + if(!waitingHandle) { + return nullptr; + } return acceptSubmission(m_submissions.popFront()); } @@ -104,5 +107,9 @@ std::shared_ptr Interface::acceptNonBlocking() { } return nullptr; } + +void Interface::notifyAcceptors() { + m_condition.notify_all(); +} }}} diff --git a/src/oatpp/network/virtual_/Interface.hpp b/src/oatpp/network/virtual_/Interface.hpp index ac2e96f8..a8197108 100644 --- a/src/oatpp/network/virtual_/Interface.hpp +++ b/src/oatpp/network/virtual_/Interface.hpp @@ -70,9 +70,21 @@ public: std::shared_ptr connect(); std::shared_ptr connectNonBlocking(); - - std::shared_ptr accept(); + + /** + * + * @param waitingHandle + * @return + */ + std::shared_ptr accept(const bool& waitingHandle = true); + std::shared_ptr acceptNonBlocking(); + + /** + * Notify all threads that are waiting on accept(). + * Those threads that have waitingHandle changed to false will be unblocked. + */ + void notifyAcceptors(); oatpp::String getName() { return m_name; diff --git a/src/oatpp/network/virtual_/server/ConnectionProvider.cpp b/src/oatpp/network/virtual_/server/ConnectionProvider.cpp index 10ca1cee..0ade259d 100644 --- a/src/oatpp/network/virtual_/server/ConnectionProvider.cpp +++ b/src/oatpp/network/virtual_/server/ConnectionProvider.cpp @@ -27,13 +27,16 @@ namespace oatpp { namespace network { namespace virtual_ { namespace server { void ConnectionProvider::close() { - // + m_open = false; + m_interface->notifyAcceptors(); } std::shared_ptr ConnectionProvider::getConnection() { - auto socket = m_interface->accept(); - socket->setNonBlocking(m_nonBlocking); - socket->setMaxAvailableToReadWrtie(m_maxAvailableToRead, m_maxAvailableToWrite); + auto socket = m_interface->accept(m_open); + if(socket) { + socket->setNonBlocking(m_nonBlocking); + socket->setMaxAvailableToReadWrtie(m_maxAvailableToRead, m_maxAvailableToWrite); + } return socket; } diff --git a/src/oatpp/network/virtual_/server/ConnectionProvider.hpp b/src/oatpp/network/virtual_/server/ConnectionProvider.hpp index 99fef00b..551bbdae 100644 --- a/src/oatpp/network/virtual_/server/ConnectionProvider.hpp +++ b/src/oatpp/network/virtual_/server/ConnectionProvider.hpp @@ -34,6 +34,7 @@ class ConnectionProvider : public oatpp::network::ServerConnectionProvider { private: std::shared_ptr m_interface; bool m_nonBlocking; + bool m_open; data::v_io_size m_maxAvailableToRead; data::v_io_size m_maxAvailableToWrite; public: @@ -41,6 +42,7 @@ public: ConnectionProvider(const std::shared_ptr& interface, bool nonBlocking = false) : m_interface(interface) , m_nonBlocking(nonBlocking) + , m_open(true) , m_maxAvailableToRead(-1) , m_maxAvailableToWrite(-1) { @@ -75,7 +77,7 @@ public: * * It may be implemented later */ - throw std::runtime_error("[oatpp::network::virtual_::server::ConnectionProvider::getConnectionAsync()] not implemented."); + throw std::runtime_error("[oatpp::network::virtual_::server::ConnectionProvider::getConnectionAsync()]: Error. Not implemented."); } }; diff --git a/test/oatpp/AllTestsMain.cpp b/test/oatpp/AllTestsMain.cpp index f6aae2d8..005c9a9d 100644 --- a/test/oatpp/AllTestsMain.cpp +++ b/test/oatpp/AllTestsMain.cpp @@ -65,6 +65,7 @@ void runTests() { OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest); OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest); + OATPP_RUN_TEST(oatpp::test::web::FullTest); OATPP_RUN_TEST(oatpp::test::web::FullAsyncTest); diff --git a/test/oatpp/web/FullAsyncTest.cpp b/test/oatpp/web/FullAsyncTest.cpp index 5f219de3..1b8b05a3 100644 --- a/test/oatpp/web/FullAsyncTest.cpp +++ b/test/oatpp/web/FullAsyncTest.cpp @@ -35,59 +35,108 @@ #include "oatpp/parser/json/mapping/ObjectMapper.hpp" +#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp" +#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp" + #include "oatpp/network/virtual_/client/ConnectionProvider.hpp" #include "oatpp/network/virtual_/server/ConnectionProvider.hpp" #include "oatpp/network/virtual_/Interface.hpp" -#include "oatpp/network/server/Server.hpp" + +#include "oatpp/core/macro/component.hpp" + +#include "oatpp-test/web/ClientServerTestRunner.hpp" namespace oatpp { namespace test { namespace web { + +namespace { + +//#define OATPP_TEST_USE_REAL_PORT + +class TestComponent { +public: + + OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { + return oatpp::network::virtual_::Interface::createShared("virtualhost"); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { +#ifdef OATPP_TEST_USE_REAL_PORT + return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000, true /* nonBlocking */); +#else + OATPP_COMPONENT(std::shared_ptr, interface); + auto provider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface, true /* nonBlocking */); + //provider->setSocketMaxAvailableToReadWrtie(123, 11); + provider->setSocketMaxAvailableToReadWrtie(-1, -1); + return provider; +#endif + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, httpRouter)([] { + return oatpp::web::server::HttpRouter::createShared(); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionHandler)([] { + OATPP_COMPONENT(std::shared_ptr, router); + return oatpp::web::server::AsyncHttpConnectionHandler::createShared(router); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, objectMapper)([] { + return oatpp::parser::json::mapping::ObjectMapper::createShared(); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, clientConnectionProvider)([this] { +#ifdef OATPP_TEST_USE_REAL_PORT + return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", 8000); +#else + OATPP_COMPONENT(std::shared_ptr, interface); + auto provider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); + //provider->setSocketMaxAvailableToReadWrtie(12421, 21312); + provider->setSocketMaxAvailableToReadWrtie(-1, -1); + return provider; +#endif + }()); + +}; + +} void FullAsyncTest::onRun() { - - auto interface = oatpp::network::virtual_::Interface::createShared("virtualhost"); - - auto serverConnectionProvider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface, true); - auto clientConnectionProvider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); - - serverConnectionProvider->setSocketMaxAvailableToReadWrtie(123, 11); - clientConnectionProvider->setSocketMaxAvailableToReadWrtie(12421, 21312); - //serverConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1); - //clientConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1); - - auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(); - - auto router = oatpp::web::server::HttpRouter::createShared(); - auto connectionHandler = oatpp::web::server::AsyncHttpConnectionHandler::createShared(router); - - auto controller = app::ControllerAsync::createShared(objectMapper); - controller->addEndpointsToRouter(router); - - auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider); - - auto client = app::Client::createShared(requestExecutor, objectMapper); - - auto server = oatpp::network::server::Server::createShared(serverConnectionProvider, connectionHandler); - - std::thread clientThread([client, server, connectionHandler, objectMapper]{ - - for(v_int32 i = 0; i < 1000; i ++) { + TestComponent component; + + oatpp::test::web::ClientServerTestRunner runner; + + runner.addController(app::ControllerAsync::createShared()); + + runner.run([] { + + OATPP_COMPONENT(std::shared_ptr, clientConnectionProvider); + OATPP_COMPONENT(std::shared_ptr, objectMapper); + + auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider); + auto client = app::Client::createShared(requestExecutor, objectMapper); + + auto connection = client->getConnection(); + + v_int32 iterationsStep = 1000; + + for(v_int32 i = 0; i < iterationsStep * 10; i ++) { { // test simple GET - auto response = client->getRoot(); + auto response = client->getRoot(connection); auto value = response->readBodyToString(); OATPP_ASSERT(value == "Hello World Async!!!"); } { // test GET with path parameter - auto response = client->getWithParams("my_test_param-Async"); + auto response = client->getWithParams("my_test_param-Async", connection); auto dto = response->readBodyToDto(objectMapper); OATPP_ASSERT(dto); OATPP_ASSERT(dto->testValue == "my_test_param-Async"); } { // test GET with header parameter - auto response = client->getWithHeaders("my_test_header-Async"); + auto response = client->getWithHeaders("my_test_header-Async", connection); //auto str = response->readBodyToString(); //OATPP_LOGE("AAA", "code=%d, str='%s'", response->statusCode, str->c_str()); auto dto = response->readBodyToDto(objectMapper); @@ -96,7 +145,7 @@ void FullAsyncTest::onRun() { } { // test POST with body - auto response = client->postBody("my_test_body-Async"); + auto response = client->postBody("my_test_body-Async", connection); auto dto = response->readBodyToDto(objectMapper); OATPP_ASSERT(dto); OATPP_ASSERT(dto->testValue == "my_test_body-Async"); @@ -108,34 +157,21 @@ void FullAsyncTest::onRun() { stream.write("0123456789", 10); } auto data = stream.toString(); - auto response = client->echoBody(data); + auto response = client->echoBody(data, connection); auto returnedData = response->readBodyToString(); OATPP_ASSERT(returnedData); OATPP_ASSERT(returnedData == data); } + + if((i + 1) % iterationsStep == 0) { + OATPP_LOGD("i", "%d", i + 1); + } } - try { - connectionHandler->stop(); - server->stop(); - client->getConnection(); // wake blocking server accept - } catch(std::runtime_error e) { - // DO NOTHING - } - }); - - std::thread serverThread([server]{ - server->run(); - }); - - clientThread.join(); - serverThread.join(); - - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } diff --git a/test/oatpp/web/FullTest.cpp b/test/oatpp/web/FullTest.cpp index 882416bf..33f252ab 100644 --- a/test/oatpp/web/FullTest.cpp +++ b/test/oatpp/web/FullTest.cpp @@ -26,7 +26,6 @@ #include "oatpp/web/app/Client.hpp" -#include "oatpp/web/app/ControllerAsync.hpp" #include "oatpp/web/app/Controller.hpp" #include "oatpp/web/client/HttpRequestExecutor.hpp" @@ -36,67 +35,115 @@ #include "oatpp/parser/json/mapping/ObjectMapper.hpp" +#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp" +#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp" + #include "oatpp/network/virtual_/client/ConnectionProvider.hpp" #include "oatpp/network/virtual_/server/ConnectionProvider.hpp" #include "oatpp/network/virtual_/Interface.hpp" -#include "oatpp/network/server/Server.hpp" +#include "oatpp/core/macro/component.hpp" + +#include "oatpp-test/web/ClientServerTestRunner.hpp" namespace oatpp { namespace test { namespace web { + +namespace { + +//#define OATPP_TEST_USE_REAL_PORT + +class TestComponent { +public: + + OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { + return oatpp::network::virtual_::Interface::createShared("virtualhost"); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { +#ifdef OATPP_TEST_USE_REAL_PORT + return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000); +#else + OATPP_COMPONENT(std::shared_ptr, interface); + auto provider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface); + //provider->setSocketMaxAvailableToReadWrtie(123, 11); + provider->setSocketMaxAvailableToReadWrtie(-1, -1); + return provider; +#endif + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, httpRouter)([] { + return oatpp::web::server::HttpRouter::createShared(); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionHandler)([] { + OATPP_COMPONENT(std::shared_ptr, router); + return oatpp::web::server::HttpConnectionHandler::createShared(router); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, objectMapper)([] { + return oatpp::parser::json::mapping::ObjectMapper::createShared(); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, clientConnectionProvider)([this] { +#ifdef OATPP_TEST_USE_REAL_PORT + return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", 8000); +#else + OATPP_COMPONENT(std::shared_ptr, interface); + auto provider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); + //provider->setSocketMaxAvailableToReadWrtie(12421, 21312); + provider->setSocketMaxAvailableToReadWrtie(-1, -1); + return provider; +#endif + }()); + +}; + +} void FullTest::onRun() { - - auto interface = oatpp::network::virtual_::Interface::createShared("virtualhost"); - - auto serverConnectionProvider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface); - auto clientConnectionProvider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); - serverConnectionProvider->setSocketMaxAvailableToReadWrtie(123, 11); - clientConnectionProvider->setSocketMaxAvailableToReadWrtie(12421, 21312); + TestComponent component; - //serverConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1); - //clientConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1); - - auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(); - - auto router = oatpp::web::server::HttpRouter::createShared(); - auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router); - - auto controller = app::Controller::createShared(objectMapper); - controller->addEndpointsToRouter(router); - - auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider); - - auto client = app::Client::createShared(requestExecutor, objectMapper); - - auto server = oatpp::network::server::Server::createShared(serverConnectionProvider, connectionHandler); - - std::thread clientThread([client, server, objectMapper]{ - - for(v_int32 i = 0; i < 1000; i ++) { + oatpp::test::web::ClientServerTestRunner runner; + + runner.addController(app::Controller::createShared()); + + runner.run([] { + + OATPP_COMPONENT(std::shared_ptr, clientConnectionProvider); + OATPP_COMPONENT(std::shared_ptr, objectMapper); + + auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider); + auto client = app::Client::createShared(requestExecutor, objectMapper); + + auto connection = client->getConnection(); + + v_int32 iterationsStep = 1000; + + for(v_int32 i = 0; i < iterationsStep * 10; i ++) { { // test simple GET - auto response = client->getRoot(); + auto response = client->getRoot(connection); auto value = response->readBodyToString(); OATPP_ASSERT(value == "Hello World!!!"); } - + { // test GET with path parameter - auto response = client->getWithParams("my_test_param"); + auto response = client->getWithParams("my_test_param", connection); auto dto = response->readBodyToDto(objectMapper); OATPP_ASSERT(dto); OATPP_ASSERT(dto->testValue == "my_test_param"); } - + { // test GET with header parameter - auto response = client->getWithHeaders("my_test_header"); + auto response = client->getWithHeaders("my_test_header", connection); auto dto = response->readBodyToDto(objectMapper); OATPP_ASSERT(dto); OATPP_ASSERT(dto->testValue == "my_test_header"); } - + { // test POST with body - auto response = client->postBody("my_test_body"); + auto response = client->postBody("my_test_body", connection); auto dto = response->readBodyToDto(objectMapper); OATPP_ASSERT(dto); OATPP_ASSERT(dto->testValue == "my_test_body"); @@ -108,29 +155,19 @@ void FullTest::onRun() { stream.write("0123456789", 10); } auto data = stream.toString(); - auto response = client->echoBody(data); + auto response = client->echoBody(data, connection); auto returnedData = response->readBodyToString(); OATPP_ASSERT(returnedData); OATPP_ASSERT(returnedData == data); } - + + if((i + 1) % iterationsStep == 0) { + OATPP_LOGD("i", "%d", i + 1); + } + } - - try { - server->stop(); - client->getRoot(); // wake blocking server accept - } catch(std::runtime_error e) { - // DO NOTHING - } - + }); - - std::thread serverThread([server]{ - server->run(); - }); - - clientThread.join(); - serverThread.join(); } diff --git a/test/oatpp/web/app/Controller.hpp b/test/oatpp/web/app/Controller.hpp index 31ac7122..f3aa1aa0 100644 --- a/test/oatpp/web/app/Controller.hpp +++ b/test/oatpp/web/app/Controller.hpp @@ -42,20 +42,20 @@ public: {} public: - static std::shared_ptr createShared(const std::shared_ptr& objectMapper){ + 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) ENDPOINT("GET", "/", root) { - OATPP_LOGD(TAG, "GET '/'"); + //OATPP_LOGD(TAG, "GET '/'"); return createResponse(Status::CODE_200, "Hello World!!!"); } ENDPOINT("GET", "params/{param}", getWithParams, PATH(String, param)) { - OATPP_LOGD(TAG, "GET params/%s", param->c_str()); + //OATPP_LOGD(TAG, "GET params/%s", param->c_str()); auto dto = TestDto::createShared(); dto->testValue = param; return createDtoResponse(Status::CODE_200, dto); @@ -63,7 +63,7 @@ public: ENDPOINT("GET", "headers", getWithHeaders, HEADER(String, param, "X-TEST-HEADER")) { - OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str()); + //OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str()); auto dto = TestDto::createShared(); dto->testValue = param; return createDtoResponse(Status::CODE_200, dto); @@ -71,7 +71,7 @@ public: ENDPOINT("POST", "body", postBody, BODY_STRING(String, body)) { - OATPP_LOGD(TAG, "POST body %s", body->c_str()); + //OATPP_LOGD(TAG, "POST body %s", body->c_str()); auto dto = TestDto::createShared(); dto->testValue = body; return createDtoResponse(Status::CODE_200, dto); @@ -79,7 +79,7 @@ public: ENDPOINT("POST", "echo", echo, BODY_STRING(String, body)) { - OATPP_LOGD(TAG, "POST body(echo) size=%d", body->getSize()); + //OATPP_LOGD(TAG, "POST body(echo) size=%d", body->getSize()); return createResponse(Status::CODE_200, body); } diff --git a/test/oatpp/web/app/ControllerAsync.hpp b/test/oatpp/web/app/ControllerAsync.hpp index 5282c2f7..90a0d3c2 100644 --- a/test/oatpp/web/app/ControllerAsync.hpp +++ b/test/oatpp/web/app/ControllerAsync.hpp @@ -43,7 +43,7 @@ public: {} public: - static std::shared_ptr createShared(const std::shared_ptr& objectMapper){ + static std::shared_ptr createShared(const std::shared_ptr& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr)){ return std::make_shared(objectMapper); } @@ -54,56 +54,56 @@ public: ENDPOINT_ASYNC_INIT(Root) Action act() { - OATPP_LOGD(TAG, "GET '/'"); + //OATPP_LOGD(TAG, "GET '/'"); return _return(controller->createResponse(Status::CODE_200, "Hello World Async!!!")); } - + }; - + ENDPOINT_ASYNC("GET", "params/{param}", GetWithParams) { - + ENDPOINT_ASYNC_INIT(GetWithParams) - + Action act() { auto param = request->getPathVariable("param"); - OATPP_LOGD(TAG, "GET params/%s", param->c_str()); + //OATPP_LOGD(TAG, "GET params/%s", param->c_str()); auto dto = TestDto::createShared(); dto->testValue = param; return _return(controller->createDtoResponse(Status::CODE_200, dto)); } - + }; - + ENDPOINT_ASYNC("GET", "headers", GetWithHeaders) { - + ENDPOINT_ASYNC_INIT(GetWithHeaders) - + Action act() { auto param = request->getHeader("X-TEST-HEADER"); - OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str()); + //OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str()); auto dto = TestDto::createShared(); dto->testValue = param; return _return(controller->createDtoResponse(Status::CODE_200, dto)); } - + }; - + ENDPOINT_ASYNC("POST", "body", PostBody) { - + ENDPOINT_ASYNC_INIT(PostBody) - + Action act() { - OATPP_LOGD(TAG, "POST body. Reading body..."); + //OATPP_LOGD(TAG, "POST body. Reading body..."); return request->readBodyToStringAsync(this, &PostBody::onBodyRead); } - + Action onBodyRead(const String& body) { - OATPP_LOGD(TAG, "POST body %s", body->c_str()); + //OATPP_LOGD(TAG, "POST body %s", body->c_str()); auto dto = TestDto::createShared(); dto->testValue = body; return _return(controller->createDtoResponse(Status::CODE_200, dto)); } - + }; ENDPOINT_ASYNC("POST", "echo", Echo) { @@ -111,12 +111,12 @@ public: ENDPOINT_ASYNC_INIT(Echo) Action act() { - OATPP_LOGD(TAG, "POST body(echo). Reading body..."); + //OATPP_LOGD(TAG, "POST body(echo). Reading body..."); return request->readBodyToStringAsync(this, &Echo::onBodyRead); } Action onBodyRead(const String& body) { - OATPP_LOGD(TAG, "POST echo size=%d", body->getSize()); + //OATPP_LOGD(TAG, "POST echo size=%d", body->getSize()); return _return(controller->createResponse(Status::CODE_200, body)); } From 9efb58fc024b071bb511074007c8a77da988ac77 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Sat, 23 Feb 2019 16:53:07 +0200 Subject: [PATCH 4/6] Better ClientServerTestRunner --- src/CMakeLists.txt | 1 - src/oatpp-test/web/ClientServerTestRunner.cpp | 25 ------ src/oatpp-test/web/ClientServerTestRunner.hpp | 80 ++++++++++++++----- .../server/SimpleTCPConnectionProvider.cpp | 8 +- .../server/SimpleTCPConnectionProvider.hpp | 1 + test/oatpp/AllTestsMain.cpp | 3 +- test/oatpp/web/FullAsyncTest.cpp | 22 ++--- test/oatpp/web/FullTest.cpp | 22 ++--- 8 files changed, 86 insertions(+), 76 deletions(-) delete mode 100644 src/oatpp-test/web/ClientServerTestRunner.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a26da20d..b84c2ecf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -206,7 +206,6 @@ add_library(oatpp-test oatpp-test/Checker.hpp oatpp-test/UnitTest.cpp oatpp-test/UnitTest.hpp - oatpp-test/web/ClientServerTestRunner.cpp oatpp-test/web/ClientServerTestRunner.hpp ) diff --git a/src/oatpp-test/web/ClientServerTestRunner.cpp b/src/oatpp-test/web/ClientServerTestRunner.cpp deleted file mode 100644 index ff5fa638..00000000 --- a/src/oatpp-test/web/ClientServerTestRunner.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/*************************************************************************** - * - * Project _____ __ ____ _ _ - * ( _ ) /__\ (_ _)_| |_ _| |_ - * )(_)( /(__)\ )( (_ _)(_ _) - * (_____)(__)(__)(__) |_| |_| - * - * - * Copyright 2018-present, Leonid Stryzhevskyi - * - * 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 "ClientServerTestRunner.hpp" diff --git a/src/oatpp-test/web/ClientServerTestRunner.hpp b/src/oatpp-test/web/ClientServerTestRunner.hpp index 9c2f9e71..22bb1927 100644 --- a/src/oatpp-test/web/ClientServerTestRunner.hpp +++ b/src/oatpp-test/web/ClientServerTestRunner.hpp @@ -34,9 +34,15 @@ #include "oatpp/core/macro/component.hpp" #include +#include +#include +#include namespace oatpp { namespace test { namespace web { +/** + * Helper class to run Client-Server tests + */ class ClientServerTestRunner { public: typedef oatpp::web::server::HttpRouter HttpRouter; @@ -52,39 +58,75 @@ public: return m_router; } + /** + * Add controller's endpoints to router + * @param controller + */ void addController(const std::shared_ptr& controller) { controller->addEndpointsToRouter(m_router); m_controllers.push_back(controller); } + /** + * Start server, execute code block passed as lambda, stop server. + * @tparam Lambda + * @param lambda + * @param timeout + */ template - void run(const Lambda& lambda) { + void run( + const Lambda& lambda, + const std::chrono::duration& timeout = std::chrono::hours(12) + ) { + + auto startTime = std::chrono::system_clock::now(); + bool running = true; + std::mutex timeoutMutex; + std::condition_variable timeoutCondition; oatpp::network::server::Server server(m_connectionProvider, m_connectionHandler); - - OATPP_LOGD("Server", "Running on port %s...", m_connectionProvider->getProperty("port").toString()->c_str()); - - std::thread clientThread([this, &server, &lambda]{ - - lambda(); - - try { - server.stop(); - m_connectionHandler->stop(); - m_connectionProvider->close(); - } catch(std::runtime_error e) { - // DO NOTHING - } - - }); + OATPP_LOGD("\033[1;34mClientServerTestRunner\033[0m", "\033[1;34mRunning server on port %s. Timeout %lld(micro)\033[0m", + m_connectionProvider->getProperty("port").toString()->c_str(), + timeout.count()); std::thread serverThread([&server]{ server.run(); }); - clientThread.join(); - serverThread.join(); + std::thread clientThread([this, &server, &lambda]{ + lambda(); + + server.stop(); + m_connectionHandler->stop(); + m_connectionProvider->close(); + + }); + + std::thread timerThread([&timeout, &startTime, &running, &timeoutMutex, &timeoutCondition]{ + + auto end = startTime + timeout; + std::unique_lock lock(timeoutMutex); + while(running) { + timeoutCondition.wait_for(lock, std::chrono::seconds(1)); + std::chrono::duration elapsed = std::chrono::system_clock::now() - startTime; + OATPP_ASSERT("ClientServerTestRunner: Error. Timeout." && elapsed < timeout); + } + + }); + + serverThread.join(); + clientThread.join(); + + std::chrono::duration elapsed = std::chrono::system_clock::now() - startTime; + OATPP_LOGD("\033[1;34mClientServerTestRunner\033[0m", "\033[1;34mFinished with time %lld(micro). Stopping server...\033[0m", elapsed.count()); + + running = false; + timeoutCondition.notify_one(); + + timerThread.join(); + + // Wait for worker threads are finished std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp index f4bb5097..249dad36 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.cpp @@ -40,6 +40,7 @@ namespace oatpp { namespace network { namespace server { SimpleTCPConnectionProvider::SimpleTCPConnectionProvider(v_word16 port, bool nonBlocking) : m_port(port) , m_nonBlocking(nonBlocking) + , m_closed(false) { m_serverHandle = instantiateServer(); setProperty(PROPERTY_HOST, "localhost"); @@ -51,7 +52,8 @@ SimpleTCPConnectionProvider::~SimpleTCPConnectionProvider() { } void SimpleTCPConnectionProvider::close() { - if(m_serverHandle != 0) { + if(!m_closed) { + m_closed = true; ::close(m_serverHandle); } } @@ -108,7 +110,9 @@ std::shared_ptr SimpleTCPConnectionProvider::getC if(error == EAGAIN || error == EWOULDBLOCK){ return nullptr; } else { - OATPP_LOGD("[oatpp::network::server::SimpleTCPConnectionProvider::getConnection()]", "Error. %d", error); + if(!m_closed) { // m_serverHandle==0 if ConnectionProvider was closed. Not an error. + OATPP_LOGD("[oatpp::network::server::SimpleTCPConnectionProvider::getConnection()]", "Error. %d", error); + } return nullptr; } } diff --git a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp index 4820fc73..18fa91a2 100644 --- a/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp +++ b/src/oatpp/network/server/SimpleTCPConnectionProvider.hpp @@ -36,6 +36,7 @@ class SimpleTCPConnectionProvider : public base::Controllable, public ServerConn private: v_word16 m_port; bool m_nonBlocking; + bool m_closed; oatpp::data::v_io_handle m_serverHandle; private: oatpp::data::v_io_handle instantiateServer(); diff --git a/test/oatpp/AllTestsMain.cpp b/test/oatpp/AllTestsMain.cpp index 005c9a9d..ab3a93f8 100644 --- a/test/oatpp/AllTestsMain.cpp +++ b/test/oatpp/AllTestsMain.cpp @@ -49,6 +49,7 @@ public: void runTests() { + /* OATPP_RUN_TEST(oatpp::test::base::RegRuleTest); OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest); OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest); @@ -65,7 +66,7 @@ void runTests() { OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest); OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest); - +*/ OATPP_RUN_TEST(oatpp::test::web::FullTest); OATPP_RUN_TEST(oatpp::test::web::FullAsyncTest); diff --git a/test/oatpp/web/FullAsyncTest.cpp b/test/oatpp/web/FullAsyncTest.cpp index 1b8b05a3..e782bffd 100644 --- a/test/oatpp/web/FullAsyncTest.cpp +++ b/test/oatpp/web/FullAsyncTest.cpp @@ -50,7 +50,7 @@ namespace oatpp { namespace test { namespace web { namespace { -//#define OATPP_TEST_USE_REAL_PORT +//#define OATPP_TEST_USE_PORT 8123 class TestComponent { public: @@ -60,14 +60,11 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { -#ifdef OATPP_TEST_USE_REAL_PORT - return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000, true /* nonBlocking */); +#ifdef OATPP_TEST_USE_PORT + return oatpp::network::server::SimpleTCPConnectionProvider::createShared(OATPP_TEST_USE_PORT, true /* nonBlocking */); #else OATPP_COMPONENT(std::shared_ptr, interface); - auto provider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface, true /* nonBlocking */); - //provider->setSocketMaxAvailableToReadWrtie(123, 11); - provider->setSocketMaxAvailableToReadWrtie(-1, -1); - return provider; + return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface, true /* nonBlocking */); #endif }()); @@ -85,14 +82,11 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, clientConnectionProvider)([this] { -#ifdef OATPP_TEST_USE_REAL_PORT - return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", 8000); +#ifdef OATPP_TEST_USE_PORT + return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", OATPP_TEST_USE_PORT); #else OATPP_COMPONENT(std::shared_ptr, interface); - auto provider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); - //provider->setSocketMaxAvailableToReadWrtie(12421, 21312); - provider->setSocketMaxAvailableToReadWrtie(-1, -1); - return provider; + return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); #endif }()); @@ -171,7 +165,7 @@ void FullAsyncTest::onRun() { } - }); + }, std::chrono::minutes(10)); } diff --git a/test/oatpp/web/FullTest.cpp b/test/oatpp/web/FullTest.cpp index 33f252ab..c539e40a 100644 --- a/test/oatpp/web/FullTest.cpp +++ b/test/oatpp/web/FullTest.cpp @@ -50,7 +50,7 @@ namespace oatpp { namespace test { namespace web { namespace { -//#define OATPP_TEST_USE_REAL_PORT +//#define OATPP_TEST_USE_PORT 8123 class TestComponent { public: @@ -60,14 +60,11 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { -#ifdef OATPP_TEST_USE_REAL_PORT - return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000); +#ifdef OATPP_TEST_USE_PORT + return oatpp::network::server::SimpleTCPConnectionProvider::createShared(OATPP_TEST_USE_PORT); #else OATPP_COMPONENT(std::shared_ptr, interface); - auto provider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface); - //provider->setSocketMaxAvailableToReadWrtie(123, 11); - provider->setSocketMaxAvailableToReadWrtie(-1, -1); - return provider; + return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface); #endif }()); @@ -85,14 +82,11 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, clientConnectionProvider)([this] { -#ifdef OATPP_TEST_USE_REAL_PORT - return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", 8000); +#ifdef OATPP_TEST_USE_PORT + return oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", OATPP_TEST_USE_PORT); #else OATPP_COMPONENT(std::shared_ptr, interface); - auto provider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); - //provider->setSocketMaxAvailableToReadWrtie(12421, 21312); - provider->setSocketMaxAvailableToReadWrtie(-1, -1); - return provider; + return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface); #endif }()); @@ -167,7 +161,7 @@ void FullTest::onRun() { } - }); + }, std::chrono::minutes(10)); } From 44b1c9e32b78d8ae543f70132b611fdb88b5cd20 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Sat, 23 Feb 2019 16:55:01 +0200 Subject: [PATCH 5/6] Uncomment all tests --- test/oatpp/AllTestsMain.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/oatpp/AllTestsMain.cpp b/test/oatpp/AllTestsMain.cpp index ab3a93f8..005c9a9d 100644 --- a/test/oatpp/AllTestsMain.cpp +++ b/test/oatpp/AllTestsMain.cpp @@ -49,7 +49,6 @@ public: void runTests() { - /* OATPP_RUN_TEST(oatpp::test::base::RegRuleTest); OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest); OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest); @@ -66,7 +65,7 @@ void runTests() { OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest); OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest); -*/ + OATPP_RUN_TEST(oatpp::test::web::FullTest); OATPP_RUN_TEST(oatpp::test::web::FullAsyncTest); From 70da953b0fe31169631b41c6a2d4a4a0e35d50e3 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Sat, 23 Feb 2019 17:26:48 +0200 Subject: [PATCH 6/6] fix ubuntu build --- src/oatpp-test/web/ClientServerTestRunner.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/oatpp-test/web/ClientServerTestRunner.hpp b/src/oatpp-test/web/ClientServerTestRunner.hpp index 22bb1927..f424524c 100644 --- a/src/oatpp-test/web/ClientServerTestRunner.hpp +++ b/src/oatpp-test/web/ClientServerTestRunner.hpp @@ -109,7 +109,7 @@ public: std::unique_lock lock(timeoutMutex); while(running) { timeoutCondition.wait_for(lock, std::chrono::seconds(1)); - std::chrono::duration elapsed = std::chrono::system_clock::now() - startTime; + auto elapsed = std::chrono::system_clock::now() - startTime; OATPP_ASSERT("ClientServerTestRunner: Error. Timeout." && elapsed < timeout); } @@ -118,7 +118,7 @@ public: serverThread.join(); clientThread.join(); - std::chrono::duration elapsed = std::chrono::system_clock::now() - startTime; + auto elapsed = std::chrono::duration_cast(std::chrono::system_clock::now() - startTime); OATPP_LOGD("\033[1;34mClientServerTestRunner\033[0m", "\033[1;34mFinished with time %lld(micro). Stopping server...\033[0m", elapsed.count()); running = false; @@ -127,7 +127,7 @@ public: timerThread.join(); // Wait for worker threads are finished - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::this_thread::sleep_for(std::chrono::seconds(1)); }