Skip to content

Commit b26bb88

Browse files
jengebrppkarwasz
authored andcommitted
Creating faster ThreadContextMap based on a String[] in the ThreadLocal
1 parent 3ccdbcc commit b26bb88

File tree

7 files changed

+1314
-3
lines changed

7 files changed

+1314
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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.junit.Assert.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import org.apache.logging.log4j.test.junit.InitializesThreadContext;
28+
import org.apache.logging.log4j.test.junit.SetTestProperty;
29+
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
30+
import org.junit.jupiter.api.Test;
31+
import org.junitpioneer.jupiter.ClearSystemProperty;
32+
33+
/**
34+
* Tests the {@code StringArrayThreadContextMap} class.
35+
*/
36+
@UsingThreadContextMap
37+
public class StringArrayThreadContextMapTest {
38+
39+
@Test
40+
public void testEqualsVsSameKind() {
41+
final StringArrayThreadContextMap map1 = createMap();
42+
final StringArrayThreadContextMap map2 = createMap();
43+
assertEquals(map1, map1);
44+
assertEquals(map2, map2);
45+
assertEquals(map1, map2);
46+
assertEquals(map2, map1);
47+
}
48+
49+
@Test
50+
public void testHashCodeVsSameKind() {
51+
final StringArrayThreadContextMap map1 = createMap();
52+
final StringArrayThreadContextMap map2 = createMap();
53+
assertEquals(map1.hashCode(), map2.hashCode());
54+
}
55+
56+
@Test
57+
public void testGet() {
58+
final StringArrayThreadContextMap map1 = createMap();
59+
assertEquals(null, map1.get("test"));
60+
map1.put("test", "test");
61+
assertEquals("test", map1.get("test"));
62+
assertEquals(null, map1.get("not_present"));
63+
}
64+
65+
@Test
66+
public void testDoesNothingIfConstructedWithUseMapIsFalse() {
67+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(false);
68+
assertTrue(map.isEmpty());
69+
assertFalse(map.containsKey("key"));
70+
map.put("key", "value");
71+
72+
assertTrue(map.isEmpty());
73+
assertFalse(map.containsKey("key"));
74+
assertNull(map.get("key"));
75+
}
76+
77+
@Test
78+
public void testPut() {
79+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
80+
assertTrue(map.isEmpty());
81+
assertFalse(map.containsKey("key"));
82+
map.put("key", "value");
83+
84+
assertFalse(map.isEmpty());
85+
assertTrue(map.containsKey("key"));
86+
assertEquals("value", map.get("key"));
87+
}
88+
89+
@Test
90+
public void testPutAll() {
91+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
92+
assertTrue(map.isEmpty());
93+
assertFalse(map.containsKey("key"));
94+
final int mapSize = 10;
95+
final Map<String, String> newMap = new HashMap<>(mapSize);
96+
for (int i = 1; i <= mapSize; i++) {
97+
newMap.put("key" + i, "value" + i);
98+
}
99+
map.putAll(newMap);
100+
assertFalse(map.isEmpty());
101+
for (int i = 1; i <= mapSize; i++) {
102+
assertTrue(map.containsKey("key" + i));
103+
assertEquals("value" + i, map.get("key" + i));
104+
}
105+
}
106+
107+
/**
108+
* Test method for
109+
* {@link org.apache.logging.log4j.spi.StringArrayThreadContextMap#remove(java.lang.String)}
110+
* .
111+
*/
112+
@Test
113+
public void testRemove() {
114+
final StringArrayThreadContextMap map = createMap();
115+
assertEquals("value", map.get("key"));
116+
assertEquals("value2", map.get("key2"));
117+
118+
map.remove("key");
119+
assertFalse(map.containsKey("key"));
120+
assertEquals("value2", map.get("key2"));
121+
}
122+
123+
@Test
124+
public void testRemoveAll() {
125+
final StringArrayThreadContextMap map = createMap();
126+
127+
Map<String, String> newValues = new HashMap<>();
128+
newValues.put("1", "value1");
129+
newValues.put("2", "value2");
130+
131+
map.putAll(newValues);
132+
map.removeAll(newValues.keySet());
133+
134+
map.put("3", "value3");
135+
}
136+
137+
@Test
138+
public void testClear() {
139+
final StringArrayThreadContextMap map = createMap();
140+
141+
map.clear();
142+
assertTrue(map.isEmpty());
143+
assertFalse(map.containsKey("key"));
144+
assertFalse(map.containsKey("key2"));
145+
}
146+
147+
/**
148+
* @return
149+
*/
150+
private StringArrayThreadContextMap createMap() {
151+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
152+
assertTrue(map.isEmpty());
153+
map.put("key", "value");
154+
map.put("key2", "value2");
155+
assertEquals("value", map.get("key"));
156+
assertEquals("value2", map.get("key2"));
157+
return map;
158+
}
159+
160+
@Test
161+
public void testGetCopyReturnsMutableMap() {
162+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
163+
assertTrue(map.isEmpty());
164+
final Map<String, String> copy = map.getCopy();
165+
assertTrue(copy.isEmpty());
166+
167+
copy.put("key", "value"); // mutable
168+
assertEquals("value", copy.get("key"));
169+
170+
// thread context map not affected
171+
assertTrue(map.isEmpty());
172+
}
173+
174+
@Test
175+
public void testGetCopyReturnsMutableCopy() {
176+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
177+
map.put("key1", "value1");
178+
assertFalse(map.isEmpty());
179+
final Map<String, String> copy = map.getCopy();
180+
assertEquals("value1", copy.get("key1")); // copy has values too
181+
182+
copy.put("key", "value"); // copy is mutable
183+
assertEquals("value", copy.get("key"));
184+
185+
// thread context map not affected
186+
assertFalse(map.containsKey("key"));
187+
188+
// clearing context map does not affect copy
189+
map.clear();
190+
assertTrue(map.isEmpty());
191+
192+
assertFalse(copy.isEmpty());
193+
}
194+
195+
@Test
196+
public void testGetImmutableMapReturnsNullIfEmpty() {
197+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
198+
assertTrue(map.isEmpty());
199+
assertNull(map.getImmutableMapOrNull());
200+
}
201+
202+
@Test
203+
public void testGetImmutableMapReturnsImmutableMapIfNonEmpty() {
204+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
205+
map.put("key1", "value1");
206+
assertFalse(map.isEmpty());
207+
208+
final Map<String, String> immutable = map.getImmutableMapOrNull();
209+
assertEquals("value1", immutable.get("key1")); // copy has values too
210+
211+
// immutable
212+
assertThrows(UnsupportedOperationException.class, () -> immutable.put("key", "value"));
213+
}
214+
215+
@Test
216+
public void testGetImmutableMapCopyNotAffectdByContextMapChanges() {
217+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
218+
map.put("key1", "value1");
219+
assertFalse(map.isEmpty());
220+
221+
final Map<String, String> immutable = map.getImmutableMapOrNull();
222+
assertEquals("value1", immutable.get("key1")); // copy has values too
223+
224+
// clearing context map does not affect copy
225+
map.clear();
226+
assertTrue(map.isEmpty());
227+
228+
assertFalse(immutable.isEmpty());
229+
}
230+
231+
@Test
232+
public void testToStringShowsMapContext() {
233+
final StringArrayThreadContextMap map = new StringArrayThreadContextMap(true);
234+
assertEquals("{}", map.toString());
235+
236+
map.put("key1", "value1");
237+
assertEquals("{key1=value1}", map.toString());
238+
239+
map.remove("key1");
240+
map.put("key2", "value2");
241+
assertEquals("{key2=value2}", map.toString());
242+
}
243+
244+
@Test
245+
@ClearSystemProperty(key = StringArrayThreadContextMap.INHERITABLE_MAP)
246+
@InitializesThreadContext
247+
public void testThreadLocalNotInheritableByDefault() {
248+
ThreadContextMapFactory.init();
249+
final ThreadLocal<Object[]> threadLocal = StringArrayThreadContextMap.createThreadLocalMap(true);
250+
assertFalse(threadLocal instanceof InheritableThreadLocal<?>);
251+
}
252+
253+
@Test
254+
@SetTestProperty(key = StringArrayThreadContextMap.INHERITABLE_MAP, value = "true")
255+
@InitializesThreadContext
256+
public void testThreadLocalInheritableIfConfigured() {
257+
ThreadContextMapFactory.init();
258+
try {
259+
final ThreadLocal<Object[]> threadLocal = StringArrayThreadContextMap.createThreadLocalMap(true);
260+
assertTrue(threadLocal instanceof InheritableThreadLocal<?>);
261+
} finally {
262+
System.clearProperty(StringArrayThreadContextMap.INHERITABLE_MAP);
263+
}
264+
}
265+
}

0 commit comments

Comments
 (0)