Spring session test deleted

pull/5427/head
Nikita Koksharov 1 year ago
parent bae989b6fc
commit 4c783d36c2

@ -1,32 +0,0 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.redisson.spring.session.config.EnableRedissonWebSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@EnableRedissonHttpSession
//@EnableRedissonWebSession
public class HttpConfig {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
@Bean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME)
public WebHandler dispatcherHandler(ApplicationContext context) {
return new DispatcherHandler(context);
}
}

@ -1,21 +0,0 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.springframework.context.annotation.Bean;
@EnableRedissonHttpSession(maxInactiveIntervalInSeconds = 5)
public class HttpConfigTimeout {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
}

@ -1,16 +0,0 @@
package org.redisson.spring.session;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
public class HttpInitializer extends AbstractHttpSessionApplicationInitializer {
public static Class<?> CONFIG_CLASS = HttpConfig.class;
public HttpInitializer() {
super(CONFIG_CLASS);
}
@Override
public void onStartup(jakarta.servlet.ServletContext servletContext) {
}
}

@ -1,244 +0,0 @@
package org.redisson.spring.session;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
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.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.redisson.RedisRunner;
import org.redisson.RedisRunner.KEYSPACE_EVENTS_OPTIONS;
import org.redisson.RedissonRuntimeEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
@Deprecated
public class RedissonSessionManagerTest {
private static RedisRunner.RedisProcess defaultRedisInstance;
@AfterAll
public static void afterClass() throws IOException, InterruptedException {
defaultRedisInstance.stop();
}
@BeforeAll
public static void beforeClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
defaultRedisInstance = new RedisRunner()
.nosave()
.port(6379)
.randomDir()
.notifyKeyspaceEvents(KEYSPACE_EVENTS_OPTIONS.E,
KEYSPACE_EVENTS_OPTIONS.x,
KEYSPACE_EVENTS_OPTIONS.g)
.run();
}
}
@Test
public void testSwitchServer() throws Exception {
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
Executor executor = Executor.newInstance();
BasicCookieStore cookieStore = new BasicCookieStore();
executor.use(cookieStore);
write(executor, "test", "1234");
Cookie cookie = cookieStore.getCookies().get(0);
Executor.closeIdleConnections();
server.stop();
server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
executor = Executor.newInstance();
cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
executor.use(cookieStore);
read(executor, "test", "1234");
remove(executor, "test", "null");
Executor.closeIdleConnections();
server.stop();
}
@Test
public void testWriteReadRemove() throws Exception {
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
Executor executor = Executor.newInstance();
write(executor, "test", "1234");
read(executor, "test", "1234");
remove(executor, "test", "null");
Executor.closeIdleConnections();
server.stop();
}
@Test
public void testRecreate() throws Exception {
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
Executor executor = Executor.newInstance();
write(executor, "test", "1");
recreate(executor, "test", "2");
read(executor, "test", "2");
Executor.closeIdleConnections();
server.stop();
}
@Test
public void testUpdate() throws Exception {
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
Executor executor = Executor.newInstance();
write(executor, "test", "1");
read(executor, "test", "1");
write(executor, "test", "2");
read(executor, "test", "2");
Executor.closeIdleConnections();
server.stop();
}
@Test
public void testExpire() throws Exception {
HttpInitializer.CONFIG_CLASS = HttpConfigTimeout.class;
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
Executor executor = Executor.newInstance();
BasicCookieStore cookieStore = new BasicCookieStore();
executor.use(cookieStore);
write(executor, "test", "1234");
Cookie cookie = cookieStore.getCookies().get(0);
Thread.sleep(50);
Assertions.assertEquals(1, listener.getSessionCreatedEvents());
Assertions.assertEquals(0, listener.getSessionExpiredEvents());
Executor.closeIdleConnections();
Thread.sleep(6000);
Assertions.assertEquals(1, listener.getSessionCreatedEvents());
Assertions.assertEquals(1, listener.getSessionExpiredEvents());
executor = Executor.newInstance();
cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
executor.use(cookieStore);
read(executor, "test", "null");
Thread.sleep(50);
Assertions.assertEquals(2, listener.getSessionCreatedEvents());
write(executor, "test", "1234");
Thread.sleep(3000);
read(executor, "test", "1234");
Thread.sleep(3000);
Assertions.assertEquals(1, listener.getSessionExpiredEvents());
Thread.sleep(1000);
Assertions.assertEquals(1, listener.getSessionExpiredEvents());
Thread.sleep(3000);
Assertions.assertEquals(2, listener.getSessionExpiredEvents());
Executor.closeIdleConnections();
server.stop();
}
@Test
public void testInvalidate() throws Exception {
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();
WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
Executor executor = Executor.newInstance();
BasicCookieStore cookieStore = new BasicCookieStore();
executor.use(cookieStore);
write(executor, "test", "1234");
Cookie cookie = cookieStore.getCookies().get(0);
Thread.sleep(50);
Assertions.assertEquals(1, listener.getSessionCreatedEvents());
Assertions.assertEquals(0, listener.getSessionDeletedEvents());
invalidate(executor);
Assertions.assertEquals(1, listener.getSessionCreatedEvents());
Assertions.assertEquals(1, listener.getSessionDeletedEvents());
Executor.closeIdleConnections();
executor = Executor.newInstance();
cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
executor.use(cookieStore);
read(executor, "test", "null");
Executor.closeIdleConnections();
server.stop();
}
private void write(Executor executor, String key, String value) throws IOException, ClientProtocolException {
String url = "http://localhost:8080/myapp/write?key=" + key + "&value=" + value;
String response = executor.execute(Request.Get(url)).returnContent().asString();
Assertions.assertEquals("OK", response);
}
private void read(Executor executor, String key, String value) throws IOException, ClientProtocolException {
String url = "http://localhost:8080/myapp/read?key=" + key;
String response = executor.execute(Request.Get(url)).returnContent().asString();
Assertions.assertEquals(value, response);
}
private void remove(Executor executor, String key, String value) throws IOException, ClientProtocolException {
String url = "http://localhost:8080/myapp/remove?key=" + key;
String response = executor.execute(Request.Get(url)).returnContent().asString();
Assertions.assertEquals(value, response);
}
private void invalidate(Executor executor) throws IOException, ClientProtocolException {
String url = "http://localhost:8080/myapp/invalidate";
String response = executor.execute(Request.Get(url)).returnContent().asString();
Assertions.assertEquals("OK", response);
}
private void recreate(Executor executor, String key, String value) throws IOException, ClientProtocolException {
String url = "http://localhost:8080/myapp/recreate?key=" + key + "&value=" + value;
String response = executor.execute(Request.Get(url)).returnContent().asString();
Assertions.assertEquals("OK", response);
}
}

@ -1,40 +0,0 @@
package org.redisson.spring.session;
import org.springframework.context.ApplicationListener;
import org.springframework.session.events.AbstractSessionEvent;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent;
public class SessionEventsListener implements ApplicationListener<AbstractSessionEvent> {
private int sessionCreatedEvents;
private int sessionDeletedEvents;
private int sessionExpiredEvents;
@Override
public void onApplicationEvent(AbstractSessionEvent event) {
if (event instanceof SessionCreatedEvent) {
sessionCreatedEvents++;
}
if (event instanceof SessionDeletedEvent) {
sessionDeletedEvents++;
}
if (event instanceof SessionExpiredEvent) {
sessionExpiredEvents++;
}
}
public int getSessionCreatedEvents() {
return sessionCreatedEvents;
}
public int getSessionDeletedEvents() {
return sessionDeletedEvents;
}
public int getSessionExpiredEvents() {
return sessionExpiredEvents;
}
}

@ -1,96 +0,0 @@
package org.redisson.spring.session;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "/testServlet", urlPatterns = "/*")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1243830648280853203L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
if (req.getPathInfo().equals("/write")) {
String[] params = req.getQueryString().split("&");
String key = null;
String value = null;
for (String param : params) {
String[] paramLine = param.split("=");
String keyParam = paramLine[0];
String valueParam = paramLine[1];
if ("key".equals(keyParam)) {
key = valueParam;
}
if ("value".equals(keyParam)) {
value = valueParam;
}
}
session.setAttribute(key, value);
resp.getWriter().print("OK");
} else if (req.getPathInfo().equals("/read")) {
String[] params = req.getQueryString().split("&");
String key = null;
for (String param : params) {
String[] line = param.split("=");
String keyParam = line[0];
if ("key".equals(keyParam)) {
key = line[1];
}
}
Object attr = session.getAttribute(key);
resp.getWriter().print(attr);
} else if (req.getPathInfo().equals("/remove")) {
String[] params = req.getQueryString().split("&");
String key = null;
for (String param : params) {
String[] line = param.split("=");
String keyParam = line[0];
if ("key".equals(keyParam)) {
key = line[1];
}
}
session.removeAttribute(key);
resp.getWriter().print(String.valueOf(session.getAttribute(key)));
} else if (req.getPathInfo().equals("/invalidate")) {
session.invalidate();
resp.getWriter().print("OK");
} else if (req.getPathInfo().equals("/recreate")) {
session.invalidate();
session = req.getSession();
String[] params = req.getQueryString().split("&");
String key = null;
String value = null;
for (String param : params) {
String[] paramLine = param.split("=");
String keyParam = paramLine[0];
String valueParam = paramLine[1];
if ("key".equals(keyParam)) {
key = valueParam;
}
if ("value".equals(keyParam)) {
value = valueParam;
}
}
session.setAttribute(key, value);
resp.getWriter().print("OK");
}
}
}

@ -1,64 +0,0 @@
package org.redisson.spring.session;
import java.io.File;
import java.net.MalformedURLException;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
public class TomcatServer {
private Tomcat tomcat = new Tomcat();
private StandardContext ctx;
public TomcatServer(String contextPath, int port, String appBase) throws MalformedURLException, ServletException {
if(contextPath == null || appBase == null || appBase.length() == 0) {
throw new IllegalArgumentException("Context path or appbase should not be null");
}
if(!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
tomcat.setBaseDir("."); // location where temp dir is created
tomcat.setPort(port);
tomcat.getHost().setAppBase(".");
ctx = (StandardContext) tomcat.addWebapp(contextPath, appBase);
ctx.setDelegate(true);
File additionWebInfClasses = new File("target/test-classes");
StandardRoot resources = new StandardRoot();
DirResourceSet webResourceSet = new DirResourceSet();
webResourceSet.setBase(additionWebInfClasses.toString());
webResourceSet.setWebAppMount("/WEB-INF/classes");
resources.addPostResources(webResourceSet);
ctx.setResources(resources);
}
/**
* Start the tomcat embedded server
*/
public void start() throws LifecycleException {
tomcat.start();
}
/**
* Stop the tomcat embedded server
*/
public void stop() throws LifecycleException {
tomcat.stop();
tomcat.destroy();
tomcat.getServer().await();
}
public ServletContext getServletContext() {
return ctx.getServletContext();
}
}

@ -1,30 +0,0 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonWebSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@EnableRedissonWebSession
public class WebConfig {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
@Bean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME)
public WebHandler dispatcherHandler(ApplicationContext context) {
return new DispatcherHandler(context);
}
}

@ -1,14 +0,0 @@
package org.redisson.spring.session;
import org.springframework.web.server.adapter.AbstractReactiveWebInitializer;
public class WebInitializer extends AbstractReactiveWebInitializer {
public static Class<?> CONFIG_CLASS = HttpConfig.class;
@Override
protected Class<?>[] getConfigClasses() {
return new Class[] {CONFIG_CLASS};
}
}
Loading…
Cancel
Save