Skip to content

Commit

Permalink
improve timeout duration (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 10, 2024
1 parent c74feaf commit d501a21
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
: self(self) {
self->socket_->is_timeout_ = false;

if (self->enable_timeout_) {
if (self->enable_timeout_ && duration.count() >= 0) {
self->timeout(self->timer_, duration, std::move(msg))
.start([](auto &&) {
});
Expand Down Expand Up @@ -1972,6 +1972,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

async_simple::coro::Lazy<resp_data> connect(const uri_t &u) {
if (socket_->has_closed_) {
socket_->is_timeout_ = false;
host_ = proxy_host_.empty() ? u.get_host() : proxy_host_;
port_ = proxy_port_.empty() ? u.get_port() : proxy_port_;
if (auto ec = co_await coro_io::async_connect(
Expand Down
36 changes: 36 additions & 0 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ TEST_CASE("test for gzip") {
CHECK(result.resp_body == "hello world");
}

{
coro_http_client client{};
client.add_header("Content-Encoding", "none");
client.set_conn_timeout(0ms);
std::string uri = "http://127.0.0.1:8090/none";
auto result = async_simple::coro::syncAwait(client.connect(uri));
if (result.net_err)
CHECK(result.net_err == std::errc::timed_out);

client.set_conn_timeout(-1ms);
client.set_req_timeout(0ms);
result = async_simple::coro::syncAwait(client.connect(uri));
if (result.net_err)
CHECK(!result.net_err);

result = async_simple::coro::syncAwait(client.async_get("/none"));
if (result.net_err)
CHECK(result.net_err == std::errc::timed_out);

client.add_header("Content-Encoding", "none");
client.set_req_timeout(-1ms);
result = async_simple::coro::syncAwait(client.async_get(uri));
CHECK(!result.net_err);
client.add_header("Content-Encoding", "none");
result = async_simple::coro::syncAwait(client.async_get(uri));
CHECK(!result.net_err);

client.add_header("Content-Encoding", "none");
coro_http_client::config conf{};
conf.req_timeout_duration = 0ms;
client.init_config(conf);
result = async_simple::coro::syncAwait(client.async_get(uri));
if (result.net_err)
CHECK(result.net_err == std::errc::timed_out);
}

{
coro_http_client client{};
std::string uri = "http://127.0.0.1:8090/deflate";
Expand Down

0 comments on commit d501a21

Please sign in to comment.