2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.slf4j;
19 import org.openecomp.sdc.logging.provider.LoggingContextService;
20 import org.testng.annotations.Test;
22 import java.util.UUID;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicBoolean;
30 import static org.testng.Assert.assertEquals;
31 import static org.testng.Assert.assertNull;
32 import static org.testng.Assert.assertTrue;
38 public class CallableContextPropagationTest extends BaseContextPropagationTest {
40 @Test(enabled = ENABLED, dataProvider = PROVIDER)
41 public void testContextPropagated(LoggingContextService ctx) throws Exception {
43 String uuid = UUID.randomUUID().toString();
46 AtomicBoolean complete = new AtomicBoolean(false);
48 // pass the callable to the context service first
49 execute(ctx.toCallable(() -> {
50 assertEquals(ctx.get(KEY), uuid, EXPECT_PROPAGATED_TO_CHILD);
55 assertEquals(ctx.get(KEY), uuid, EXPECT_RETAINED_IN_CURRENT);
56 assertTrue(complete.get(), EXPECT_INNER_RUN);
59 @Test(enabled = ENABLED, dataProvider = PROVIDER)
60 public void testContextReplacement(LoggingContextService ctx) throws Exception {
62 String innerRandom = UUID.randomUUID().toString();
63 ctx.put(KEY, innerRandom);
65 AtomicBoolean innerComplete = new AtomicBoolean(false);
67 // should run with the context of main thread
68 Callable inner = ctx.toCallable(() -> {
69 assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD);
70 innerComplete.set(true);
74 // pushes its own context, but the inner must run with its own context
75 AtomicBoolean outerComplete = new AtomicBoolean(false);
77 String outerUuid = UUID.randomUUID().toString();
78 ctx.put(KEY, outerUuid);
80 assertEquals(ctx.get(KEY), outerUuid, EXPECT_REPLACED_WITH_STORED);
81 outerComplete.set(true);
85 assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_CURRENT);
86 assertTrue(outerComplete.get(), EXPECT_OUTER_RUN);
87 assertTrue(innerComplete.get(), EXPECT_INNER_RUN);
90 @Test(enabled = ENABLED, dataProvider = PROVIDER)
91 public void testContextRemainsEmpty(LoggingContextService ctx) throws Exception {
94 assertNull(ctx.get(KEY), EXPECT_EMPTY);
96 final AtomicBoolean complete = new AtomicBoolean(false);
97 execute(ctx.toCallable(() -> {
98 assertNull(ctx.get(KEY), EXPECT_EMPTY);
103 assertNull(ctx.get(KEY), EXPECT_EMPTY);
104 assertTrue(complete.get(), EXPECT_INNER_RUN);
107 @Test(enabled = ENABLED, dataProvider = PROVIDER)
108 public void testContextCleanedUp(LoggingContextService ctx) throws Exception {
110 String innerRandom = UUID.randomUUID().toString();
111 ctx.put(KEY, innerRandom);
113 AtomicBoolean innerComplete = new AtomicBoolean(false);
114 // should run with the context of main thread
115 Callable inner = ctx.toCallable((() -> {
116 assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD);
117 innerComplete.set(true);
121 // pushes its own context, but runs the inner
122 AtomicBoolean outerComplete = new AtomicBoolean(false);
124 assertNull(ctx.get(KEY), EXPECT_NOT_COPIED);
126 assertNull(ctx.get(KEY), EXPECT_REMAIN_EMPTY);
127 outerComplete.set(true);
131 assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT);
132 assertTrue(outerComplete.get(), EXPECT_OUTER_RUN);
133 assertTrue(innerComplete.get(), EXPECT_INNER_RUN);
136 @Test(enabled = ENABLED, dataProvider = PROVIDER)
137 public void testCleanupAfterError(LoggingContextService ctx) throws Exception {
139 String innerRandom = UUID.randomUUID().toString();
140 ctx.put(KEY, innerRandom);
142 // should run with the context of main thread
143 AtomicBoolean innerComplete = new AtomicBoolean(false);
144 Callable inner = ctx.toCallable(() -> {
145 assertEquals(ctx.get(KEY), innerRandom, EXPECT_PROPAGATED_TO_CHILD);
146 innerComplete.set(true);
147 throw new IllegalArgumentException();
150 // pushes its own context, but runs the inner callable
151 AtomicBoolean outerComplete = new AtomicBoolean(false);
152 AtomicBoolean exceptionThrown = new AtomicBoolean(false);
155 String outerUuid = UUID.randomUUID().toString();
156 ctx.put(KEY, outerUuid);
157 assertEquals(ctx.get(KEY), outerUuid, EXPECT_POPULATED);
161 } catch (IllegalArgumentException e) {
162 exceptionThrown.set(true);
164 assertEquals(ctx.get(KEY), outerUuid, EXPECT_REVERTED_ON_EXCEPTION);
165 outerComplete.set(true);
171 assertEquals(ctx.get(KEY), innerRandom, EXPECT_RETAINED_IN_PARENT);
172 assertTrue(outerComplete.get(), EXPECT_OUTER_RUN);
173 assertTrue(innerComplete.get(), EXPECT_INNER_RUN);
174 assertTrue(exceptionThrown.get(), EXPECT_EXCEPTION_FROM_INNER);
177 private void execute(Callable<Object> callable) throws Exception {
179 ExecutorService executor = Executors.newSingleThreadExecutor();
182 Future<Object> future = executor.submit(callable);
183 future.get(10, TimeUnit.SECONDS);