mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-15 04:15:30 +07:00
add is_done/is_pending functions to request
This commit is contained in:
@@ -140,6 +140,9 @@ namespace curly_hpp
|
||||
bool cancel() noexcept;
|
||||
statuses status() const noexcept;
|
||||
|
||||
bool is_done() const noexcept;
|
||||
bool is_pending() const noexcept;
|
||||
|
||||
statuses wait() const noexcept;
|
||||
statuses wait_for(time_ms_t ms) const noexcept;
|
||||
statuses wait_until(time_point_t tp) const noexcept;
|
||||
|
||||
@@ -509,6 +509,16 @@ namespace curly_hpp
|
||||
return status_;
|
||||
}
|
||||
|
||||
bool is_done() const noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return status_ == statuses::done;
|
||||
}
|
||||
|
||||
bool is_pending() const noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return status_ == statuses::pending;
|
||||
}
|
||||
|
||||
statuses wait() const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait(lock, [this](){
|
||||
@@ -664,6 +674,14 @@ namespace curly_hpp
|
||||
return state_->status();
|
||||
}
|
||||
|
||||
bool request::is_done() const noexcept {
|
||||
return state_->is_done();
|
||||
}
|
||||
|
||||
bool request::is_pending() const noexcept {
|
||||
return state_->is_pending();
|
||||
}
|
||||
|
||||
request::statuses request::wait() const noexcept {
|
||||
return state_->wait();
|
||||
}
|
||||
@@ -939,7 +957,7 @@ namespace curly_hpp
|
||||
|
||||
curl_state::with([](CURLM* curlm){
|
||||
for ( auto iter = active_handles.begin(); iter != active_handles.end(); ) {
|
||||
if ( (*iter)->status() != request::statuses::pending ) {
|
||||
if ( !(*iter)->is_pending() ) {
|
||||
(*iter)->dequeue(curlm);
|
||||
iter = active_handles.erase(iter);
|
||||
} else {
|
||||
|
||||
@@ -109,6 +109,31 @@ TEST_CASE("curly") {
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("is_done/is_pending") {
|
||||
{
|
||||
auto req = net::request_builder(net::methods::get)
|
||||
.url("https://httpbin.org/delay/1")
|
||||
.send();
|
||||
REQUIRE_FALSE(req.is_done());
|
||||
REQUIRE(req.is_pending());
|
||||
req.wait();
|
||||
REQUIRE(req.is_done());
|
||||
REQUIRE_FALSE(req.is_pending());
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder(net::methods::post, "http://www.httpbin.org/post")
|
||||
.url("https://httpbin.org/delay/2")
|
||||
.request_timeout(net::time_sec_t(1))
|
||||
.send();
|
||||
REQUIRE_FALSE(req.is_done());
|
||||
REQUIRE(req.is_pending());
|
||||
req.wait();
|
||||
REQUIRE_FALSE(req.is_done());
|
||||
REQUIRE_FALSE(req.is_pending());
|
||||
REQUIRE(!req.get_error().empty());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("get") {
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/status/204").send();
|
||||
|
||||
Reference in New Issue
Block a user