You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2024/10/17 03:03:53 UTC

(logging-log4cxx) branch master updated: Prevent asyncappender test timeout (#416)

This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new bb327ebb Prevent asyncappender test timeout (#416)
bb327ebb is described below

commit bb327ebbd0c75d5dad779502232474764c04c47e
Author: Stephen Webb <st...@ieee.org>
AuthorDate: Thu Oct 17 14:03:48 2024 +1100

    Prevent asyncappender test timeout (#416)
    
    * Help the scheduler when there are more threads than physical cores
    
    * The 10 ms expectation in termination test is unreasonably short
---
 src/main/cpp/asyncappender.cpp         | 5 ++++-
 src/test/cpp/asyncappendertestcase.cpp | 3 ++-
 src/test/cpp/terminationtestcase.cpp   | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp
index cd302f62..4205e557 100644
--- a/src/main/cpp/asyncappender.cpp
+++ b/src/main/cpp/asyncappender.cpp
@@ -310,10 +310,13 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
 			// Write to the ring buffer
 			priv->buffer[index] = AsyncAppenderPriv::EventData{event, pendingCount};
 			// Notify the dispatch thread that an event has been added
+			auto failureCount = 0;
 			auto savedEventCount = oldEventCount;
 			while (!priv->commitCount.compare_exchange_weak(oldEventCount, oldEventCount + 1, std::memory_order_release))
 			{
-				 oldEventCount = savedEventCount;
+				oldEventCount = savedEventCount;
+				if (2 < ++failureCount) // Did the scheduler suspend a thread between claiming a slot and advancing commitCount?
+					std::this_thread::yield(); // Wait a bit
 			}
 			priv->bufferNotEmpty.notify_all();
 			break;
diff --git a/src/test/cpp/asyncappendertestcase.cpp b/src/test/cpp/asyncappendertestcase.cpp
index bffa5e73..ea6781fc 100644
--- a/src/test/cpp/asyncappendertestcase.cpp
+++ b/src/test/cpp/asyncappendertestcase.cpp
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+#define NOMINMAX
 #include "logunit.h"
 
 #include <log4cxx/logger.h>
@@ -235,7 +236,7 @@ class AsyncAppenderTestCase : public AppenderSkeletonTestCase
 		void testMultiThread()
 		{
 			int LEN = 2000; // Larger than default buffer size (128)
-			int threadCount = 6;
+			auto threadCount = std::max(static_cast<int>(std::thread::hardware_concurrency() - 1), 2);
 			auto root = Logger::getRootLogger();
 			auto vectorAppender = std::make_shared<VectorAppender>();
 			auto asyncAppender = std::make_shared<AsyncAppender>();
diff --git a/src/test/cpp/terminationtestcase.cpp b/src/test/cpp/terminationtestcase.cpp
index 4c62bb92..548d979f 100644
--- a/src/test/cpp/terminationtestcase.cpp
+++ b/src/test/cpp/terminationtestcase.cpp
@@ -63,7 +63,7 @@ public:
 	{
 		auto root = getLogger();
 		LOG4CXX_INFO(root, "Message");
-		std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
+		std::this_thread::sleep_for( std::chrono::milliseconds( 30 ) );
 		const std::vector<spi::LoggingEventPtr>& v = vectorAppender->getVector();
 		LOGUNIT_ASSERT_EQUAL((size_t) 1, v.size());
 	}