Skip to content

Commit

Permalink
[TEST] Change is_called_ and got_response_ to use atomic (open-te…
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Dec 13, 2024
1 parent 490f882 commit 8b418fe
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions ext/test/http/curl_http_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,28 @@ class CustomEventHandler : public http_client::EventHandler
public:
void OnResponse(http_client::Response & /* response */) noexcept override
{
got_response_ = true;
got_response_.store(true, std::memory_order_release);
}
void OnEvent(http_client::SessionState state, nostd::string_view /* reason */) noexcept override
{
switch (state)
{
case http_client::SessionState::ConnectFailed:
case http_client::SessionState::SendFailed: {
is_called_ = true;
is_called_.store(true, std::memory_order_release);
break;
}
default:
break;
}
}

CustomEventHandler() : is_called_(false), got_response_(false) {}

~CustomEventHandler() override = default;
bool is_called_ = false;
bool got_response_ = false;

std::atomic<bool> is_called_;
std::atomic<bool> got_response_;
};

class GetEventHandler : public CustomEventHandler
Expand All @@ -59,8 +63,8 @@ class GetEventHandler : public CustomEventHandler
{
ASSERT_EQ(200, response.GetStatusCode());
ASSERT_EQ(response.GetBody().size(), 0);
is_called_ = true;
got_response_ = true;
is_called_.store(true, std::memory_order_release);
got_response_.store(true, std::memory_order_release);
}
};

Expand All @@ -71,8 +75,8 @@ class PostEventHandler : public CustomEventHandler
ASSERT_EQ(200, response.GetStatusCode());
std::string body(response.GetBody().begin(), response.GetBody().end());
ASSERT_EQ(body, "{'k1':'v1', 'k2':'v2', 'k3':'v3'}");
is_called_ = true;
got_response_ = true;
is_called_.store(true, std::memory_order_release);
got_response_.store(true, std::memory_order_release);
}
};

Expand All @@ -87,8 +91,8 @@ class FinishInCallbackHandler : public CustomEventHandler
{
ASSERT_EQ(200, response.GetStatusCode());
ASSERT_EQ(response.GetBody().size(), 0);
is_called_ = true;
got_response_ = true;
is_called_.store(true, std::memory_order_release);
got_response_.store(true, std::memory_order_release);

if (session_)
{
Expand Down Expand Up @@ -249,8 +253,8 @@ TEST_F(BasicCurlHttpTests, SendGetRequest)
session->SendRequest(handler);
ASSERT_TRUE(waitForRequests(30, 1));
session->FinishSession();
ASSERT_TRUE(handler->is_called_);
ASSERT_TRUE(handler->got_response_);
ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire));
ASSERT_TRUE(handler->got_response_.load(std::memory_order_acquire));
}

TEST_F(BasicCurlHttpTests, SendPostRequest)
Expand All @@ -272,8 +276,8 @@ TEST_F(BasicCurlHttpTests, SendPostRequest)
session->SendRequest(handler);
ASSERT_TRUE(waitForRequests(30, 1));
session->FinishSession();
ASSERT_TRUE(handler->is_called_);
ASSERT_TRUE(handler->got_response_);
ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire));
ASSERT_TRUE(handler->got_response_.load(std::memory_order_acquire));

session_manager->CancelAllSessions();
session_manager->FinishAllSessions();
Expand All @@ -291,8 +295,8 @@ TEST_F(BasicCurlHttpTests, RequestTimeout)
auto handler = std::make_shared<GetEventHandler>();
session->SendRequest(handler);
session->FinishSession();
ASSERT_TRUE(handler->is_called_);
ASSERT_FALSE(handler->got_response_);
ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire));
ASSERT_FALSE(handler->got_response_.load(std::memory_order_acquire));
}

TEST_F(BasicCurlHttpTests, CurlHttpOperations)
Expand Down Expand Up @@ -408,8 +412,8 @@ TEST_F(BasicCurlHttpTests, SendGetRequestAsync)
sessions[i]->FinishSession();
ASSERT_FALSE(sessions[i]->IsSessionActive());

ASSERT_TRUE(handlers[i]->is_called_);
ASSERT_TRUE(handlers[i]->got_response_);
ASSERT_TRUE(handlers[i]->is_called_.load(std::memory_order_acquire));
ASSERT_TRUE(handlers[i]->got_response_.load(std::memory_order_acquire));
}

http_client.WaitBackgroundThreadExit();
Expand Down Expand Up @@ -437,16 +441,17 @@ TEST_F(BasicCurlHttpTests, SendGetRequestAsyncTimeout)
// Lock mtx_requests to prevent response, we will check IsSessionActive() in the end
std::unique_lock<std::mutex> lock_requests(mtx_requests);
sessions[i]->SendRequest(handlers[i]);
ASSERT_TRUE(sessions[i]->IsSessionActive() || handlers[i]->is_called_);
ASSERT_TRUE(sessions[i]->IsSessionActive() ||
handlers[i]->is_called_.load(std::memory_order_acquire));
}

for (unsigned i = 0; i < batch_count; ++i)
{
sessions[i]->FinishSession();
ASSERT_FALSE(sessions[i]->IsSessionActive());

ASSERT_TRUE(handlers[i]->is_called_);
ASSERT_FALSE(handlers[i]->got_response_);
ASSERT_TRUE(handlers[i]->is_called_.load(std::memory_order_acquire));
ASSERT_FALSE(handlers[i]->got_response_.load(std::memory_order_acquire));
}
}

Expand Down Expand Up @@ -482,8 +487,8 @@ TEST_F(BasicCurlHttpTests, SendPostRequestAsync)
ASSERT_FALSE(session->IsSessionActive());
}

ASSERT_TRUE(handler->is_called_);
ASSERT_TRUE(handler->got_response_);
ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire));
ASSERT_TRUE(handler->got_response_.load(std::memory_order_acquire));

http_client.WaitBackgroundThreadExit();
}
Expand Down Expand Up @@ -521,8 +526,8 @@ TEST_F(BasicCurlHttpTests, FinishInAsyncCallback)
{
ASSERT_FALSE(sessions[i]->IsSessionActive());

ASSERT_TRUE(handlers[i]->is_called_);
ASSERT_TRUE(handlers[i]->got_response_);
ASSERT_TRUE(handlers[i]->is_called_.load(std::memory_order_acquire));
ASSERT_TRUE(handlers[i]->got_response_.load(std::memory_order_acquire));
}
}
}

0 comments on commit 8b418fe

Please sign in to comment.