diff options
author | alexv-smirnov <alex@ydb.tech> | 2022-08-18 16:52:30 +0300 |
---|---|---|
committer | alexv-smirnov <alex@ydb.tech> | 2022-08-18 16:52:30 +0300 |
commit | c140abc954b61ab7d86af80bdeced01482d9971a (patch) | |
tree | c47d70fa3213240d5e0eb59787a5325782a360de /library/cpp/testing | |
parent | 0ce07b9705ed20e3fce2759eae41496014ca4c33 (diff) | |
download | ydb-c140abc954b61ab7d86af80bdeced01482d9971a.tar.gz |
temp fix ydb oss sync config to unlock sync on /vendor dependency
Diffstat (limited to 'library/cpp/testing')
-rw-r--r-- | library/cpp/testing/mock_server/server.cpp | 70 | ||||
-rw-r--r-- | library/cpp/testing/mock_server/server.h | 52 |
2 files changed, 122 insertions, 0 deletions
diff --git a/library/cpp/testing/mock_server/server.cpp b/library/cpp/testing/mock_server/server.cpp new file mode 100644 index 0000000000..d310c76ad3 --- /dev/null +++ b/library/cpp/testing/mock_server/server.cpp @@ -0,0 +1,70 @@ +#include "server.h" + +#include <util/system/thread.h> + +namespace NMock { + class TMockServer::TCallBack: public THttpServer::ICallBack { + TGenerator Generator_; + + TClientRequest* CreateClient() override { + return Generator_(); + } + + public: + void SetGenerator(TGenerator generator) { + Generator_ = generator; + } + + TCallBack(TGenerator& generator) + : Generator_(generator) + { + } + }; + + static THttpServerOptions createDefaultOptions(ui16 port) { + THttpServerOptions o; + o.AddBindAddress("localhost", port); + o.SetThreads(1); + o.SetMaxConnections(300); + o.SetMaxQueueSize(30); + return o; + } + + TMockServer::TMockServer(ui16 port, TGenerator generator) + : TMockServer(createDefaultOptions(port), generator) + { + } + + TMockServer::TMockServer(const THttpServerOptions& options, TGenerator generator) + : Cb_(MakeHolder<TCallBack>(generator)) + , Server_(MakeHolder<THttpServer>(Cb_.Get(), options)) + , Thread_(MakeHolder<TThread>(Worker, this)) + , Ev_(MakeHolder<TAutoEvent>()) + { + Thread_->Start(); + Ev_->Wait(); + } + + TMockServer::~TMockServer() { + Server_->Stop(); + Thread_->Join(); + } + + size_t TMockServer::GetClientCount() const { + return Server_->GetClientCount(); + } + + void TMockServer::SetGenerator(TMockServer::TGenerator generator) { + Cb_->SetGenerator(generator); + } + + void* TMockServer::Worker(void* arg) { + TMockServer& this_ = *static_cast<TMockServer*>(arg); + + this_.Server_->Start(); + this_.Ev_->Signal(); + this_.Server_->Wait(); + + return nullptr; + } +} diff --git a/library/cpp/testing/mock_server/server.h b/library/cpp/testing/mock_server/server.h new file mode 100644 index 0000000000..6d57cebf40 --- /dev/null +++ b/library/cpp/testing/mock_server/server.h @@ -0,0 +1,52 @@ +#pragma once + +#include <library/cpp/http/misc/parsed_request.h> +#include <library/cpp/http/server/http.h> +#include <library/cpp/http/server/response.h> +#include <library/cpp/testing/unittest/tests_data.h> + +#include <util/system/event.h> + +#include <functional> + +class TAutoEvent; +class TThread; + +namespace NMock { + class TMockServer { + public: + using TGenerator = std::function<TRequestReplier*()>; + + TMockServer(ui16 port, TGenerator generator); + TMockServer(const THttpServerOptions& options, TGenerator generator); + + ~TMockServer(); + + size_t GetClientCount() const; + void SetGenerator(TGenerator generator); + + private: + static void* Worker(void* arg); + + class TCallBack; + + THolder<TCallBack> Cb_; + THolder<THttpServer> Server_; + THolder<TThread> Thread_; + THolder<TAutoEvent> Ev_; + }; + + class TPong: public TRequestReplier { + public: + bool DoReply(const TReplyParams& params) override { + const TParsedHttpFull parsed(params.Input.FirstLine()); + + const HttpCodes code = parsed.Path == "/ping" ? HTTP_OK : HTTP_NOT_FOUND; + + THttpResponse resp(code); + resp.OutTo(params.Output); + + return true; + } + }; +} |