Search in sources :

Example 1 with ContextManager

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()); */
    }
}
Also used : Context(org.glassfish.diagnostics.context.Context) ContextManager(org.glassfish.diagnostics.context.ContextManager) LinkedList(java.util.LinkedList) Thread.currentThread(java.lang.Thread.currentThread) Test(org.junit.Test)

Aggregations

Thread.currentThread (java.lang.Thread.currentThread)1 LinkedList (java.util.LinkedList)1 Context (org.glassfish.diagnostics.context.Context)1 ContextManager (org.glassfish.diagnostics.context.ContextManager)1 Test (org.junit.Test)1