Skip to content

Commit

Permalink
Stop server using a signal callback
Browse files Browse the repository at this point in the history
Using cin.get() as a means to stop the server listening for requests is
preventing the server from being used in a CI environment. This also allows
the test server to run in background since exit is now controlled by a
http request.
  • Loading branch information
psx95 committed Nov 1, 2024
1 parent 5d1d476 commit 22b8eb1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ext/test/w3c_tracecontext_test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ int main(int argc, char *argv[])
constexpr char default_host[] = "localhost";
constexpr uint16_t default_port = 30000;
uint16_t port;
std::atomic_bool stop_server(false);

// The port the validation service listens to can be specified via the command line.
if (argc > 1)
Expand Down Expand Up @@ -225,16 +226,31 @@ int main(int argc, char *argv[])
return 0;
}};

testing::HttpRequestCallback stop_cb{
[&](testing::HttpRequest const & /*req*/, testing::HttpResponse &resp) {
std::cout << "Received request to stop server \n";
stop_server.store(true);
resp.code = 200;
return 0;
}};

server["/test"] = test_cb;
server["/stop"] = stop_cb;

// Start server
server.start();

std::cout << "Listening at http://" << default_host << ":" << port << "/test\n";

// Wait for console input
std::cin.get();

// Stop server
server.stop();
// Wait for signal to stop server
std::thread server_check([&stop_server, &server]() {
while (!stop_server.load())
{
// keep running the thread
}
// received signal to stop server
std::cout << "stopping server \n";
server.stop();
});
server_check.join();
}

0 comments on commit 22b8eb1

Please sign in to comment.