Skip to content

Commit 041e5eb

Browse files
committed
Add Unit test
1 parent 9498a0d commit 041e5eb

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.spi;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.lang.ref.WeakReference;
22+
import java.util.stream.Stream;
23+
import org.apache.logging.log4j.message.MessageFactory;
24+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
25+
import org.apache.logging.log4j.message.ReusableMessageFactory;
26+
import org.apache.logging.log4j.test.TestLogger;
27+
import org.jspecify.annotations.Nullable;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.MethodSource;
30+
31+
class LoggerRegistryTest {
32+
33+
private static final String LOGGER_NAME = LoggerRegistryTest.class.getName();
34+
35+
static Stream<@Nullable MessageFactory> doesNotLoseLoggerReferences() {
36+
return Stream.of(
37+
ParameterizedMessageFactory.INSTANCE,
38+
ReusableMessageFactory.INSTANCE,
39+
new ParameterizedMessageFactory(),
40+
null);
41+
}
42+
43+
/**
44+
* @see <a href="https://github.com/apache/logging-log4j2/issues/3143>Issue #3143</a>
45+
*/
46+
@ParameterizedTest
47+
@MethodSource
48+
void doesNotLoseLoggerReferences(@Nullable MessageFactory messageFactory) {
49+
LoggerRegistry<TestLogger> loggerRegistry = new LoggerRegistry<>();
50+
TestLogger logger = new TestLogger(LOGGER_NAME, messageFactory);
51+
WeakReference<TestLogger> loggerRef = new WeakReference<>(logger);
52+
// Register logger
53+
loggerRegistry.putIfAbsent(LOGGER_NAME, messageFactory, logger);
54+
// The JIT compiler/optimizer might figure out by himself the `logger` and `messageFactory` are no longer used:
55+
// https://shipilev.net/jvm/anatomy-quarks/8-local-var-reachability/
56+
// We help him with the task though.
57+
logger = null;
58+
// Trigger a GC run
59+
System.gc();
60+
// Check if the logger is still there
61+
assertThat(loggerRef.get()).isNotNull();
62+
assertThat(loggerRegistry.getLogger(LOGGER_NAME, messageFactory)).isInstanceOf(TestLogger.class);
63+
}
64+
}

0 commit comments

Comments
 (0)