diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java index 6f988c22d5..772a89e86f 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/RunnableContextPropagationTest.java @@ -16,6 +16,8 @@ package org.openecomp.sdc.logging.slf4j; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EMPTY; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_EXCEPTION_FROM_INNER; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.EXPECT_INNER_RUN; @@ -32,12 +34,12 @@ import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.IS_SU import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextEmpty; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.assertContextFields; import static org.openecomp.sdc.logging.slf4j.ContextPropagationTestHelper.putUniqueValues; -import static org.testng.Assert.assertTrue; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; /** * Unit-testing logging context propagation to Runnable. @@ -50,7 +52,12 @@ public class RunnableContextPropagationTest { private final LoggingContextService ctxService = new SLF4JLoggingServiceProvider(); - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Before + public void checkSuitableLogbackVersion() { + assumeTrue(IS_SUITABLE_LOGBACK_VERSION); + } + + @Test public void contextNotCopiedToChildThreadByDefault() throws InterruptedException { Map<ContextField, String> values = putUniqueValues(); @@ -66,11 +73,11 @@ public class RunnableContextPropagationTest { thread.start(); thread.join(); - assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, values); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void contextCopiedWhenToRunnableCalled() throws InterruptedException { Map<ContextField, String> values = putUniqueValues(); @@ -78,18 +85,18 @@ public class RunnableContextPropagationTest { // pass the runnable to the context service first Thread thread = new Thread(ctxService.copyToRunnable(() -> { - assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, values); complete.set(true); })); thread.start(); thread.join(); - assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, values); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void copiedContextRetainedEvenWhenAnotherPushed() throws InterruptedException { Map<ContextField, String> innerValues = putUniqueValues(); @@ -97,7 +104,7 @@ public class RunnableContextPropagationTest { // should run with the context of main thread Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); }); @@ -106,19 +113,19 @@ public class RunnableContextPropagationTest { Thread outer = new Thread(() -> { Map<ContextField, String> outerValues = putUniqueValues(); inner.run(); - assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); + assertContextFields(EXPECT_REPLACED_WITH_STORED, outerValues); outerComplete.set(true); }); outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_CURRENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void contextRemainsEmptyWhenParentWasEmpty() throws InterruptedException { ctxService.clear(); @@ -135,17 +142,17 @@ public class RunnableContextPropagationTest { thread.join(); assertContextEmpty(EXPECT_EMPTY); - assertTrue(complete.get(), EXPECT_INNER_RUN); + assertTrue(EXPECT_INNER_RUN, complete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void childThreadCleanedUpAfterRunnableRuns() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); }); @@ -161,12 +168,12 @@ public class RunnableContextPropagationTest { outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); } - @Test(enabled = IS_SUITABLE_LOGBACK_VERSION) + @Test public void childThreadCleanedUpAfterException() throws Exception { Map<ContextField, String> innerValues = putUniqueValues(); @@ -174,7 +181,7 @@ public class RunnableContextPropagationTest { // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Runnable inner = ctxService.copyToRunnable(() -> { - assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(EXPECT_PROPAGATED_TO_CHILD, innerValues); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -185,14 +192,14 @@ public class RunnableContextPropagationTest { Thread outer = new Thread(() -> { Map<ContextField, String> outerValues = putUniqueValues(); - assertContextFields(outerValues, EXPECT_POPULATED); + assertContextFields(EXPECT_POPULATED, outerValues); try { inner.run(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(EXPECT_REVERTED_ON_EXCEPTION, outerValues); outerComplete.set(true); } }); @@ -200,9 +207,9 @@ public class RunnableContextPropagationTest { outer.start(); outer.join(); - assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); - assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); - assertTrue(innerComplete.get(), EXPECT_INNER_RUN); - assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER); + assertContextFields(EXPECT_RETAINED_IN_PARENT, innerValues); + assertTrue(EXPECT_OUTER_RUN, outerComplete.get()); + assertTrue(EXPECT_INNER_RUN, innerComplete.get()); + assertTrue(EXPECT_EXCEPTION_FROM_INNER, exceptionThrown.get()); } }
\ No newline at end of file |