Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9fabdbf

Browse filesBrowse files
authored
Merge pull request #14 from qchateau/this-server-stop
This server stop
2 parents 666fb0a + eaba360 commit 9fabdbf
Copy full SHA for 9fabdbf

File tree

2 files changed

+27
-2
lines changed
Filter options

2 files changed

+27
-2
lines changed

‎lib/rpc/server.cc

Copy file name to clipboardExpand all lines: lib/rpc/server.cc
+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "asio.hpp"
1010
#include "format.h"
1111

12+
#include "rpc/this_server.h"
1213
#include "rpc/detail/dev_utils.h"
1314
#include "rpc/detail/log.h"
1415
#include "rpc/detail/log.h"
@@ -62,7 +63,8 @@ struct server::impl {
6263
} else {
6364
LOG_ERROR("Error while accepting connection: {}", ec);
6465
}
65-
start_accept();
66+
if (!this_server().stopping())
67+
start_accept();
6668
// TODO: allow graceful exit [sztomi 2016-01-13]
6769
});
6870
}
@@ -77,6 +79,9 @@ struct server::impl {
7779
for (auto &session : sessions_copy) {
7880
session->close();
7981
}
82+
83+
if (this_server().stopping())
84+
acceptor_.cancel();
8085
}
8186

8287
void stop() {

‎tests/rpc/this_server_test.cc

Copy file name to clipboardExpand all lines: tests/rpc/this_server_test.cc
+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "gtest/gtest.h"
22

3+
#include <future>
4+
35
#include "rpc/client.h"
46
#include "rpc/server.h"
57
#include "rpc/this_server.h"
@@ -24,7 +26,7 @@ class this_server_test : public testing::Test {
2426
rpc::client c2;
2527
};
2628

27-
TEST_F(this_server_test, stop) {
29+
TEST_F(this_server_test, stop_async) {
2830
s.bind("stop_server", []() { rpc::this_server().stop(); });
2931
s.async_run();
3032
c1.call("stop_server");
@@ -35,3 +37,21 @@ TEST_F(this_server_test, stop) {
3537
EXPECT_EQ(c2.get_connection_state(),
3638
client::connection_state::disconnected);
3739
}
40+
41+
TEST_F(this_server_test, stop_sync) {
42+
s.bind("stop_server", []() { rpc::this_server().stop(); });
43+
44+
auto handle = std::async(std::launch::async, [this]() {
45+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
46+
c1.call("stop_server");
47+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
48+
});
49+
50+
s.run();
51+
handle.get();
52+
53+
EXPECT_EQ(c1.get_connection_state(),
54+
client::connection_state::disconnected);
55+
EXPECT_EQ(c2.get_connection_state(),
56+
client::connection_state::disconnected);
57+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.