Skip to content

Commit fb77018

Browse files
authored
Guard for the case in which a request scope object is already available from CDI but a request context is not. New test. (helidon-io#2933)
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
1 parent fa30f1a commit fb77018

4 files changed

Lines changed: 89 additions & 16 deletions

File tree

microprofile/fault-tolerance/src/main/java/io/helidon/microprofile/faulttolerance/RequestScopeHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ FtSupplier<Object> wrapInScope(FtSupplier<Object> supplier) {
9292
if (state != State.STORED) {
9393
throw new IllegalStateException("Request scope state never stored");
9494
}
95-
if (requestScope != null) { // Jersey and CDI
95+
if (requestScope != null && requestContext != null) { // Jersey and CDI
9696
return () -> requestScope.runInScope(requestContext,
9797
(Callable<?>) (() -> {
9898
InjectionManager old = WeldRequestScope.actualInjectorManager.get();

tests/functional/request-scope/pom.xml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,13 @@
3131
<dependencies>
3232
<dependency>
3333
<groupId>io.helidon.microprofile.bundles</groupId>
34-
<artifactId>helidon-microprofile-core</artifactId>
34+
<artifactId>helidon-microprofile</artifactId>
3535
</dependency>
3636
<dependency>
37-
<groupId>io.helidon.config</groupId>
38-
<artifactId>helidon-config-yaml</artifactId>
39-
</dependency>
40-
<dependency>
41-
<groupId>io.helidon.microprofile</groupId>
42-
<artifactId>helidon-microprofile-fault-tolerance</artifactId>
43-
</dependency>
44-
<dependency>
45-
<groupId>io.helidon.microprofile.metrics</groupId>
46-
<artifactId>helidon-microprofile-metrics</artifactId>
37+
<groupId>org.jboss</groupId>
38+
<artifactId>jandex</artifactId>
39+
<scope>runtime</scope>
40+
<optional>true</optional>
4741
</dependency>
4842
<dependency>
4943
<groupId>org.junit.jupiter</groupId>
@@ -65,9 +59,5 @@
6559
<artifactId>helidon-microprofile-tests-junit5</artifactId>
6660
<scope>test</scope>
6761
</dependency>
68-
<dependency>
69-
<groupId>org.eclipse.microprofile.opentracing</groupId>
70-
<artifactId>microprofile-opentracing-api</artifactId>
71-
</dependency>
7262
</dependencies>
7363
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
package io.helidon.tests.functional.requestscope;
17+
18+
import java.time.temporal.ChronoUnit;
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.ScheduledExecutorService;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
23+
import javax.annotation.Priority;
24+
import javax.enterprise.context.ApplicationScoped;
25+
import javax.enterprise.context.BeforeDestroyed;
26+
import javax.enterprise.context.Initialized;
27+
import javax.enterprise.event.Observes;
28+
import javax.interceptor.Interceptor;
29+
import javax.ws.rs.ProcessingException;
30+
31+
import org.eclipse.microprofile.faulttolerance.Fallback;
32+
import org.eclipse.microprofile.faulttolerance.Retry;
33+
34+
/**
35+
* Verifies that FT initializes its internal state correctly when a request scope
36+
* is already initialized but a request context is not.
37+
*/
38+
@ApplicationScoped
39+
public class StartupServices {
40+
41+
/**
42+
* Checked by tests to verify startup was successful.
43+
*/
44+
public static final AtomicBoolean SUCCESSFUL_STARTUP = new AtomicBoolean(false);
45+
46+
private static ScheduledExecutorService execServiceApis;
47+
48+
private static void onStartup(@Observes @Priority(Interceptor.Priority.PLATFORM_AFTER + 101)
49+
@Initialized(ApplicationScoped.class) final Object event, StartupServices self) {
50+
execServiceApis = Executors.newSingleThreadScheduledExecutor();
51+
execServiceApis.execute(() -> {
52+
try {
53+
self.loadApis();
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
});
58+
}
59+
60+
private static void rightBeforeShutdown(@Observes @BeforeDestroyed(ApplicationScoped.class) final Object event) {
61+
if (execServiceApis != null) {
62+
execServiceApis.shutdown();
63+
}
64+
}
65+
66+
@Retry(delay = 1, delayUnit = ChronoUnit.SECONDS,
67+
maxDuration = 60, durationUnit = ChronoUnit.SECONDS,
68+
maxRetries = 10, abortOn = javax.ws.rs.ClientErrorException.class)
69+
@Fallback(fallbackMethod = "loadApisFromDisk")
70+
protected void loadApis() throws Exception {
71+
if (SUCCESSFUL_STARTUP.compareAndSet(false, true)) {
72+
throw new ProcessingException("oops"); // will throw exception once
73+
}
74+
}
75+
76+
protected void loadApisFromDisk() throws Exception {
77+
}
78+
}

tests/functional/request-scope/src/test/java/io/helidon/tests/functional/requestscope/TenantTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.netty.handler.codec.http.HttpResponseStatus;
2929
import static org.hamcrest.CoreMatchers.is;
3030
import static org.hamcrest.MatcherAssert.assertThat;
31+
3132
import org.junit.jupiter.api.Test;
3233

3334
@HelidonTest
@@ -96,4 +97,8 @@ public void test5() {
9697
assertThat(entityValue, is("1"));
9798
}
9899

100+
@Test
101+
public void testStartup() {
102+
assertThat(StartupServices.SUCCESSFUL_STARTUP.get(), is(true));
103+
}
99104
}

0 commit comments

Comments
 (0)