PipelineTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "PipelineTest.hpp"
  25. #include "oatpp/web/app/Controller.hpp"
  26. #include "oatpp/web/server/HttpConnectionHandler.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::network::virtual_::Interface>, virtualInterface)([] {
  47. return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
  48. }());
  49. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
  50. if(m_port == 0) { // Use oatpp virtual interface
  51. OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, _interface);
  52. return std::static_pointer_cast<oatpp::network::ServerConnectionProvider>(
  53. oatpp::network::virtual_::server::ConnectionProvider::createShared(_interface)
  54. );
  55. }
  56. return std::static_pointer_cast<oatpp::network::ServerConnectionProvider>(
  57. oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", m_port})
  58. );
  59. }());
  60. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
  61. return oatpp::web::server::HttpRouter::createShared();
  62. }());
  63. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
  64. OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
  65. return oatpp::web::server::HttpConnectionHandler::createShared(router);
  66. }());
  67. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper)([] {
  68. return oatpp::parser::json::mapping::ObjectMapper::createShared();
  69. }());
  70. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([this] {
  71. if(m_port == 0) {
  72. OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, _interface);
  73. return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
  74. oatpp::network::virtual_::client::ConnectionProvider::createShared(_interface)
  75. );
  76. }
  77. return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
  78. oatpp::network::tcp::client::ConnectionProvider::createShared({"localhost", m_port})
  79. );
  80. }());
  81. };
  82. const char* const SAMPLE_IN =
  83. "GET / HTTP/1.1\r\n"
  84. "Connection: keep-alive\r\n"
  85. "Content-Length: 0\r\n"
  86. "\r\n";
  87. const char* const SAMPLE_OUT =
  88. "HTTP/1.1 200 OK\r\n"
  89. "Content-Length: 14\r\n"
  90. "Connection: keep-alive\r\n"
  91. "Server: oatpp/" OATPP_VERSION "\r\n"
  92. "\r\n"
  93. "Hello World!!!";
  94. }
  95. void PipelineTest::onRun() {
  96. TestComponent component(m_port);
  97. oatpp::test::web::ClientServerTestRunner runner;
  98. runner.addController(app::Controller::createShared());
  99. runner.run([this, &runner] {
  100. OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
  101. auto connection = clientConnectionProvider->get();
  102. connection.object->setInputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);
  103. std::thread pipeInThread([this, connection] {
  104. oatpp::data::stream::BufferOutputStream pipelineStream;
  105. for (v_int32 i = 0; i < m_pipelineSize; i++) {
  106. pipelineStream << SAMPLE_IN;
  107. }
  108. auto dataToSend = pipelineStream.toString();
  109. OATPP_LOGD(TAG, "Sending %d bytes", dataToSend->size());
  110. oatpp::data::stream::BufferInputStream inputStream(dataToSend);
  111. oatpp::data::buffer::IOBuffer ioBuffer;
  112. auto res = oatpp::data::stream::transfer(&inputStream, connection.object.get(), 0, ioBuffer.getData(), ioBuffer.getSize());
  113. });
  114. std::thread pipeOutThread([this, connection] {
  115. oatpp::String sample = SAMPLE_OUT;
  116. oatpp::data::stream::BufferOutputStream receiveStream;
  117. oatpp::data::buffer::IOBuffer ioBuffer;
  118. v_io_size transferSize = sample->size() * m_pipelineSize;
  119. OATPP_LOGD(TAG, "want to Receive %d bytes", transferSize);
  120. auto res = oatpp::data::stream::transfer(connection.object.get(), &receiveStream, transferSize, 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. }, std::chrono::minutes(10));
  128. std::this_thread::sleep_for(std::chrono::seconds(1));
  129. }
  130. }}}