Skip to content

Commit

Permalink
pistache server throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
chorfa007 committed Nov 2, 2022
1 parent 769e4f4 commit 86bdf24
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pistache_test_files = [
'threadname_test',
'typeid_test',
'view_test',
'remove_routes_crash'
]

network_tests = ['net_test']
Expand Down
110 changes: 110 additions & 0 deletions tests/remove_routes_crash.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <gtest/gtest.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/peer.h>
#include <pistache/router.h>

#include "rapidjson/document.h"
#include <csignal>
#include <iostream>
#include <memory>

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<Pistache::Http::Endpoint>(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<Http::Endpoint> 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<Server>(addr);
server_instance->StartServer();
server_instance->SwitchMode();
server_instance->StopServer();
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.5.20220901
0.0.5.20221102

0 comments on commit 86bdf24

Please sign in to comment.