ramanathan1504 commented on code in PR #4157: URL: https://github.com/apache/logging-log4j2/pull/4157#discussion_r3594521680
########## log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java: ########## @@ -0,0 +1,156 @@ +/* + * 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. + */ +package org.apache.logging.log4j.core.util.internal; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.net.URI; +import java.util.Map; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.message.MessageFactory; +import org.apache.logging.log4j.message.ParameterizedMessageFactory; +import org.apache.logging.log4j.message.SimpleMessageFactory; +import org.apache.logging.log4j.plugins.di.DI; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; + +class InternalLoggerRegistryTest { + private LoggerContext loggerContext; + private InternalLoggerRegistry registry; + + @BeforeEach + void setUp(final TestInfo testInfo) throws Exception { + loggerContext = new LoggerContext( + testInfo.getDisplayName(), null, (URI) null, DI.createInitializedFactory()); + final Field registryField = LoggerContext.class.getDeclaredField("loggerRegistry"); + registryField.setAccessible(true); + registry = (InternalLoggerRegistry) registryField.get(loggerContext); + } + + @AfterEach + void tearDown() { + if (loggerContext != null) { + loggerContext.stop(); + } + } + + @Test + void testGetLoggerReturnsNullForNonExistentLogger() { + assertNull(registry.getLogger("nonExistent", null)); + } + + @Test + void testComputeIfAbsentCreatesLogger() { + final Logger logger = loggerContext.getLogger("testLogger", null); + assertNotNull(logger); + assertEquals("testLogger", logger.getName()); + } + + @Test + void testGetLoggerRetrievesExistingLogger() { + final Logger logger = loggerContext.getLogger("testLogger", null); + assertSame(logger, registry.getLogger("testLogger", null)); + } + + @Test + void testHasLoggerReturnsCorrectStatus() { Review Comment: @vpelikh , thanks for combining these two ports! I pulled this down to test it locally (running ./mvnw test -pl :log4j-core-test -Dtest=InternalLoggerRegistryTest), and the tests are currently failing on main. It looks like loggers aren't being successfully stored/retrieved, and the internal map might not be initialized: testGetLoggerRetrievesExistingLogger returns null testHasLoggerReturnsCorrectStatus returns false testExpungeStaleWeakReferenceEntries throws a NullPointerException because loggerRefByName is null. Could you please take a look, fix the logic for Log4j 3, and ensure the tests pass locally? Let me know when it's updated and I will happily review it again! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
