Skip to content

Commit 22b8eb1

Browse files
committed
Stop server using a signal callback
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.
1 parent 5d1d476 commit 22b8eb1

File tree

1 file changed

+21
-5
lines changed
  • ext/test/w3c_tracecontext_test

1 file changed

+21
-5
lines changed

ext/test/w3c_tracecontext_test/main.cc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ int main(int argc, char *argv[])
179179
constexpr char default_host[] = "localhost";
180180
constexpr uint16_t default_port = 30000;
181181
uint16_t port;
182+
std::atomic_bool stop_server(false);
182183

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

229+
testing::HttpRequestCallback stop_cb{
230+
[&](testing::HttpRequest const & /*req*/, testing::HttpResponse &resp) {
231+
std::cout << "Received request to stop server \n";
232+
stop_server.store(true);
233+
resp.code = 200;
234+
return 0;
235+
}};
236+
228237
server["/test"] = test_cb;
238+
server["/stop"] = stop_cb;
229239

230240
// Start server
231241
server.start();
232242

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

235-
// Wait for console input
236-
std::cin.get();
237-
238-
// Stop server
239-
server.stop();
245+
// Wait for signal to stop server
246+
std::thread server_check([&stop_server, &server]() {
247+
while (!stop_server.load())
248+
{
249+
// keep running the thread
250+
}
251+
// received signal to stop server
252+
std::cout << "stopping server \n";
253+
server.stop();
254+
});
255+
server_check.join();
240256
}

0 commit comments

Comments
 (0)