-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ThreadContextMapSuite.java
130 lines (123 loc) · 5.56 KB
/
ThreadContextMapSuite.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* 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.test.spi;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.logging.log4j.spi.ThreadContextMap;
/**
* A simple suite of tests for {@link ThreadContextMap} implementations.
*/
public abstract class ThreadContextMapSuite {
private static final String KEY = "key";
/**
* Checks if new threads either inherit or do not inherit the context data of the current thread.
*
* @param threadContext A {@link ThreadContextMap implementation}.
* @param key The key to use.
* @param expectedValue The expected value on a new thread.
*/
protected static void assertThreadContextValueOnANewThread(
final ThreadContextMap threadContext, final String key, final String expectedValue) {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
assertThat(executorService.submit(() -> threadContext.get(key)))
.succeedsWithin(Duration.ofSeconds(1))
.isEqualTo(expectedValue);
} finally {
executorService.shutdown();
}
}
/**
* Ensures that {@code save/restore} works correctly on the current thread.
*/
protected static void assertContextDataCanBeSavedAndRestored(final ThreadContextMap threadContext) {
final String externalValue = "externalValue";
final String internalValue = "internalValue";
final RuntimeException throwable = new RuntimeException();
threadContext.put(KEY, externalValue);
final Object saved = threadContext.save();
try {
threadContext.put(KEY, internalValue);
assertThat(threadContext.get(KEY)).isEqualTo(internalValue);
throw throwable;
} catch (final RuntimeException e) {
assertThat(e).isSameAs(throwable);
assertThat(threadContext.get(KEY)).isEqualTo(internalValue);
} finally {
threadContext.restore(saved);
}
assertThat(threadContext.get(KEY)).isEqualTo(externalValue);
}
/**
* Ensures that the context data obtained through {@link ThreadContextMap#save} can be safely transferred to another
* thread.
*
* @param threadContext The {@link ThreadContextMap} to test.
*/
protected static void assertContextDataCanBeTransferred(final ThreadContextMap threadContext) {
final String mainThreadValue = "mainThreadValue";
final String newThreadValue = "newThreadValue";
final RuntimeException throwable = new RuntimeException();
threadContext.put(KEY, mainThreadValue);
final Object mainThreadSaved = threadContext.save();
threadContext.remove(KEY);
// Move to new thread
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
assertThat(executorService.submit(() -> {
threadContext.put(KEY, newThreadValue);
final Object newThreadSaved = threadContext.restore(mainThreadSaved);
try {
assertThat(threadContext.get(KEY)).isEqualTo(mainThreadValue);
throw throwable;
} catch (final RuntimeException e) {
assertThat(e).isSameAs(throwable);
assertThat(threadContext.get(KEY)).isEqualTo(mainThreadValue);
} finally {
threadContext.restore(newThreadSaved);
}
assertThat(threadContext.get(KEY)).isEqualTo(newThreadValue);
}))
.succeedsWithin(Duration.ofSeconds(1));
} finally {
executorService.shutdown();
}
}
/**
* Ensures the {@code restore(null)} can be used to clear the context data.
*
* @param threadContext The {@link ThreadContextMap} to test.
*/
protected static void assertNullValueClearsTheContextData(final ThreadContextMap threadContext) {
final String value = "restoreAcceptsNull";
final RuntimeException throwable = new RuntimeException();
threadContext.put(KEY, value);
final Object saved = threadContext.restore(null);
try {
assertThat(threadContext.getImmutableMapOrNull()).isNullOrEmpty();
throw throwable;
} catch (final RuntimeException e) {
assertThat(e).isSameAs(throwable);
assertThat(threadContext.getImmutableMapOrNull()).isNullOrEmpty();
} finally {
threadContext.restore(saved);
}
assertThat(threadContext.get(KEY)).isEqualTo(value);
}
}