PipelineAsyncTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #include "PipelineAsyncTest.hpp"
  25. #include "oatpp/web/app/ControllerAsync.hpp"
  26. #include "oatpp/web/server/AsyncHttpConnectionHandler.hpp"
  27. #include "oatpp/web/server/HttpRouter.hpp"
  28. #include "oatpp/parser/json/mapping/ObjectMapper.hpp"
  29. #include "oatpp/network/tcp/server/ConnectionProvider.hpp"
  30. #include "oatpp/network/tcp/client/ConnectionProvider.hpp"
  31. #include "oatpp/network/virtual_/client/ConnectionProvider.hpp"
  32. #include "oatpp/network/virtual_/server/ConnectionProvider.hpp"
  33. #include "oatpp/network/virtual_/Interface.hpp"
  34. #include "oatpp/core/data/stream/BufferStream.hpp"
  35. #include "oatpp/core/macro/component.hpp"
  36. #include "oatpp-test/web/ClientServerTestRunner.hpp"
  37. namespace oatpp { namespace test { namespace web {
  38. namespace {
  39. class TestComponent {
  40. private:
  41. v_uint16 m_port;
  42. public:
  43. TestComponent(v_uint16 port)
  44. : m_port(port)
  45. {}
  46. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::async::Executor>, executor)([] {
  47. return std::make_shared<oatpp::async::Executor>(1, 1, 1);
  48. }());
  49. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
  50. return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
  51. }());
  52. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
  53. if(m_port == 0) { // Use oatpp virtual interface
  54. OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, _interface);
  55. return std::static_pointer_cast<oatpp::network::ServerConnectionProvider>(
  56. oatpp::network::virtual_::server::ConnectionProvider::createShared(_interface)
  57. );
  58. }
  59. return std::static_pointer_cast<oatpp::network::ServerConnectionProvider>(
  60. oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", m_port})
  61. );
  62. }());
  63. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
  64. return oatpp::web::server::HttpRouter::createShared();
  65. }());
  66. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
  67. OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
  68. OATPP_COMPONENT(std::shared_ptr<oatpp::async::Executor>, executor);
  69. return oatpp::web::server::AsyncHttpConnectionHandler::createShared(router, executor);
  70. }());
  71. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper)([] {
  72. return oatpp::parser::json::mapping::ObjectMapper::createShared();
  73. }());
  74. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([this] {
  75. if(m_port == 0) {
  76. OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, _interface);
  77. return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
  78. oatpp::network::virtual_::client::ConnectionProvider::createShared(_interface)
  79. );
  80. }
  81. return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
  82. oatpp::network::tcp::client::ConnectionProvider::createShared({"localhost", m_port})
  83. );
  84. }());
  85. };
  86. const char* const SAMPLE_IN =
  87. "GET / HTTP/1.1\r\n"
  88. "Connection: keep-alive\r\n"
  89. "Content-Length: 0\r\n"
  90. "\r\n";
  91. const char* const SAMPLE_OUT =
  92. "HTTP/1.1 200 OK\r\n"
  93. "Content-Length: 20\r\n"
  94. "Connection: keep-alive\r\n"
  95. "Server: oatpp/" OATPP_VERSION "\r\n"
  96. "\r\n"
  97. "Hello World Async!!!";
  98. }
  99. void PipelineAsyncTest::onRun() {
  100. TestComponent component(m_port);
  101. oatpp::test::web::ClientServerTestRunner runner;
  102. runner.addController(app::ControllerAsync::createShared());
  103. runner.run([this, &runner] {
  104. OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
  105. auto connection = clientConnectionProvider->get();
  106. connection.object->setInputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);
  107. std::thread pipeInThread([this, connection] {
  108. oatpp::data::stream::BufferOutputStream pipelineStream;
  109. for (v_int32 i = 0; i < m_pipelineSize; i++) {
  110. pipelineStream << SAMPLE_IN;
  111. }
  112. oatpp::data::stream::BufferInputStream inputStream(pipelineStream.toString());
  113. oatpp::data::buffer::IOBuffer ioBuffer;
  114. oatpp::data::stream::transfer(&inputStream, connection.object.get(), 0, ioBuffer.getData(), ioBuffer.getSize());
  115. });
  116. std::thread pipeOutThread([this, connection] {
  117. oatpp::String sample = SAMPLE_OUT;
  118. oatpp::data::stream::BufferOutputStream receiveStream;
  119. oatpp::data::buffer::IOBuffer ioBuffer;
  120. auto res = oatpp::data::stream::transfer(connection.object.get(), &receiveStream, sample->size() * m_pipelineSize, ioBuffer.getData(), ioBuffer.getSize());
  121. auto result = receiveStream.toString();
  122. OATPP_ASSERT(result->size() == sample->size() * m_pipelineSize);
  123. //OATPP_ASSERT(result == wantedResult); // headers may come in different order on different OSs
  124. });
  125. pipeOutThread.join();
  126. pipeInThread.join();
  127. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  128. // Stop server and unblock accepting thread
  129. //connection.reset();
  130. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  131. }, std::chrono::minutes(10));
  132. OATPP_COMPONENT(std::shared_ptr<oatpp::async::Executor>, executor);
  133. executor->waitTasksFinished();
  134. executor->stop();
  135. executor->join();
  136. }
  137. }}}