Skip to content

Commit

Permalink
Prevent asyncappender test timeout (#416)
Browse files Browse the repository at this point in the history
* Help the scheduler when there are more threads than physical cores

* The 10 ms expectation in termination test is unreasonably short
  • Loading branch information
swebb2066 authored Oct 17, 2024
1 parent eaab902 commit bb327eb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/test/cpp/asyncappendertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#define NOMINMAX
#include "logunit.h"

#include <log4cxx/logger.h>
Expand Down Expand Up @@ -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>();
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/terminationtestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ LOGUNIT_CLASS(TerminationTestCase)
{
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());
}
Expand Down

0 comments on commit bb327eb

Please sign in to comment.