Skip to content

Commit 28e14b2

Browse files
committed
Add unit test for MDC restoration.
1 parent a741b63 commit 28e14b2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

mdc-propagation/src/test/java/nl/talsmasoftware/context/mdc/MdcManagerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package nl.talsmasoftware.context.mdc;
1717

18+
import nl.talsmasoftware.context.Context;
19+
import nl.talsmasoftware.context.ContextManagers;
20+
import nl.talsmasoftware.context.ContextSnapshot;
1821
import nl.talsmasoftware.context.executors.ContextAwareExecutorService;
1922
import org.junit.After;
2023
import org.junit.Before;
@@ -46,6 +49,12 @@ public void shutdownThreadpool() {
4649
threadpool = null;
4750
}
4851

52+
@Before
53+
@After
54+
public void clearMDC() {
55+
MDC.clear();
56+
}
57+
4958
private static final Callable<String> GET_MDC_ITEM = new Callable<String>() {
5059
public String call() {
5160
return MDC.get("mdc-item");
@@ -59,4 +68,21 @@ public void testMdcItemPropagation() throws ExecutionException, InterruptedExcep
5968
assertThat(itemValue.get(), is("Item value"));
6069
}
6170

71+
@Test
72+
public void testMdcItemRestoration() throws Exception {
73+
MDC.put("mdc-item", "Value 1");
74+
75+
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
76+
assertThat("New snapshot shouldn't manipulate MDC.", GET_MDC_ITEM.call(), is("Value 1"));
77+
78+
MDC.put("mdc-item", "Value 2");
79+
assertThat("Sanity check: MDC changed?", GET_MDC_ITEM.call(), is("Value 2"));
80+
81+
Context<Void> reactivation = snapshot.reactivate();
82+
assertThat("MDC changed by reactivation", GET_MDC_ITEM.call(), is("Value 1"));
83+
84+
reactivation.close();
85+
assertThat("MDC restored?", GET_MDC_ITEM.call(), is("Value 2"));
86+
}
87+
6288
}

0 commit comments

Comments
 (0)