diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 63cd983c1..f2b783116 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -21,7 +21,7 @@ ### Bug Fixes - Prevent pipeline of length zero to be created. -- Avoid re-using a connection when an uploading request fail when using CurlTransport. +- Avoid re-using a connection when a request to upload data fails while using the `CurlTransport`. ### New Features diff --git a/sdk/core/azure-core/test/ut/curl_session_test.cpp b/sdk/core/azure-core/test/ut/curl_session_test.cpp index b3b33559f..6b930b82d 100644 --- a/sdk/core/azure-core/test/ut/curl_session_test.cpp +++ b/sdk/core/azure-core/test/ut/curl_session_test.cpp @@ -6,6 +6,8 @@ #include #include +#include + using ::testing::_; using ::testing::DoAll; using ::testing::Return; @@ -78,4 +80,33 @@ namespace Azure { namespace Core { namespace Test { // Clear the connections from the pool to invoke clean routine Azure::Core::Http::CurlConnectionPool::ConnectionPoolIndex.clear(); } + + TEST_F(CurlSession, DoNotReuseConnectionIfDownloadFail) + { + + // Can't mock the curlMock directly from a unique ptr, heap allocate it first and then make a + // unique ptr for it + MockCurlNetworkConnection* curlMock = new MockCurlNetworkConnection(); + // mock an upload error + EXPECT_CALL(*curlMock, SendBuffer(_, _, _)).WillOnce(Return(CURLE_SEND_ERROR)); + EXPECT_CALL(*curlMock, DestructObj()); + + // Create the unique ptr to take care about memory free at the end + std::unique_ptr uniqueCurlMock(curlMock); + + // Simulate a request to be sent + Azure::Core::Http::Url url("http://microsoft.com"); + Azure::Core::Http::Request request(Azure::Core::Http::HttpMethod::Get, url); + + { + // Create the session inside scope so it is released and the connection is moved to the pool + auto session = std::make_unique( + request, std::move(uniqueCurlMock), true); + + auto returnCode = session->Perform(Azure::Core::GetApplicationContext()); + EXPECT_EQ(CURLE_SEND_ERROR, returnCode); + } + // Check connection pool is empty (connection was not moved to the pool) + EXPECT_EQ(Azure::Core::Http::CurlConnectionPool::ConnectionPoolIndex.size(), 0); + } }}} // namespace Azure::Core::Test