Skip to content

Commit

Permalink
Fix for reset (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 17, 2025
1 parent 6ee8f04 commit 857e144
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
27 changes: 17 additions & 10 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

~coro_http_client() { close(); }

void close() {
if (socket_ == nullptr || socket_->has_closed_)
return;

asio::dispatch(executor_wrapper_.get_asio_executor(), [socket = socket_] {
close_socket(*socket);
});
}

coro_io::ExecutorWrapper<> &get_executor() { return executor_wrapper_; }

const config &get_config() { return config_; }
Expand Down Expand Up @@ -683,7 +674,13 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

void reset() {
if (!has_closed()) {
close_socket(*socket_);
std::promise<void> promise;
asio::dispatch(executor_wrapper_.get_asio_executor(),
[&promise, socket = socket_] {
close_socket(*socket);
promise.set_value();
});
promise.get_future().wait();
}

socket_->impl_ = asio::ip::tcp::socket{executor_wrapper_.context()};
Expand Down Expand Up @@ -723,6 +720,15 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string_view get_port() { return port_; }

private:
void close() {
if (socket_ == nullptr || socket_->has_closed_)
return;

asio::dispatch(executor_wrapper_.get_asio_executor(), [socket = socket_] {
close_socket(*socket);
});
}

async_simple::coro::Lazy<void> send_file_copy_with_chunked(
std::string_view source, std::error_code &ec) {
std::string file_data;
Expand Down Expand Up @@ -2343,6 +2349,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}

static void close_socket(socket_t &socket) {
assert(*coro_io::get_current() == &socket.impl_.get_executor().context());
std::error_code ec;
socket.impl_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket.impl_.close(ec);
Expand Down
2 changes: 1 addition & 1 deletion press_tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int main(int argc, char* argv[]) {
for (auto& counter : v) {
for (auto& conn : counter.conns) {
conn->set_bench_stop();
conn->close();
conn->reset();
}
}
});
Expand Down
24 changes: 17 additions & 7 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,8 +1704,6 @@ TEST_CASE("test upload file") {
"http//badurl.com", "test_not_exist_file", not_exist_file));
CHECK(result.status == 404);

client.close();

server.stop();
}

Expand Down Expand Up @@ -2544,15 +2542,18 @@ TEST_CASE("test coro_http_client not exist domain and bad uri") {

TEST_CASE("test coro_http_client async_get") {
coro_http_client client{};
client.set_conn_timeout(1s);
auto r =
async_simple::coro::syncAwait(client.async_get("http://www.baidu.com"));
CHECK(!r.net_err);
CHECK(r.status < 400);
if (!r.net_err) {
CHECK(r.status < 400);
}

auto r1 =
async_simple::coro::syncAwait(client.async_get("http://www.baidu.com"));
CHECK(!r1.net_err);
CHECK(r1.status == 200);
if (!r.net_err) {
CHECK(r.status < 400);
}
}

TEST_CASE("test basic http request") {
Expand Down Expand Up @@ -2694,7 +2695,7 @@ TEST_CASE("test inject failed") {
ret = client1.get(uri);
CHECK(ret.status != 200);

client1.close();
client1.reset();
std::string out;
out.resize(2024);
ret = async_simple::coro::syncAwait(
Expand Down Expand Up @@ -2904,6 +2905,15 @@ TEST_CASE("test coro_http_client no scheme still send request check") {
auto resp = async_simple::coro::syncAwait(client.async_get("127.0.0.1:8090"));
CHECK(!resp.net_err);
CHECK(resp.status == 200);
client.reset();
resp = async_simple::coro::syncAwait(client.async_get("127.0.0.1:8090"));
CHECK(!resp.net_err);
CHECK(resp.status == 200);
client.reset();
resp = async_simple::coro::syncAwait(client.async_get("127.0.0.1:8090"));
CHECK(!resp.net_err);
CHECK(resp.status == 200);

resp = async_simple::coro::syncAwait(
client.async_get("127.0.0.1:8090/ref='http://www.baidu.com'"));
CHECK(resp.status == 404);
Expand Down
3 changes: 0 additions & 3 deletions tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ TEST_CASE("test wss client") {
auto data = async_simple::coro::syncAwait(client.read_websocket());
CHECK(data.resp_body == "hello");

client.close();

server.stop();
}
#endif
Expand Down Expand Up @@ -439,6 +437,5 @@ TEST_CASE("test websocket permessage defalte") {
std::this_thread::sleep_for(std::chrono::milliseconds(300));

server.stop();
client.close();
}
#endif

0 comments on commit 857e144

Please sign in to comment.