-
Notifications
You must be signed in to change notification settings - Fork 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.5.20220901 | ||
0.0.5.20221102 |