use of org.glassfish.diagnostics.context.ContextManager in project Payara by payara.
the class AContextImplContextPropagationIntegrationTest method testValuePropagationAndNonPropagation.
/**
*/
@Test
public void testValuePropagationAndNonPropagation() throws Exception {
final String propagatingKey = "propagatingKey";
final String propagatingValue = "propagatingValue";
final String nonPropagatingKey = "nonPropagatingKey";
final String nonPropagatingValue = "nonPropagatingValue";
final ContextManager contextManager = mContextManager;
final List<Throwable> exceptionList = new LinkedList<>();
final List<Thread> threadList = new LinkedList<>();
{
Context diagnosticsContextStart = mContextManager.getContext();
diagnosticsContextStart.put(propagatingKey, propagatingValue, true);
diagnosticsContextStart.put(nonPropagatingKey, nonPropagatingValue, false);
}
for (int i = 0; i < 17; i++) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
String threadName = currentThread().getName();
Context diagnosticsContext = contextManager.getContext();
assertEquals("The value associated with key " + propagatingKey + " on thread " + threadName + " is not as expected.", propagatingValue, diagnosticsContext.get(propagatingKey));
assertNull("The null value should be associated with key " + nonPropagatingKey + " on thread " + threadName, diagnosticsContext.get(nonPropagatingKey));
} catch (Throwable e) {
synchronized (exceptionList) {
exceptionList.add(e);
}
}
}
});
thread.setName("Child_" + i + "_of_parent_'" + currentThread().getName() + "'");
thread.start();
threadList.add(thread);
}
for (Thread thread : threadList) {
thread.join();
}
if (exceptionList.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Throwable e : exceptionList) {
sb.append("\n ").append(e.getMessage());
}
sb.append("\n");
// TODO: Enable this assertion when/if contextpropagation takes
// place to child thread.
/* Assert.fail("Compound failure: " + sb.toString()); */
}
}
Aggregations