Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for Spring session integration #452
- Loading branch information
Nikita
committed
Nov 22, 2016
1 parent
1611bf1
commit 922d5b8
Showing
8 changed files
with
528 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
redisson/src/test/java/org/redisson/spring/session/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
redisson/src/test/java/org/redisson/spring/session/ConfigTimeout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
redisson/src/test/java/org/redisson/spring/session/Initializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
239 changes: 239 additions & 0 deletions
239
redisson/src/test/java/org/redisson/spring/session/RedissonSessionManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
redisson/src/test/java/org/redisson/spring/session/SessionEventsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.