From 2040332fe520b9733c5447afda0ba776026bf817 Mon Sep 17 00:00:00 2001 From: Jakub Glapa Date: Thu, 19 Sep 2024 12:42:36 +0200 Subject: [PATCH] test for programmatic configuration of RedissonSessionmanager Signed-off-by: Jakub Glapa --- .../tomcat/RedissonSessionManagerTest.java | 47 +++++++++++++++++++ .../org/redisson/tomcat/TomcatServer.java | 12 ++++- .../test/webapp/META-INF/context_empty.xml | 4 ++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 redisson-tomcat/redisson-tomcat-9/src/test/webapp/META-INF/context_empty.xml diff --git a/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java b/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java index 5e34d55ee..f7ed0f421 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java +++ b/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java @@ -1,9 +1,11 @@ package org.redisson.tomcat; +import org.apache.catalina.Manager; import org.apache.http.client.fluent.Executor; import org.apache.http.client.fluent.Request; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; +import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -18,6 +20,9 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.concurrent.TimeUnit; +import static org.redisson.tomcat.RedissonSessionManager.ReadMode.MEMORY; +import static org.redisson.tomcat.RedissonSessionManager.UpdateMode.AFTER_REQUEST; + public class RedissonSessionManagerTest { public static Iterable data() { @@ -35,6 +40,48 @@ public class RedissonSessionManagerTest { Files.copy(Paths.get(basePath + contextName), Paths.get(basePath + "context.xml")); } + private Manager prepareManager() { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + + RedissonSessionManager redissonSessionManager = new RedissonSessionManager(); + redissonSessionManager.setReadMode(MEMORY.name()); + redissonSessionManager.setUpdateMode(AFTER_REQUEST.name()); + redissonSessionManager.setBroadcastSessionEvents(true); + redissonSessionManager.setBroadcastSessionUpdates(true); + redissonSessionManager.setConfig(config); + + return redissonSessionManager; + } + + @Test + public void testProgrammaticManagerConfigurationUpdateTwoServers_readValue() throws Exception { + prepare("context_empty.xml"); + TomcatServer server1 = new TomcatServer("myapp", 8080, "src/test/"); + server1.setManager(prepareManager()); + TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/"); + server2.setManager(prepareManager()); + try { + server1.start(); + server2.start(); + + Executor executor = Executor.newInstance(); + BasicCookieStore cookieStore = new BasicCookieStore(); + executor.use(cookieStore); + + write(8080, executor, "test", "from_server1"); + write(8081, executor, "test", "from_server2"); + + read(8080, executor, "test", "from_server2"); + read(8081, executor, "test", "from_server2"); + + } finally { + Executor.closeIdleConnections(); + server1.stop(); + server2.stop(); + } + } + @ParameterizedTest @MethodSource("data") public void testUpdateTwoServers_readValue(String contextName) throws Exception { diff --git a/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/TomcatServer.java b/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/TomcatServer.java index d18978fb3..55db289b0 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/TomcatServer.java +++ b/redisson-tomcat/redisson-tomcat-9/src/test/java/org/redisson/tomcat/TomcatServer.java @@ -4,7 +4,9 @@ import java.net.MalformedURLException; import javax.servlet.ServletException; +import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; +import org.apache.catalina.Manager; import org.apache.catalina.startup.Tomcat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,6 +14,7 @@ import org.slf4j.LoggerFactory; public class TomcatServer { private Tomcat tomcat = new Tomcat(); + private Context context; private int port; private boolean isRunning; @@ -30,7 +33,14 @@ public class TomcatServer { tomcat.setPort(port); tomcat.getHost().setAppBase("."); - tomcat.addWebapp(contextPath, appBase + "/webapp"); + context = tomcat.addWebapp(contextPath, appBase + "/webapp"); + } + + void setManager(Manager manager) { + if (context.getManager() != null) { + throw new IllegalArgumentException("Manager already configured"); + } + context.setManager(manager); } /** diff --git a/redisson-tomcat/redisson-tomcat-9/src/test/webapp/META-INF/context_empty.xml b/redisson-tomcat/redisson-tomcat-9/src/test/webapp/META-INF/context_empty.xml new file mode 100644 index 000000000..dd1d6b33b --- /dev/null +++ b/redisson-tomcat/redisson-tomcat-9/src/test/webapp/META-INF/context_empty.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file