From 5b5589715cf00b921aebc6da2b223cf4b3c9d1e2 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Tue, 17 Oct 2023 17:25:39 +0200 Subject: [PATCH] WIP add tests for sessions --- .../client/websessions/CounterResource.java | 21 +++++ .../src/main/resources/application.properties | 1 + .../client/websessions/CounterTest.java | 85 ++++++++++++++++++ .../redis/it/websessions/CounterResource.java | 21 +++++ .../src/main/resources/application.properties | 8 +- .../redis/it/websessions/CounterTest.java | 85 ++++++++++++++++++ .../it/vertx/websessions/CounterEndpoint.java | 22 +++++ .../src/main/resources/application.properties | 1 + .../it/vertx/websessions/CounterTest.java | 89 +++++++++++++++++++ 9 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 integration-tests/infinispan-client/src/main/java/io/quarkus/it/infinispan/client/websessions/CounterResource.java create mode 100644 integration-tests/infinispan-client/src/test/java/io/quarkus/it/infinispan/client/websessions/CounterTest.java create mode 100644 integration-tests/redis-client/src/main/java/io/quarkus/redis/it/websessions/CounterResource.java create mode 100644 integration-tests/redis-client/src/test/java/io/quarkus/redis/it/websessions/CounterTest.java create mode 100644 integration-tests/vertx-web/src/main/java/io/quarkus/it/vertx/websessions/CounterEndpoint.java create mode 100644 integration-tests/vertx-web/src/main/resources/application.properties create mode 100644 integration-tests/vertx-web/src/test/java/io/quarkus/it/vertx/websessions/CounterTest.java diff --git a/integration-tests/infinispan-client/src/main/java/io/quarkus/it/infinispan/client/websessions/CounterResource.java b/integration-tests/infinispan-client/src/main/java/io/quarkus/it/infinispan/client/websessions/CounterResource.java new file mode 100644 index 00000000000000..d5f008f62cb1b9 --- /dev/null +++ b/integration-tests/infinispan-client/src/main/java/io/quarkus/it/infinispan/client/websessions/CounterResource.java @@ -0,0 +1,21 @@ +package io.quarkus.it.infinispan.client.websessions; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import io.vertx.ext.web.Session; + +@Path("/counter") +public class CounterResource { + @Inject + Session session; + + @GET + public String counter() { + Integer counter = session.get("counter"); + counter = counter == null ? 1 : counter + 1; + session.put("counter", counter); + return session.id() + "|" + counter; + } +} diff --git a/integration-tests/infinispan-client/src/main/resources/application.properties b/integration-tests/infinispan-client/src/main/resources/application.properties index d9e2391da36e85..d1dfb6129874e9 100644 --- a/integration-tests/infinispan-client/src/main/resources/application.properties +++ b/integration-tests/infinispan-client/src/main/resources/application.properties @@ -21,3 +21,4 @@ quarkus.infinispan-client.another.devservices.mcast-port=46667 quarkus.infinispan-client.another.devservices.port=31223 quarkus.infinispan-client.another.devservices.service-name=infinispanAnother +quarkus.http.sessions.mode=infinispan diff --git a/integration-tests/infinispan-client/src/test/java/io/quarkus/it/infinispan/client/websessions/CounterTest.java b/integration-tests/infinispan-client/src/test/java/io/quarkus/it/infinispan/client/websessions/CounterTest.java new file mode 100644 index 00000000000000..e789a674b6cda0 --- /dev/null +++ b/integration-tests/infinispan-client/src/test/java/io/quarkus/it/infinispan/client/websessions/CounterTest.java @@ -0,0 +1,85 @@ +package io.quarkus.it.infinispan.client.websessions; + +import static io.restassured.RestAssured.with; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.filter.session.SessionFilter; +import io.restassured.response.Response; + +@QuarkusTest +public class CounterTest { + @Test + public void test() throws InterruptedException { + List users = new ArrayList<>(); + for (int i = 0; i < 50; i++) { + users.add(new User(100)); + } + + for (User user : users) { + user.start(); + } + for (User user : users) { + user.join(); + } + for (User user : users) { + user.verify(); + } + } + + static class User extends Thread { + private static final AtomicInteger counter = new AtomicInteger(); + + private final Set sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>()); + private final Queue responses = new ConcurrentLinkedQueue<>(); + + private final int requests; + + User(int requests) { + super("User" + counter.incrementAndGet()); + this.requests = requests; + } + + @Override + public void run() { + SessionFilter sessions = new SessionFilter(); + for (int i = 0; i < requests; i++) { + Response response = with().filter(sessions).get("/counter"); + if (response.sessionId() != null) { + sessionIds.add(response.sessionId()); + } + responses.add(response.body().asString()); + + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(50)); + } catch (InterruptedException e) { + return; + } + } + } + + public void verify() { + assertEquals(1, sessionIds.size()); + String id = sessionIds.iterator().next(); + + assertEquals(requests, responses.size()); + int i = 1; + for (String response : responses) { + assertEquals(id + "|" + i, response); + i++; + } + } + } +} diff --git a/integration-tests/redis-client/src/main/java/io/quarkus/redis/it/websessions/CounterResource.java b/integration-tests/redis-client/src/main/java/io/quarkus/redis/it/websessions/CounterResource.java new file mode 100644 index 00000000000000..ce4e905d1bb004 --- /dev/null +++ b/integration-tests/redis-client/src/main/java/io/quarkus/redis/it/websessions/CounterResource.java @@ -0,0 +1,21 @@ +package io.quarkus.redis.it.websessions; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import io.vertx.ext.web.Session; + +@Path("/counter") +public class CounterResource { + @Inject + Session session; + + @GET + public String counter() { + Integer counter = session.get("counter"); + counter = counter == null ? 1 : counter + 1; + session.put("counter", counter); + return session.id() + "|" + counter; + } +} diff --git a/integration-tests/redis-client/src/main/resources/application.properties b/integration-tests/redis-client/src/main/resources/application.properties index 6bb245e908d5ed..caaa9935687701 100644 --- a/integration-tests/redis-client/src/main/resources/application.properties +++ b/integration-tests/redis-client/src/main/resources/application.properties @@ -8,4 +8,10 @@ quarkus.redis.instance-client.hosts=redis://localhost:6379/5 # use DB 3 quarkus.redis.provided-hosts.hosts-provider-name=test-hosts-provider -quarkus.redis.load-script=starwars.redis \ No newline at end of file +quarkus.redis.load-script=starwars.redis + +quarkus.redis.web-sessions.hosts=redis://localhost:6379/7 +quarkus.redis.web-sessions.max-pool-waiting=100 + +quarkus.http.sessions.mode=redis +quarkus.http.sessions.redis.client-name=web-sessions diff --git a/integration-tests/redis-client/src/test/java/io/quarkus/redis/it/websessions/CounterTest.java b/integration-tests/redis-client/src/test/java/io/quarkus/redis/it/websessions/CounterTest.java new file mode 100644 index 00000000000000..eb8c20aad72af2 --- /dev/null +++ b/integration-tests/redis-client/src/test/java/io/quarkus/redis/it/websessions/CounterTest.java @@ -0,0 +1,85 @@ +package io.quarkus.redis.it.websessions; + +import static io.restassured.RestAssured.with; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.filter.session.SessionFilter; +import io.restassured.response.Response; + +@QuarkusTest +public class CounterTest { + @Test + public void test() throws InterruptedException { + List users = new ArrayList<>(); + for (int i = 0; i < 50; i++) { + users.add(new User(100)); + } + + for (User user : users) { + user.start(); + } + for (User user : users) { + user.join(); + } + for (User user : users) { + user.verify(); + } + } + + static class User extends Thread { + private static final AtomicInteger counter = new AtomicInteger(); + + private final Set sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>()); + private final Queue responses = new ConcurrentLinkedQueue<>(); + + private final int requests; + + User(int requests) { + super("User" + counter.incrementAndGet()); + this.requests = requests; + } + + @Override + public void run() { + SessionFilter sessions = new SessionFilter(); + for (int i = 0; i < requests; i++) { + Response response = with().filter(sessions).get("/counter"); + if (response.sessionId() != null) { + sessionIds.add(response.sessionId()); + } + responses.add(response.body().asString()); + + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(50)); + } catch (InterruptedException e) { + return; + } + } + } + + public void verify() { + assertEquals(1, sessionIds.size()); + String id = sessionIds.iterator().next(); + + assertEquals(requests, responses.size()); + int i = 1; + for (String response : responses) { + assertEquals(id + "|" + i, response); + i++; + } + } + } +} diff --git a/integration-tests/vertx-web/src/main/java/io/quarkus/it/vertx/websessions/CounterEndpoint.java b/integration-tests/vertx-web/src/main/java/io/quarkus/it/vertx/websessions/CounterEndpoint.java new file mode 100644 index 00000000000000..c31b79145ac9ba --- /dev/null +++ b/integration-tests/vertx-web/src/main/java/io/quarkus/it/vertx/websessions/CounterEndpoint.java @@ -0,0 +1,22 @@ +package io.quarkus.it.vertx.websessions; + +import io.quarkus.vertx.web.Route; +import io.vertx.ext.web.RoutingContext; +import io.vertx.ext.web.Session; + +public class CounterEndpoint { + @Route(path = "/counter", methods = Route.HttpMethod.GET) + String counter(RoutingContext ctx) { + Session session = ctx.session(); + Integer counter = session.get("counter"); + counter = counter == null ? 1 : counter + 1; + session.put("counter", counter); + return session.id() + "|" + counter; + } + + @Route(path = "/check-sessions", methods = Route.HttpMethod.GET) + void checkSessions(RoutingContext ctx) { + Session session = ctx.session(); + ctx.end(session != null ? "OK" : "KO"); + } +} diff --git a/integration-tests/vertx-web/src/main/resources/application.properties b/integration-tests/vertx-web/src/main/resources/application.properties new file mode 100644 index 00000000000000..3f10c491bace20 --- /dev/null +++ b/integration-tests/vertx-web/src/main/resources/application.properties @@ -0,0 +1 @@ +quarkus.http.sessions.mode=in-memory diff --git a/integration-tests/vertx-web/src/test/java/io/quarkus/it/vertx/websessions/CounterTest.java b/integration-tests/vertx-web/src/test/java/io/quarkus/it/vertx/websessions/CounterTest.java new file mode 100644 index 00000000000000..79acd3a66a5847 --- /dev/null +++ b/integration-tests/vertx-web/src/test/java/io/quarkus/it/vertx/websessions/CounterTest.java @@ -0,0 +1,89 @@ +package io.quarkus.it.vertx.websessions; + +import static io.restassured.RestAssured.when; +import static io.restassured.RestAssured.with; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.filter.session.SessionFilter; +import io.restassured.response.Response; + +@QuarkusTest +public class CounterTest { + @Test + public void test() throws InterruptedException { + when().get("/check-sessions").then().statusCode(200).body(Matchers.is("OK")); + + List users = new ArrayList<>(); + for (int i = 0; i < 50; i++) { + users.add(new User(100)); + } + + for (User user : users) { + user.start(); + } + for (User user : users) { + user.join(); + } + for (User user : users) { + user.verify(); + } + } + + static class User extends Thread { + private static final AtomicInteger counter = new AtomicInteger(); + + private final Set sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>()); + private final Queue responses = new ConcurrentLinkedQueue<>(); + + private final int requests; + + User(int requests) { + super("User" + counter.incrementAndGet()); + this.requests = requests; + } + + @Override + public void run() { + SessionFilter sessions = new SessionFilter(); + for (int i = 0; i < requests; i++) { + Response response = with().filter(sessions).get("/counter"); + if (response.sessionId() != null) { + sessionIds.add(response.sessionId()); + } + responses.add(response.body().asString()); + + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(50)); + } catch (InterruptedException e) { + return; + } + } + } + + public void verify() { + assertEquals(1, sessionIds.size()); + String id = sessionIds.iterator().next(); + + assertEquals(requests, responses.size()); + int i = 1; + for (String response : responses) { + assertEquals(id + "|" + i, response); + i++; + } + } + } +}