Search in sources :

Example 1 with GenericLoggingContext

use of co.cask.cdap.logging.context.GenericLoggingContext in project cdap by caskdata.

the class LoggingContextAccessorTest method testReset.

@Test
public void testReset() {
    Cancellable cancellable = LoggingContextAccessor.setLoggingContext(new GenericLoggingContext(OLD_NS, OLD_APP, OLD_ENTITY));
    Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), OLD_NS);
    Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), OLD_APP);
    Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), OLD_ENTITY);
    final Cancellable cancellable2 = LoggingContextAccessor.setLoggingContext(new GenericLoggingContext(NS, APP, ENTITY));
    Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), NS);
    Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), APP);
    Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), ENTITY);
    // Verify a different thread cannot change context
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            cancellable2.cancel();
        }
    });
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
        Assert.fail();
    }
    Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), NS);
    Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), APP);
    Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), ENTITY);
    // Reset in the same thread
    cancellable2.cancel();
    Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), OLD_NS);
    Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), OLD_APP);
    Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), OLD_ENTITY);
    // Check reset back to nothing
    cancellable.cancel();
    Assert.assertTrue(MDC.getCopyOfContextMap().isEmpty());
}
Also used : Cancellable(org.apache.twill.common.Cancellable) GenericLoggingContext(co.cask.cdap.logging.context.GenericLoggingContext) Test(org.junit.Test)

Aggregations

GenericLoggingContext (co.cask.cdap.logging.context.GenericLoggingContext)1 Cancellable (org.apache.twill.common.Cancellable)1 Test (org.junit.Test)1