mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
add server side query parameters support
This commit is contained in:
commit
80e6d483fb
@ -206,7 +206,9 @@ add_library(oatpp-test
|
||||
oatpp-test/Checker.hpp
|
||||
oatpp-test/UnitTest.cpp
|
||||
oatpp-test/UnitTest.hpp
|
||||
oatpp-test/web/ClientServerTestRunner.hpp
|
||||
)
|
||||
|
||||
set_target_properties(oatpp-test PROPERTIES
|
||||
CXX_STANDARD 11
|
||||
CXX_EXTENSIONS OFF
|
||||
|
@ -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 <chrono>
|
||||
|
||||
namespace oatpp { namespace test {
|
||||
|
||||
v_int64 UnitTest::getTickCount(){
|
||||
std::chrono::microseconds ms = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}}
|
||||
|
@ -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<class T>
|
||||
static void runTest(v_int32 times){
|
||||
|
138
src/oatpp-test/web/ClientServerTestRunner.hpp
Normal file
138
src/oatpp-test/web/ClientServerTestRunner.hpp
Normal file
@ -0,0 +1,138 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_test_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"
|
||||
|
||||
#include <list>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace oatpp { namespace test { namespace web {
|
||||
|
||||
/**
|
||||
* Helper class to run Client-Server tests
|
||||
*/
|
||||
class ClientServerTestRunner {
|
||||
public:
|
||||
typedef oatpp::web::server::HttpRouter HttpRouter;
|
||||
typedef oatpp::web::server::api::ApiController ApiController;
|
||||
private:
|
||||
std::list<std::shared_ptr<ApiController>> m_controllers;
|
||||
OATPP_COMPONENT(std::shared_ptr<HttpRouter>, m_router);
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, m_connectionProvider);
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, m_connectionHandler);
|
||||
public:
|
||||
|
||||
std::shared_ptr<HttpRouter> getRouter() {
|
||||
return m_router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add controller's endpoints to router
|
||||
* @param controller
|
||||
*/
|
||||
void addController(const std::shared_ptr<ApiController>& 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<typename Lambda>
|
||||
void run(
|
||||
const Lambda& lambda,
|
||||
const std::chrono::duration<v_int64, std::micro>& 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("\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();
|
||||
});
|
||||
|
||||
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<std::mutex> lock(timeoutMutex);
|
||||
while(running) {
|
||||
timeoutCondition.wait_for(lock, std::chrono::seconds(1));
|
||||
auto elapsed = std::chrono::system_clock::now() - startTime;
|
||||
OATPP_ASSERT("ClientServerTestRunner: Error. Timeout." && elapsed < timeout);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
serverThread.join();
|
||||
clientThread.join();
|
||||
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(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::seconds(1));
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif //oatpp_test_web_ClientServerTestRunner_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); \
|
||||
}
|
||||
|
||||
|
@ -55,9 +55,15 @@ protected:
|
||||
void setProperty(const oatpp::String& key, const oatpp::String& value);
|
||||
public:
|
||||
virtual ~ConnectionProvider() {}
|
||||
|
||||
virtual std::shared_ptr<IOStream> 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.
|
||||
|
@ -50,7 +50,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> 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<oatpp::data::stream::IOStream> 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<oatpp::data::stream::IOStream> 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.");
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -43,7 +43,11 @@ public:
|
||||
static std::shared_ptr<SimpleTCPConnectionProvider> createShared(const oatpp::String& host, v_word16 port){
|
||||
return std::make_shared<SimpleTCPConnectionProvider>(host, port);
|
||||
}
|
||||
|
||||
|
||||
void close() override {
|
||||
// DO NOTHING
|
||||
}
|
||||
|
||||
std::shared_ptr<IOStream> getConnection() override;
|
||||
Action getConnectionAsync(oatpp::async::AbstractCoroutine* parentCoroutine, AsyncCallback callback) override;
|
||||
|
||||
|
@ -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<oatpp::data::stream::IOStream>& connection) = 0;
|
||||
|
||||
/**
|
||||
* Stop all threads here
|
||||
*/
|
||||
virtual void stop() = 0;
|
||||
};
|
||||
|
||||
}}}
|
||||
|
@ -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");
|
||||
@ -47,7 +48,14 @@ SimpleTCPConnectionProvider::SimpleTCPConnectionProvider(v_word16 port, bool non
|
||||
}
|
||||
|
||||
SimpleTCPConnectionProvider::~SimpleTCPConnectionProvider() {
|
||||
::close(m_serverHandle);
|
||||
close();
|
||||
}
|
||||
|
||||
void SimpleTCPConnectionProvider::close() {
|
||||
if(!m_closed) {
|
||||
m_closed = true;
|
||||
::close(m_serverHandle);
|
||||
}
|
||||
}
|
||||
|
||||
oatpp::data::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){
|
||||
@ -70,14 +78,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 ;
|
||||
}
|
||||
|
||||
@ -94,9 +102,7 @@ oatpp::data::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){
|
||||
}
|
||||
|
||||
std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getConnection(){
|
||||
|
||||
//oatpp::test::PerformanceChecker checker("Accept Checker");
|
||||
|
||||
|
||||
oatpp::data::v_io_handle handle = accept(m_serverHandle, nullptr, nullptr);
|
||||
|
||||
if (handle < 0) {
|
||||
@ -104,7 +110,9 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getC
|
||||
if(error == EAGAIN || error == EWOULDBLOCK){
|
||||
return nullptr;
|
||||
} else {
|
||||
OATPP_LOGD("Server", "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;
|
||||
}
|
||||
}
|
||||
@ -113,7 +121,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> 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
|
||||
|
||||
|
@ -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();
|
||||
@ -48,6 +49,8 @@ public:
|
||||
}
|
||||
|
||||
~SimpleTCPConnectionProvider();
|
||||
|
||||
void close() override;
|
||||
|
||||
std::shared_ptr<IOStream> getConnection() override;
|
||||
|
||||
@ -61,7 +64,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(){
|
||||
|
@ -89,11 +89,14 @@ std::shared_ptr<Interface::ConnectionSubmission> Interface::connectNonBlocking()
|
||||
return submission;
|
||||
}
|
||||
|
||||
std::shared_ptr<Socket> Interface::accept() {
|
||||
std::shared_ptr<Socket> Interface::accept(const bool& waitingHandle) {
|
||||
std::unique_lock<std::mutex> 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<Socket> Interface::acceptNonBlocking() {
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Interface::notifyAcceptors() {
|
||||
m_condition.notify_all();
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -70,9 +70,21 @@ public:
|
||||
|
||||
std::shared_ptr<ConnectionSubmission> connect();
|
||||
std::shared_ptr<ConnectionSubmission> connectNonBlocking();
|
||||
|
||||
std::shared_ptr<Socket> accept();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param waitingHandle
|
||||
* @return
|
||||
*/
|
||||
std::shared_ptr<Socket> accept(const bool& waitingHandle = true);
|
||||
|
||||
std::shared_ptr<Socket> 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;
|
||||
|
@ -25,7 +25,11 @@
|
||||
#include "ConnectionProvider.hpp"
|
||||
|
||||
namespace oatpp { namespace network { namespace virtual_ { namespace client {
|
||||
|
||||
|
||||
void ConnectionProvider::close() {
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<ConnectionProvider::IOStream> ConnectionProvider::getConnection() {
|
||||
auto submission = m_interface->connect();
|
||||
auto socket = submission->getSocket();
|
||||
|
@ -58,6 +58,8 @@ public:
|
||||
m_maxAvailableToRead = maxToRead;
|
||||
m_maxAvailableToWrite = maxToWrite;
|
||||
}
|
||||
|
||||
void close() override;
|
||||
|
||||
std::shared_ptr<IOStream> getConnection() override;
|
||||
|
||||
|
@ -25,11 +25,18 @@
|
||||
#include "ConnectionProvider.hpp"
|
||||
|
||||
namespace oatpp { namespace network { namespace virtual_ { namespace server {
|
||||
|
||||
|
||||
void ConnectionProvider::close() {
|
||||
m_open = false;
|
||||
m_interface->notifyAcceptors();
|
||||
}
|
||||
|
||||
std::shared_ptr<ConnectionProvider::IOStream> 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;
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ class ConnectionProvider : public oatpp::network::ServerConnectionProvider {
|
||||
private:
|
||||
std::shared_ptr<virtual_::Interface> 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<virtual_::Interface>& interface, bool nonBlocking = false)
|
||||
: m_interface(interface)
|
||||
, m_nonBlocking(nonBlocking)
|
||||
, m_open(true)
|
||||
, m_maxAvailableToRead(-1)
|
||||
, m_maxAvailableToWrite(-1)
|
||||
{
|
||||
@ -51,7 +53,7 @@ public:
|
||||
static std::shared_ptr<ConnectionProvider> createShared(const std::shared_ptr<virtual_::Interface>& interface, bool nonBlocking = false) {
|
||||
return std::make_shared<ConnectionProvider>(interface, nonBlocking);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* this one used for testing purposes only
|
||||
* set to -1 in order to ignore this value
|
||||
@ -60,6 +62,8 @@ public:
|
||||
m_maxAvailableToRead = maxToRead;
|
||||
m_maxAvailableToWrite = maxToWrite;
|
||||
}
|
||||
|
||||
void close() override;
|
||||
|
||||
std::shared_ptr<IOStream> getConnection() override;
|
||||
|
||||
@ -73,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.");
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -94,7 +94,7 @@ public:
|
||||
|
||||
void handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) override;
|
||||
|
||||
void stop() {
|
||||
void stop() override {
|
||||
m_executor->stop();
|
||||
}
|
||||
|
||||
|
@ -107,6 +107,10 @@ public:
|
||||
}
|
||||
|
||||
void handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) override;
|
||||
|
||||
void stop() override {
|
||||
// DO NOTHING
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -36,7 +36,7 @@ class CommandLineArgumentsTest : public UnitTest{
|
||||
public:
|
||||
|
||||
CommandLineArgumentsTest():UnitTest("TEST[base::CommandLineArgumentsTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -33,7 +33,7 @@ class RegRuleTest : public UnitTest{
|
||||
public:
|
||||
|
||||
RegRuleTest():UnitTest("TEST[base::RegRuleTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -33,7 +33,7 @@ class LinkedListTest : public UnitTest{
|
||||
public:
|
||||
|
||||
LinkedListTest():UnitTest("TEST[oatpp::collection::LinkedListTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -36,7 +36,7 @@ class MemoryPoolTest : public UnitTest{
|
||||
public:
|
||||
|
||||
MemoryPoolTest():UnitTest("TEST[base::memory::MemoryPoolTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -33,7 +33,7 @@ class PerfTest : public UnitTest{
|
||||
public:
|
||||
|
||||
PerfTest():UnitTest("TEST[base::memory::PerfTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}}}}}}
|
||||
|
@ -33,7 +33,7 @@ class TypeTest : public UnitTest{
|
||||
public:
|
||||
|
||||
TypeTest():UnitTest("TEST[core::data::mapping::type::TypeTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}}}
|
||||
|
@ -33,7 +33,7 @@ class MemoryLabelTest : public UnitTest{
|
||||
public:
|
||||
|
||||
MemoryLabelTest():UnitTest("TEST[core::data::share::MemoryLabelTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}}}
|
@ -33,7 +33,7 @@ class CaretTest : public UnitTest{
|
||||
public:
|
||||
|
||||
CaretTest():UnitTest("TEST[parser::CaretTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
}}}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
}}}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
}}}}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
}}}}
|
||||
|
@ -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<Test1>(caret);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}}}}}
|
||||
|
@ -33,7 +33,7 @@ class DTOMapperPerfTest : public UnitTest{
|
||||
public:
|
||||
|
||||
DTOMapperPerfTest():UnitTest("TEST[parser::json::mapping::DTOMapperPerfTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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)
|
||||
|
@ -33,7 +33,7 @@ class DTOMapperTest : public UnitTest{
|
||||
public:
|
||||
|
||||
DTOMapperTest():UnitTest("TEST[parser::json::mapping::DTOMapperTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}}}
|
||||
|
@ -33,7 +33,7 @@ class DeserializerTest : public UnitTest{
|
||||
public:
|
||||
|
||||
DeserializerTest():UnitTest("TEST[parser::json::mapping::DeserializerTestTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -35,59 +35,102 @@
|
||||
|
||||
#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 {
|
||||
|
||||
bool 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);
|
||||
namespace {
|
||||
|
||||
//#define OATPP_TEST_USE_PORT 8123
|
||||
|
||||
class TestComponent {
|
||||
public:
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
|
||||
return oatpp::network::virtual_::Interface::createShared("virtualhost");
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
|
||||
#ifdef OATPP_TEST_USE_PORT
|
||||
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(OATPP_TEST_USE_PORT, true /* nonBlocking */);
|
||||
#else
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface, true /* nonBlocking */);
|
||||
#endif
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
||||
return oatpp::web::server::HttpRouter::createShared();
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
||||
return oatpp::web::server::AsyncHttpConnectionHandler::createShared(router);
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper)([] {
|
||||
return oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([this] {
|
||||
#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<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
|
||||
#endif
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
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 ++) {
|
||||
void FullAsyncTest::onRun() {
|
||||
|
||||
TestComponent component;
|
||||
|
||||
oatpp::test::web::ClientServerTestRunner runner;
|
||||
|
||||
runner.addController(app::ControllerAsync::createShared());
|
||||
|
||||
runner.run([] {
|
||||
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, 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<app::TestDto>(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<app::TestDto>(objectMapper);
|
||||
@ -96,7 +139,7 @@ bool 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<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_body-Async");
|
||||
@ -108,36 +151,22 @@ bool 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->getRoot(); // 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));
|
||||
|
||||
return true;
|
||||
}, std::chrono::minutes(10));
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -33,7 +33,7 @@ class FullAsyncTest : public UnitTest {
|
||||
public:
|
||||
|
||||
FullAsyncTest():UnitTest("TEST[web::FullAsyncTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -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,53 +35,95 @@
|
||||
|
||||
#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 {
|
||||
|
||||
bool 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);
|
||||
namespace {
|
||||
|
||||
//serverConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1);
|
||||
//clientConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1);
|
||||
//#define OATPP_TEST_USE_PORT 8123
|
||||
|
||||
class TestComponent {
|
||||
public:
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
|
||||
return oatpp::network::virtual_::Interface::createShared("virtualhost");
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
|
||||
#ifdef OATPP_TEST_USE_PORT
|
||||
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(OATPP_TEST_USE_PORT);
|
||||
#else
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::server::ConnectionProvider::createShared(interface);
|
||||
#endif
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
||||
return oatpp::web::server::HttpRouter::createShared();
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
||||
return oatpp::web::server::HttpConnectionHandler::createShared(router);
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper)([] {
|
||||
return oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider)([this] {
|
||||
#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<oatpp::network::virtual_::Interface>, interface);
|
||||
return oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
|
||||
#endif
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
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 ++) {
|
||||
void FullTest::onRun() {
|
||||
|
||||
TestComponent component;
|
||||
|
||||
oatpp::test::web::ClientServerTestRunner runner;
|
||||
|
||||
runner.addController(app::Controller::createShared());
|
||||
|
||||
runner.run([] {
|
||||
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, 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<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_param");
|
||||
@ -94,16 +135,16 @@ bool FullTest::onRun() {
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "age=1&name=oatpp");
|
||||
}
|
||||
|
||||
|
||||
{ // test GET with header parameter
|
||||
auto response = client->getWithHeaders("my_test_header");
|
||||
auto response = client->getWithHeaders("my_test_header", connection);
|
||||
auto dto = response->readBodyToDto<app::TestDto>(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<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_body");
|
||||
@ -115,31 +156,20 @@ bool 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();
|
||||
|
||||
return true;
|
||||
|
||||
}, std::chrono::minutes(10));
|
||||
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -33,7 +33,7 @@ class FullTest : public UnitTest {
|
||||
public:
|
||||
|
||||
FullTest():UnitTest("TEST[web::FullTest]"){}
|
||||
bool onRun() override;
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
|
@ -43,20 +43,20 @@ public:
|
||||
{}
|
||||
public:
|
||||
|
||||
static std::shared_ptr<Controller> createShared(const std::shared_ptr<ObjectMapper>& objectMapper){
|
||||
static std::shared_ptr<Controller> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
|
||||
return std::make_shared<Controller>(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);
|
||||
@ -80,7 +80,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);
|
||||
@ -88,7 +88,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);
|
||||
@ -96,7 +96,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);
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public:
|
||||
{}
|
||||
public:
|
||||
|
||||
static std::shared_ptr<ControllerAsync> createShared(const std::shared_ptr<ObjectMapper>& objectMapper){
|
||||
static std::shared_ptr<ControllerAsync> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
|
||||
return std::make_shared<ControllerAsync>(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));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user