Skip to content

Commit bb327eb

Browse files
authored
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
1 parent eaab902 commit bb327eb

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/main/cpp/asyncappender.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,13 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
310310
// Write to the ring buffer
311311
priv->buffer[index] = AsyncAppenderPriv::EventData{event, pendingCount};
312312
// Notify the dispatch thread that an event has been added
313+
auto failureCount = 0;
313314
auto savedEventCount = oldEventCount;
314315
while (!priv->commitCount.compare_exchange_weak(oldEventCount, oldEventCount + 1, std::memory_order_release))
315316
{
316-
oldEventCount = savedEventCount;
317+
oldEventCount = savedEventCount;
318+
if (2 < ++failureCount) // Did the scheduler suspend a thread between claiming a slot and advancing commitCount?
319+
std::this_thread::yield(); // Wait a bit
317320
}
318321
priv->bufferNotEmpty.notify_all();
319322
break;

src/test/cpp/asyncappendertestcase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#define NOMINMAX
1819
#include "logunit.h"
1920

2021
#include <log4cxx/logger.h>
@@ -235,7 +236,7 @@ class AsyncAppenderTestCase : public AppenderSkeletonTestCase
235236
void testMultiThread()
236237
{
237238
int LEN = 2000; // Larger than default buffer size (128)
238-
int threadCount = 6;
239+
auto threadCount = std::max(static_cast<int>(std::thread::hardware_concurrency() - 1), 2);
239240
auto root = Logger::getRootLogger();
240241
auto vectorAppender = std::make_shared<VectorAppender>();
241242
auto asyncAppender = std::make_shared<AsyncAppender>();

src/test/cpp/terminationtestcase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ LOGUNIT_CLASS(TerminationTestCase)
6363
{
6464
auto root = getLogger();
6565
LOG4CXX_INFO(root, "Message");
66-
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
66+
std::this_thread::sleep_for( std::chrono::milliseconds( 30 ) );
6767
const std::vector<spi::LoggingEventPtr>& v = vectorAppender->getVector();
6868
LOGUNIT_ASSERT_EQUAL((size_t) 1, v.size());
6969
}

0 commit comments

Comments
 (0)