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/04/28 01:28:21 UTC

(logging-log4cxx) 01/01: Add a simple termination test using AsyncAppender

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

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

commit 48b45360cfa0aa121808a0932911eae765b0f7b2
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Sun Apr 28 11:27:56 2024 +1000

    Add a simple termination test using AsyncAppender
---
 src/test/cpp/CMakeLists.txt          |  1 +
 src/test/cpp/terminationtestcase.cpp | 69 ++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt
index 47918f2b..19dcabb1 100644
--- a/src/test/cpp/CMakeLists.txt
+++ b/src/test/cpp/CMakeLists.txt
@@ -61,6 +61,7 @@ set(ALL_LOG4CXX_TESTS
     streamtestcase
     locationtest
     locationdisabledtest
+    terminationtestcase
 )
 if(${ENABLE_FMT_LAYOUT})
     set(ALL_LOG4CXX_TESTS ${ALL_LOG4CXX_TESTS} fmttest)
diff --git a/src/test/cpp/terminationtestcase.cpp b/src/test/cpp/terminationtestcase.cpp
new file mode 100644
index 00000000..d7bfda84
--- /dev/null
+++ b/src/test/cpp/terminationtestcase.cpp
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "logunit.h"
+
+#include <log4cxx/logger.h>
+#include <log4cxx/logmanager.h>
+#include "vectorappender.h"
+#include <log4cxx/asyncappender.h>
+
+using namespace log4cxx;
+
+static VectorAppenderPtr vectorAppender = std::make_shared<VectorAppender>();
+
+LOGUNIT_CLASS(TerminationTestCase)
+{
+	LOGUNIT_TEST_SUITE(TerminationTestCase);
+	LOGUNIT_TEST(logOnce);
+	LOGUNIT_TEST_SUITE_END();
+
+public:
+
+	static void setDefaultAppender()
+	{
+		auto r = LogManager::getLoggerRepository();
+		r->ensureIsConfigured([r]()
+			{
+			auto asyncAppender = std::make_shared<AsyncAppender>();
+			asyncAppender->addAppender(vectorAppender);
+			r->getRootLogger()->addAppender(asyncAppender);
+			}
+		);
+	}
+
+	static LoggerPtr getLogger(const LogString& name = LogString())
+	{
+		static struct initializer
+		{
+			initializer() { setDefaultAppender(); }
+			~initializer() { LogManager::shutdown(); }
+		} x;
+		auto r = LogManager::getLoggerRepository();
+		return r->getLogger(name);
+	}
+
+	void logOnce()
+	{
+		auto root = getLogger();
+		LOG4CXX_INFO(root, "Message");
+		std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
+		const std::vector<spi::LoggingEventPtr>& v = vectorAppender->getVector();
+		LOGUNIT_ASSERT_EQUAL((size_t) 1, v.size());
+	}
+};
+
+LOGUNIT_TEST_SUITE_REGISTRATION(TerminationTestCase);