Skip to content

Commit

Permalink
Merge pull request #19601 from geoand/blocking-nonblocking
Browse files Browse the repository at this point in the history
Make the use of @Blocking and @nonblocking a build time error
  • Loading branch information
geoand authored Aug 24, 2021
2 parents 66f63a7 + 5620356 commit 4f0745b
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.resteasy.reactive.server.test;

import static org.junit.jupiter.api.Assertions.fail;

import java.util.function.Supplier;

import javax.enterprise.inject.spi.DeploymentException;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.common.annotation.NonBlocking;

public class BothBlockingAndNonBlockingOnApplicationTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Resource.class, MyApplication.class);
}
}).setExpectedException(DeploymentException.class);

@Test
public void test() {
fail("Should never have been called");
}

@Path("test")
public static class Resource {

@Path("hello")
public String hello() {
return "hello";
}
}

@ApplicationPath("/app")
@Blocking
@NonBlocking
public static class MyApplication extends Application {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.resteasy.reactive.server.test;

import static org.junit.jupiter.api.Assertions.fail;

import java.util.function.Supplier;

import javax.enterprise.inject.spi.DeploymentException;
import javax.ws.rs.Path;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.common.annotation.NonBlocking;

public class BothBlockingAndNonBlockingOnClassTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Resource.class);
}
}).setExpectedException(DeploymentException.class);

@Test
public void test() {
fail("Should never have been called");
}

@Path("test")
@Blocking
@NonBlocking
public static class Resource {

@Path("hello")
public String hello() {
return "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.resteasy.reactive.server.test;

import static org.junit.jupiter.api.Assertions.fail;

import java.util.function.Supplier;

import javax.enterprise.inject.spi.DeploymentException;
import javax.ws.rs.Path;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.common.annotation.NonBlocking;

public class BothBlockingAndNonBlockingOnMethodTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Resource.class);
}
}).setExpectedException(DeploymentException.class);

@Test
public void test() {
fail("Should never have been called");
}

@Path("test")
public static class Resource {

@Path("hello")
@Blocking
@NonBlocking
public String hello() {
return "hello";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@ private boolean isBlocking(MethodInfo info, BlockingDefault defaultValue) {
Map.Entry<AnnotationTarget, AnnotationInstance> nonBlockingAnnotation = getInheritableAnnotation(info,
NON_BLOCKING);
if ((blockingAnnotation != null) && (nonBlockingAnnotation != null)) {
if (blockingAnnotation.getKey().kind() == nonBlockingAnnotation.getKey().kind()) {
if (blockingAnnotation.getKey().kind() == AnnotationTarget.Kind.METHOD) {
throw new DeploymentException("Method '" + info.name() + "' of class '" + info.declaringClass().name()
+ "' contains both @Blocking and @NonBlocking annotations.");
} else {
throw new DeploymentException("Class '" + info.declaringClass().name()
+ "' contains both @Blocking and @NonBlocking annotations.");
}
}
if (blockingAnnotation.getKey().kind() == AnnotationTarget.Kind.METHOD) {
// the most specific annotation was the @Blocking annotation on the method
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.enterprise.inject.spi.DeploymentException;
import javax.ws.rs.core.Application;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand Down Expand Up @@ -95,6 +96,10 @@ public static ApplicationScanningResult scanForApplicationClass(IndexView index,
throw new RuntimeException("Unable to handle class: " + applicationClass, e);
}
if (applicationClassInfo.classAnnotation(ResteasyReactiveDotNames.BLOCKING) != null) {
if (applicationClassInfo.classAnnotation(ResteasyReactiveDotNames.NON_BLOCKING) != null) {
throw new DeploymentException("JAX-RS Application class '" + applicationClassInfo.name()
+ "' contains both @Blocking and @NonBlocking annotations.");
}
blocking = BlockingDefault.BLOCKING;
} else if (applicationClassInfo.classAnnotation(ResteasyReactiveDotNames.NON_BLOCKING) != null) {
blocking = BlockingDefault.NON_BLOCKING;
Expand Down

0 comments on commit 4f0745b

Please sign in to comment.