Skip to content

Commit

Permalink
tests for Spring session integration #452
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Nov 22, 2016
1 parent 1611bf1 commit 922d5b8
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 0 deletions.
37 changes: 37 additions & 0 deletions redisson/pom.xml
Expand Up @@ -113,6 +113,43 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.73</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.73</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.73</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>7.0.73</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>[3.1,)</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions redisson/src/test/java/org/redisson/spring/session/Config.java
@@ -0,0 +1,21 @@
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
public class Config {

@Bean
public RedissonClient redisson() {
return Redisson.create();
}

@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}

}
@@ -0,0 +1,21 @@
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 ConfigTimeout {

@Bean
public RedissonClient redisson() {
return Redisson.create();
}

@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}

}
@@ -0,0 +1,13 @@
package org.redisson.spring.session;

import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;

public class Initializer extends AbstractHttpSessionApplicationInitializer {

public static Class<?> CONFIG_CLASS = Config.class;

public Initializer() {
super(CONFIG_CLASS);
}

}
@@ -0,0 +1,239 @@
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.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.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;

public class RedissonSessionManagerTest {

private static RedisRunner.RedisProcess defaultRedisInstance;

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (!RedissonRuntimeEnvironment.isTravis) {
defaultRedisInstance.stop();
}
}

@BeforeClass
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 {
Initializer.CONFIG_CLASS = ConfigTimeout.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);

Assert.assertEquals(1, listener.getSessionCreatedEvents());
Assert.assertEquals(0, listener.getSessionExpiredEvents());

Executor.closeIdleConnections();

Thread.sleep(6000);

Assert.assertEquals(1, listener.getSessionCreatedEvents());
Assert.assertEquals(1, listener.getSessionExpiredEvents());

executor = Executor.newInstance();
cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
executor.use(cookieStore);
read(executor, "test", "null");

Assert.assertEquals(2, listener.getSessionCreatedEvents());

write(executor, "test", "1234");
Thread.sleep(3000);
read(executor, "test", "1234");
Thread.sleep(3000);
Assert.assertEquals(1, listener.getSessionExpiredEvents());
Thread.sleep(1000);
Assert.assertEquals(1, listener.getSessionExpiredEvents());
Thread.sleep(3000);
Assert.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);

Assert.assertEquals(1, listener.getSessionCreatedEvents());
Assert.assertEquals(0, listener.getSessionDeletedEvents());

invalidate(executor);

Assert.assertEquals(1, listener.getSessionCreatedEvents());
Assert.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();
Assert.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();
Assert.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();
Assert.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();
Assert.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();
Assert.assertEquals("OK", response);
}

}
@@ -0,0 +1,40 @@
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;
}

}

0 comments on commit 922d5b8

Please sign in to comment.