diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 71dd9519..e98e79b2 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -180,15 +180,6 @@ class coro_http_client : public std::enable_shared_from_this { ~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_; } @@ -683,7 +674,13 @@ class coro_http_client : public std::enable_shared_from_this { void reset() { if (!has_closed()) { - close_socket(*socket_); + std::promise 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()}; @@ -723,6 +720,15 @@ class coro_http_client : public std::enable_shared_from_this { 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 send_file_copy_with_chunked( std::string_view source, std::error_code &ec) { std::string file_data; @@ -2343,6 +2349,7 @@ class coro_http_client : public std::enable_shared_from_this { } 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); diff --git a/press_tool/main.cpp b/press_tool/main.cpp index feddb378..c13d13f5 100644 --- a/press_tool/main.cpp +++ b/press_tool/main.cpp @@ -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(); } } }); diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index e71c2a78..e3c568c8 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -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(); } @@ -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") { @@ -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( @@ -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); diff --git a/tests/test_cinatra_websocket.cpp b/tests/test_cinatra_websocket.cpp index 8f817c28..6ee80687 100644 --- a/tests/test_cinatra_websocket.cpp +++ b/tests/test_cinatra_websocket.cpp @@ -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 @@ -439,6 +437,5 @@ TEST_CASE("test websocket permessage defalte") { std::this_thread::sleep_for(std::chrono::milliseconds(300)); server.stop(); - client.close(); } #endif