fix session cancel issue

This commit is contained in:
Chunting Gu
2021-06-09 15:14:18 +08:00
parent e70863e2bd
commit 12971b7df3
7 changed files with 74 additions and 41 deletions
+2 -1
View File
@@ -297,7 +297,8 @@ TEST(ClientTest, Post) {
static webcc::fs::path GenerateTempFile(const std::string& data) {
try {
webcc::fs::path path = webcc::fs::temp_directory_path() / webcc::RandomString(10);
webcc::fs::path path =
webcc::fs::temp_directory_path() / webcc::RandomString(10);
webcc::fs::ofstream ofs;
ofs.open(path, std::ios::binary);
@@ -101,3 +101,28 @@ TEST_F(ClientTimeoutTest, Timeout) {
EXPECT_TRUE(!r);
EXPECT_TRUE(timeout);
}
// Test ClientSession::Cancel()
TEST_F(ClientTimeoutTest, SessionCancel) {
webcc::ClientSession session;
session.set_read_timeout(30);
bool canceled = false;
// Create a thread to cancel the session after 3 seconds.
std::thread t{ [&session, &canceled]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
canceled = session.Cancel();
} };
// Send a request and ask the server to sleep 5 seconds before reply.
try {
auto r = session.Send(WEBCC_GET("http://localhost/sleep/5").Port(kPort)());
} catch (const webcc::Error&) {
}
t.join();
// The request should be canceled.
EXPECT_TRUE(canceled);
}