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 | 80 |
1 files changed, 35 insertions, 45 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 90d6cda52a..fcd1a56252 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,18 +16,19 @@ package org.openecomp.sdc.logging.slf4j; -import org.openecomp.sdc.logging.spi.LoggingContextService; -import org.testng.annotations.Test; +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.UUID; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; +import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; +import org.openecomp.sdc.logging.spi.LoggingContextService; +import org.testng.annotations.Test; /** - * @author EVITALIY + * @author evitaliy * @since 08 Jan 18 */ public class RunnableContextPropagationTest extends BaseContextPropagationTest { @@ -36,14 +37,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextNotCopiedToChildThreadByDefault(LoggingContextService ctx) throws InterruptedException { - String random = UUID.randomUUID().toString(); - ctx.put(KEY, random); - + Map<ContextField, String> values = putUniqueValues(ctx); AtomicBoolean complete = new AtomicBoolean(false); // create thread right away without copying context Thread thread = new Thread(() -> { - assertNull(ctx.get(KEY), "Data unexpectedly copied to a child thread. " + + assertContextEmpty("Data unexpectedly copied to a child thread. " + "Are you using an old version of SLF4J diagnostic context implementation (e.g. logback)?"); complete.set(true); }); @@ -51,7 +50,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { thread.start(); thread.join(); - assertEquals(ctx.get(KEY), random, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -59,21 +58,19 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextCopiedWhenToRunnableCalled(LoggingContextService ctx) throws InterruptedException { - String uuid = UUID.randomUUID().toString(); - ctx.put(KEY, uuid); - + Map<ContextField, String> values = putUniqueValues(ctx); AtomicBoolean complete = new AtomicBoolean(false); // pass the runnable to the context service first Thread thread = new Thread(ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(values, EXPECT_PROPAGATED_TO_CHILD); complete.set(true); })); thread.start(); thread.join(); - assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(values, EXPECT_RETAINED_IN_CURRENT); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -81,31 +78,28 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void copiedContextRetainedEvenWhenAnotherPushed(LoggingContextService ctx) throws InterruptedException { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); - + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); }); // pushes its context, but the inner must run with its own context AtomicBoolean outerComplete = new AtomicBoolean(false); Thread outer = new Thread(() -> { - String outerUuid = UUID.randomUUID().toString(); - ctx.put(KEY, outerUuid); + Map<ContextField, String> outerValues = putUniqueValues(ctx); inner.run(); - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED); + assertContextFields(outerValues, EXPECT_REPLACED_WITH_STORED); outerComplete.set(true); }); outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_CURRENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -114,12 +108,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void contextRemainsEmptyWhenParentWasEmpty(LoggingContextService ctx) throws InterruptedException { - ctx.remove(KEY); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + ctx.clear(); + assertContextEmpty(EXPECT_EMPTY); final AtomicBoolean complete = new AtomicBoolean(false); Runnable runnable = ctx.copyToRunnable(() -> { - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(EXPECT_EMPTY); complete.set(true); }); @@ -127,7 +121,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { thread.start(); thread.join(); - assertNull(ctx.get(KEY), EXPECT_EMPTY); + assertContextEmpty(EXPECT_EMPTY); assertTrue(complete.get(), EXPECT_INNER_RUN); } @@ -135,29 +129,27 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void childThreadCleanedUpAfterRunnableRuns(LoggingContextService ctx) throws Exception { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); - + Map<ContextField, String> innerValues = putUniqueValues(ctx); AtomicBoolean innerComplete = new AtomicBoolean(false); // should run with the context of main thread Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); }); // pushes its own context, but runs the inner AtomicBoolean outerComplete = new AtomicBoolean(false); Thread outer = new Thread(() -> { - assertNull(ctx.get(KEY), EXPECT_NOT_COPIED); + assertContextEmpty(EXPECT_NOT_COPIED); inner.run(); - assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY); + assertContextEmpty(EXPECT_REMAIN_EMPTY); outerComplete.set(true); }); outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + assertContextFields(innerValues, EXPECT_RETAINED_IN_PARENT); assertTrue(outerComplete.get(), EXPECT_OUTER_RUN); assertTrue(innerComplete.get(), EXPECT_INNER_RUN); } @@ -166,13 +158,12 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { public void childThreadCleanedUpAfterException(LoggingContextService ctx) throws Exception { - String innerRandom = UUID.randomUUID().toString(); - ctx.put(KEY, innerRandom); + Map<ContextField, String> innerValues = putUniqueValues(ctx); // should run with the context of main thread AtomicBoolean innerComplete = new AtomicBoolean(false); Runnable inner = ctx.copyToRunnable(() -> { - assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD); + assertContextFields(innerValues, EXPECT_PROPAGATED_TO_CHILD); innerComplete.set(true); throw new IllegalArgumentException(); }); @@ -182,16 +173,15 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { AtomicBoolean exceptionThrown = new AtomicBoolean(false); Thread outer = new Thread(() -> { - String outerUuid = UUID.randomUUID().toString(); - ctx.put(KEY, outerUuid); - assertEquals(ctx.get(KEY), outerUuid, EXPECT_POPULATED); + Map<ContextField, String> outerValues = putUniqueValues(ctx); + assertContextFields(outerValues, EXPECT_POPULATED); try { inner.run(); } catch (IllegalArgumentException e) { exceptionThrown.set(true); } finally { - assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION); + assertContextFields(outerValues, EXPECT_REVERTED_ON_EXCEPTION); outerComplete.set(true); } }); @@ -199,7 +189,7 @@ public class RunnableContextPropagationTest extends BaseContextPropagationTest { outer.start(); outer.join(); - assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT); + 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); |