From 86bdf24cb83d9ece17efc9aaf34915834429ce74 Mon Sep 17 00:00:00 2001 From: chorfa007 Date: Wed, 2 Nov 2022 13:55:41 +0100 Subject: [PATCH] pistache server throw exception --- tests/meson.build | 1 + tests/remove_routes_crash.cc | 110 +++++++++++++++++++++++++++++++++++ version.txt | 2 +- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/remove_routes_crash.cc diff --git a/tests/meson.build b/tests/meson.build index d74761521..476fcd5b2 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -33,6 +33,7 @@ pistache_test_files = [ 'threadname_test', 'typeid_test', 'view_test', + 'remove_routes_crash' ] network_tests = ['net_test'] diff --git a/tests/remove_routes_crash.cc b/tests/remove_routes_crash.cc new file mode 100644 index 000000000..fd28a2a29 --- /dev/null +++ b/tests/remove_routes_crash.cc @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + +#include "rapidjson/document.h" +#include +#include +#include + +using namespace Pistache; +using namespace Rest; + +static constexpr auto KEEPALIVE_TIMEOUT = std::chrono::seconds(2); +static constexpr auto BT_BUF_SIZE = 100; +class Server +{ +public: + enum Modes { + mode_1, + mode_2 + }; + Server(Address addr) + { + endpoint_ = std::make_shared(addr); + auto opts = Pistache::Http::Endpoint::options() + .threads(2) + .flags(Pistache::Tcp::Options::ReuseAddr); + opts.keepaliveTimeout(KEEPALIVE_TIMEOUT); + endpoint_->init(opts); + system_mode_ = Server::Modes::mode_2; + SetupRoutes(); + } + virtual ~Server() = default; + void SwitchMode() + { + if (system_mode_ == Modes::mode_1) + { + Routes::Remove(router_, Pistache::Http::Method::Get, "/read/hello_fun_1_mode_1"); + Routes::Remove(router_, Pistache::Http::Method::Get, "/read/hello_fun_2_mode_1"); + system_mode_ = Modes::mode_2; + SetupRoutes(); + } + else + { + Routes::Remove(router_, Pistache::Http::Method::Get, "/read/hello_fun_1_mode_2"); + Routes::Remove(router_, Pistache::Http::Method::Get, "/read/hello_fun_1_mode_2"); + system_mode_ = Modes::mode_1; + SetupRoutes(); + } + } + void StartServer() + { + endpoint_->setHandler(router_.handler()); + endpoint_->serveThreaded(); + } + void StopServer() + { + endpoint_->shutdown(); + } + +private: + Router router_; + Router empty_router_; + std::shared_ptr endpoint_; + +private: + void SetupRoutes() + { + using namespace Pistache::Rest; + if (system_mode_ == Modes::mode_1) + { + Routes::Get(router_, "/read/hello_fun_1_mode_1", Routes::bind(&Server::hello_fun_1_mode_1, this)); + Routes::Get(router_, "/read/hello_fun_2_mode_1", Routes::bind(&Server::hello_fun_2_mode_1, this)); + } + else + { + Routes::Get(router_, "/read/hello_fun_1_mode_2", Routes::bind(&Server::hello_fun_2_mode_2, this)); + Routes::Get(router_, "/read/hello_fun_2_mode_2", Routes::bind(&Server::hello_fun_2_mode_2, this)); + } + } + + void hello_fun_1_mode_1(const Rest::Request& /*request*/, Http::ResponseWriter response) + { + response.send(Http::Code::Ok, response.peer()->hostname()); + } + void hello_fun_2_mode_1(const Rest::Request& /*request*/, Http::ResponseWriter response) + { + response.send(Http::Code::Ok, response.peer()->hostname()); + } + void hello_fun_1_mode_2(const Rest::Request& /*request*/, Http::ResponseWriter response) + { + response.send(Http::Code::Ok, response.peer()->hostname()); + } + void hello_fun_2_mode_2(const Rest::Request& /*request*/, Http::ResponseWriter response) + { + response.send(Http::Code::Ok, response.peer()->hostname()); + } + Modes system_mode_ = Modes::mode_2; +}; + +TEST(rest_server_test, remove_routes_crash) +{ + Address addr(Ipv4::any(), Port(0)); + auto server_instance = std::make_unique(addr); + server_instance->StartServer(); + server_instance->SwitchMode(); + server_instance->StopServer(); +} diff --git a/version.txt b/version.txt index 5881edf17..c3d33cecc 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.0.5.20220901 +0.0.5.20221102