Skip to content

Commit

Permalink
[TEST] Change is_called_ and got_response_ to use atomic (#3204)
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));
}
}
}

1 comment on commit 8b418fe

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 8b418fe Previous: 490f882 Ratio
BM_SpinLockThrashing/2/process_time/real_time 0.5413380918334654 ms/iter 0.19680344119561316 ms/iter 2.75
BM_ProcYieldSpinLockThrashing/1/process_time/real_time 0.2443228327931034 ms/iter 0.10329443141086574 ms/iter 2.37
BM_ProcYieldSpinLockThrashing/4/process_time/real_time 1.6405725479125977 ms/iter 0.7012647887070974 ms/iter 2.34

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.