Search in sources :

Example 1 with Context

use of org.glassfish.diagnostics.context.Context in project Payara by payara.

the class AContextImplContextPropagationIntegrationTest method testThreadLocalBehaviour.

/**
 * Verify that multiple calls to get the current diagnostics context return the same instance.
 */
@Test
public void testThreadLocalBehaviour() {
    Context diagnosticsContextStart = mContextManager.getContext();
    assertEquals("The implementation class of diagnosticsContext1 is not as expected.", diagnosticsContextStart.getClass().getName(), ContextImpl.class.getName());
    for (int i = 0; i < 13; i++) {
        Context diagnosticsContext = mContextManager.getContext();
        assertSame("The diagnostics context instance returned in iteration " + i + " is not the same instance as fetched at the start of the test.", diagnosticsContextStart, diagnosticsContext);
    }
}
Also used : Context(org.glassfish.diagnostics.context.Context) Test(org.junit.Test)

Example 2 with Context

use of org.glassfish.diagnostics.context.Context 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)

Example 3 with Context

use of org.glassfish.diagnostics.context.Context in project Payara by payara.

the class ContextManagerImplUnitTest method testGetContextUseOfContextMap_new.

/**
 * Verify the expected delegation to ContextMap by ContextManagerImpl on invocation of getContext.
 */
// @Test
public void testGetContextUseOfContextMap_new(@Mocked final ContextMap mockedContextMap) throws Exception {
    new Expectations() {

        // We expect ContextManagerImpl to call getScopeAwareContextMap, but
        // we also need that method to return a ContextMap instance so
        // we tell the mocking framework to return an instance.
        ContextMapHelper expectationsRefContextMapHelper;

        {
            expectationsRefContextMapHelper.getScopeAwareContextMap();
            returns(mockedContextMap, null);
        }

        // We expect ContextManagerImpl to then go ahead and use the
        // ContextMap - in particular to call get (from which we deliberately
        // return null) and the createViewCapable (from which we return null
        // which is in practice an exceptional condition (which will result
        // in a WARNING log message) but does fine for this test.
        ContextMap expectationsRefContextMap = mockedContextMap;

        {
            expectationsRefContextMap.get(WORK_CONTEXT_KEY);
            returns(null, null);
            expectationsRefContextMap.createViewCapable(WORK_CONTEXT_KEY);
            returns(null, null);
        }
    };
    ContextManagerImpl cmi = new ContextManagerImpl();
    Context ci = cmi.getContext();
}
Also used : Expectations(mockit.Expectations) Context(org.glassfish.diagnostics.context.Context) ContextMapHelper(org.glassfish.contextpropagation.spi.ContextMapHelper) ContextMap(org.glassfish.contextpropagation.ContextMap)

Example 4 with Context

use of org.glassfish.diagnostics.context.Context in project Payara by payara.

the class ContextManagerImpl method getContext.

// from ContextManager
@Override
public Context getContext() {
    LOGGER.entering(CLASS_NAME, "getContext()");
    Context retVal = null;
    ContextMap contextMap = ContextMapHelper.getScopeAwareContextMap();
    try {
        retVal = contextMap.get(WORK_CONTEXT_KEY);
        if (retVal == null) {
            LOGGER.logp(Level.FINEST, CLASS_NAME, "getContext()", "No ContextImpl found in ContextMap, creating a new one.");
            ContextImpl contextImpl = contextMap.createViewCapable(WORK_CONTEXT_KEY);
            retVal = contextImpl;
        }
    } catch (InsufficientCredentialException isce) {
        // Nothing we can do to remedy isce so log the exception and move on
        // Do we return null or a dummy Context implementation?
        // Most callers won't be looking too closely at the context and
        // they probably won't want to concern themselves with problems
        // bubbling up from the contextpropagation layer, so a dummy
        // Context is preferred - the buck stops here!
        LOGGER.logp(Level.WARNING, CLASS_NAME, "getContext()", EXCEPTION_CREATING_CONTEXT_AS_DIAG_3001, isce);
        retVal = dummyContextInstance;
    }
    if (retVal == null) {
        LOGGER.logp(Level.WARNING, CLASS_NAME, "getContext()", CAN_NOT_GET_CONTEXT_AS_DIAG_3000);
        retVal = dummyContextInstance;
    }
    LOGGER.exiting(CLASS_NAME, "getContext()", retVal);
    return retVal;
}
Also used : Context(org.glassfish.diagnostics.context.Context)

Example 5 with Context

use of org.glassfish.diagnostics.context.Context in project Payara by payara.

the class AContextImplContextPropagationIntegrationTest method testValuePersistence.

/**
 * Verify that values set on the incumbent diagnostics context remain accessible on subsequent fetches of the
 * diagnostics context.
 */
@Test
public void testValuePersistence() {
    final String propagatingKey = "propagatingKey";
    final String propagatingValue = "propagatingValue";
    final String nonPropagatingKey = "nonPropagatingKey";
    final String nonPropagatingValue = "nonPropagatingValue";
    {
        Context diagnosticsContextStart = mContextManager.getContext();
        diagnosticsContextStart.put(propagatingKey, propagatingValue, true);
        diagnosticsContextStart.put(nonPropagatingKey, nonPropagatingValue, false);
    }
    for (int i = 0; i < 17; i++) {
        Context diagnosticsContext = mContextManager.getContext();
        assertEquals("The value associated with key " + propagatingKey + " is not as expected.", propagatingValue, diagnosticsContext.get(propagatingKey));
        assertEquals("The value associated with key " + nonPropagatingKey + " is not as expected.", nonPropagatingValue, diagnosticsContext.get(nonPropagatingKey));
    }
}
Also used : Context(org.glassfish.diagnostics.context.Context) Test(org.junit.Test)

Aggregations

Context (org.glassfish.diagnostics.context.Context)5 Test (org.junit.Test)3 Thread.currentThread (java.lang.Thread.currentThread)1 LinkedList (java.util.LinkedList)1 Expectations (mockit.Expectations)1 ContextMap (org.glassfish.contextpropagation.ContextMap)1 ContextMapHelper (org.glassfish.contextpropagation.spi.ContextMapHelper)1 ContextManager (org.glassfish.diagnostics.context.ContextManager)1