|
| 1 | +package io.quarkus.resteasy.reactive.server.test; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.get; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | + |
| 6 | +import java.io.Closeable; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +import jakarta.enterprise.context.RequestScoped; |
| 13 | +import jakarta.inject.Inject; |
| 14 | +import jakarta.inject.Singleton; |
| 15 | +import jakarta.ws.rs.GET; |
| 16 | +import jakarta.ws.rs.Path; |
| 17 | +import jakarta.ws.rs.core.Context; |
| 18 | + |
| 19 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 20 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 23 | + |
| 24 | +import io.quarkus.resteasy.reactive.server.Closer; |
| 25 | +import io.quarkus.test.QuarkusUnitTest; |
| 26 | +import io.smallrye.mutiny.Uni; |
| 27 | + |
| 28 | +public class CloserTest { |
| 29 | + |
| 30 | + @RegisterExtension |
| 31 | + static QuarkusUnitTest test = new QuarkusUnitTest() |
| 32 | + .setArchiveProducer(new Supplier<>() { |
| 33 | + @Override |
| 34 | + public JavaArchive get() { |
| 35 | + return ShrinkWrap.create(JavaArchive.class) |
| 36 | + .addClasses(PerRequestResource.class, SingletonResource.class, CounterResource.class, |
| 37 | + Counter.class); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void test() { |
| 43 | + get("/counter/singleton") |
| 44 | + .then() |
| 45 | + .body(equalTo("0")); |
| 46 | + get("/counter/uni-singleton") |
| 47 | + .then() |
| 48 | + .body(equalTo("0")); |
| 49 | + get("/counter/per-request") |
| 50 | + .then() |
| 51 | + .body(equalTo("0")); |
| 52 | + |
| 53 | + get("/singleton") |
| 54 | + .then() |
| 55 | + .statusCode(200) |
| 56 | + .body(equalTo("0")); |
| 57 | + get("/singleton") |
| 58 | + .then() |
| 59 | + .statusCode(200) |
| 60 | + .body(equalTo("1")); |
| 61 | + get("/counter/singleton") |
| 62 | + .then() |
| 63 | + .body(equalTo("2")); |
| 64 | + get("/counter/uni-singleton") |
| 65 | + .then() |
| 66 | + .body(equalTo("0")); |
| 67 | + get("/counter/per-request") |
| 68 | + .then() |
| 69 | + .body(equalTo("0")); |
| 70 | + |
| 71 | + get("/uni-singleton") |
| 72 | + .then() |
| 73 | + .statusCode(200) |
| 74 | + .body(equalTo("0")); |
| 75 | + get("/uni-singleton") |
| 76 | + .then() |
| 77 | + .statusCode(200) |
| 78 | + .body(equalTo("1")); |
| 79 | + get("/counter/singleton") |
| 80 | + .then() |
| 81 | + .body(equalTo("2")); |
| 82 | + get("/counter/uni-singleton") |
| 83 | + .then() |
| 84 | + .body(equalTo("2")); |
| 85 | + get("/counter/per-request") |
| 86 | + .then() |
| 87 | + .body(equalTo("0")); |
| 88 | + |
| 89 | + get("/per-request") |
| 90 | + .then() |
| 91 | + .statusCode(200) |
| 92 | + .body(equalTo("0")); |
| 93 | + get("/per-request") |
| 94 | + .then() |
| 95 | + .statusCode(200) |
| 96 | + .body(equalTo("1")); |
| 97 | + get("/counter/singleton") |
| 98 | + .then() |
| 99 | + .body(equalTo("2")); |
| 100 | + get("/counter/uni-singleton") |
| 101 | + .then() |
| 102 | + .body(equalTo("2")); |
| 103 | + get("/counter/per-request") |
| 104 | + .then() |
| 105 | + .body(equalTo("2")); |
| 106 | + } |
| 107 | + |
| 108 | + @Path("per-request") |
| 109 | + @RequestScoped |
| 110 | + public static class PerRequestResource implements Closeable { |
| 111 | + private final Closer closer; |
| 112 | + private final Counter counter; |
| 113 | + |
| 114 | + public PerRequestResource(Closer closer, Counter counter) { |
| 115 | + this.closer = closer; |
| 116 | + this.counter = counter; |
| 117 | + } |
| 118 | + |
| 119 | + @GET |
| 120 | + public int get() { |
| 121 | + closer.add(this); |
| 122 | + return counter.perRequest.get(); |
| 123 | + } |
| 124 | + |
| 125 | + public void close() throws IOException { |
| 126 | + counter.perRequest.incrementAndGet(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Path("singleton") |
| 131 | + public static class SingletonResource implements Closeable { |
| 132 | + |
| 133 | + private final Counter counter; |
| 134 | + |
| 135 | + public SingletonResource(Counter counter) { |
| 136 | + this.counter = counter; |
| 137 | + } |
| 138 | + |
| 139 | + @GET |
| 140 | + public int get(@Context Closer closer) { |
| 141 | + closer.add(this); |
| 142 | + return counter.singleton.get(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void close() { |
| 147 | + counter.singleton.incrementAndGet(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Path("uni-singleton") |
| 152 | + public static class UniSingletonResource implements Closeable { |
| 153 | + |
| 154 | + @Inject |
| 155 | + Counter counter; |
| 156 | + |
| 157 | + @Inject |
| 158 | + Closer closer; |
| 159 | + |
| 160 | + public UniSingletonResource(Counter counter) { |
| 161 | + this.counter = counter; |
| 162 | + } |
| 163 | + |
| 164 | + @GET |
| 165 | + public Uni<Integer> get() { |
| 166 | + return Uni.createFrom().completionStage(() -> CompletableFuture.completedStage(null)) |
| 167 | + .invoke(() -> closer.add(UniSingletonResource.this)) |
| 168 | + .map(v -> counter.uniSingleton.get()); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void close() { |
| 173 | + counter.uniSingleton.incrementAndGet(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Path("counter") |
| 178 | + public static class CounterResource { |
| 179 | + |
| 180 | + private final Counter counter; |
| 181 | + |
| 182 | + public CounterResource(Counter counter) { |
| 183 | + this.counter = counter; |
| 184 | + } |
| 185 | + |
| 186 | + @Path("singleton") |
| 187 | + @GET |
| 188 | + public int singletonCount() { |
| 189 | + return counter.singleton.get(); |
| 190 | + } |
| 191 | + |
| 192 | + @Path("uni-singleton") |
| 193 | + @GET |
| 194 | + public int uniSingleton() { |
| 195 | + return counter.uniSingleton.get(); |
| 196 | + } |
| 197 | + |
| 198 | + @Path("per-request") |
| 199 | + @GET |
| 200 | + public int perRequestCount() { |
| 201 | + return counter.perRequest.get(); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + @Singleton |
| 206 | + public static class Counter { |
| 207 | + public final AtomicInteger perRequest = new AtomicInteger(0); |
| 208 | + public final AtomicInteger singleton = new AtomicInteger(0); |
| 209 | + public final AtomicInteger uniSingleton = new AtomicInteger(0); |
| 210 | + |
| 211 | + } |
| 212 | +} |
0 commit comments