Skip to content

Commit

Permalink
WIP add tests for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Oct 17, 2023
1 parent 9bb97e7 commit 5b55897
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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<User> 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<String> sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Queue<String> 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++;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
@@ -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<User> 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<String> sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Queue<String> 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++;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.http.sessions.mode=in-memory
Original file line number Diff line number Diff line change
@@ -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<User> 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<String> sessionIds = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Queue<String> 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++;
}
}
}
}

0 comments on commit 5b55897

Please sign in to comment.