test for programmatic configuration of RedissonSessionmanager

Signed-off-by: Jakub Glapa <jakub.glapa@gmail.com>
pull/6179/head
Jakub Glapa 4 months ago
parent 0c5392128a
commit 2040332fe5

@ -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<Object[]> 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 {

@ -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);
}
/**

@ -0,0 +1,4 @@
<?xml version='1.0' encoding='utf-8'?>
<Context>
</Context>
Loading…
Cancel
Save