Support External Hazelcast instance
Previously the Hazelcast support only worked with embedded Hazelcast instances. This commit ensures that Hazelcast support works with external Hazelcast instances. Fixes gh-339
This commit is contained in:
@@ -59,8 +59,7 @@ import com.hazelcast.core.HazelcastInstance;
|
||||
@WebAppConfiguration
|
||||
public class EnableHazelcastHttpSessionEventsTests<S extends ExpiringSession> {
|
||||
|
||||
private final static String MAX_INACTIVE_INTERVAL_IN_SECONDS_STR = "1";
|
||||
private final static int MAX_INACTIVE_INTERVAL_IN_SECONDS = Integer.valueOf(MAX_INACTIVE_INTERVAL_IN_SECONDS_STR);
|
||||
private final static int MAX_INACTIVE_INTERVAL_IN_SECONDS = 1;
|
||||
|
||||
@Autowired
|
||||
private SessionRepository<S> repository;
|
||||
@@ -143,9 +142,29 @@ public class EnableHazelcastHttpSessionEventsTests<S extends ExpiringSession> {
|
||||
assertThat(repository.getSession(sessionToSave.getId())).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveUpdatesTimeToLiveTest() throws InterruptedException {
|
||||
S sessionToSave = repository.createSession();
|
||||
|
||||
repository.save(sessionToSave);
|
||||
|
||||
synchronized (lock) {
|
||||
lock.wait((sessionToSave.getMaxInactiveIntervalInSeconds() * 1000) - 500);
|
||||
}
|
||||
|
||||
// Get and save the session like SessionRepositoryFilter would.
|
||||
S sessionToUpdate = repository.getSession(sessionToSave.getId());
|
||||
repository.save(sessionToUpdate);
|
||||
|
||||
synchronized (lock) {
|
||||
lock.wait((sessionToUpdate.getMaxInactiveIntervalInSeconds() * 1000) - 100);
|
||||
}
|
||||
|
||||
assertThat(repository.getSession(sessionToUpdate.getId())).isNotNull();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableHazelcastHttpSession(maxInactiveIntervalInSeconds = MAX_INACTIVE_INTERVAL_IN_SECONDS_STR)
|
||||
@EnableHazelcastHttpSession(maxInactiveIntervalInSeconds = MAX_INACTIVE_INTERVAL_IN_SECONDS)
|
||||
static class HazelcastSessionConfig {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -43,59 +43,6 @@ import com.hazelcast.core.HazelcastInstance;
|
||||
*/
|
||||
public class HazelcastHttpSessionConfigurationXmlTests<S extends ExpiringSession> {
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@WebAppConfiguration
|
||||
public static class CustomXmlInactiveIntervalTest<S extends ExpiringSession> {
|
||||
|
||||
@Autowired
|
||||
private SessionRepository<S> repository;
|
||||
|
||||
@Autowired
|
||||
private HazelcastInstance hazelcast;
|
||||
|
||||
@Test
|
||||
public void saveSessionTest() throws InterruptedException {
|
||||
|
||||
S sessionToSave = repository.createSession();
|
||||
|
||||
assertThat(sessionToSave.getMaxInactiveIntervalInSeconds())
|
||||
.isEqualTo(150);
|
||||
|
||||
repository.save(sessionToSave);
|
||||
|
||||
S session = repository.getSession(sessionToSave.getId());
|
||||
|
||||
assertThat(session.getId()).isEqualTo(sessionToSave.getId());
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds())
|
||||
.isEqualTo(150);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUnderlyingMapSettingsTest() {
|
||||
assertThat(
|
||||
hazelcast.getConfig()
|
||||
.getMapConfig("spring:session:sessions")
|
||||
.getMaxIdleSeconds())
|
||||
.isEqualTo(150);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableHazelcastHttpSession(maxInactiveIntervalInSeconds = "")
|
||||
static class HazelcastSessionXmlConfigCustomIdle {
|
||||
|
||||
@Bean
|
||||
public HazelcastInstance embeddedHazelcast() {
|
||||
Config hazelcastConfig = new ClasspathXmlConfig(
|
||||
"org/springframework/session/hazelcast/config/annotation/web/http/hazelcast-custom-idle-time.xml");
|
||||
NetworkConfig netConfig = new NetworkConfig();
|
||||
netConfig.setPort(SocketUtils.findAvailableTcpPort());
|
||||
hazelcastConfig.setNetworkConfig(netConfig);
|
||||
return Hazelcast.newHazelcastInstance(hazelcastConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@WebAppConfiguration
|
||||
@@ -104,9 +51,6 @@ public class HazelcastHttpSessionConfigurationXmlTests<S extends ExpiringSession
|
||||
@Autowired
|
||||
private SessionRepository<S> repository;
|
||||
|
||||
@Autowired
|
||||
private HazelcastInstance hazelcast;
|
||||
|
||||
@Test
|
||||
public void saveSessionTest() throws InterruptedException {
|
||||
|
||||
@@ -120,15 +64,6 @@ public class HazelcastHttpSessionConfigurationXmlTests<S extends ExpiringSession
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(1800);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUnderlyingMapSettingsTest() {
|
||||
assertThat(
|
||||
hazelcast.getConfig()
|
||||
.getMapConfig("my-sessions")
|
||||
.getMaxIdleSeconds())
|
||||
.isEqualTo(1800);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableHazelcastHttpSession(sessionMapName = "my-sessions")
|
||||
static class HazelcastSessionXmlConfigCustomMapName {
|
||||
@@ -153,9 +88,6 @@ public class HazelcastHttpSessionConfigurationXmlTests<S extends ExpiringSession
|
||||
@Autowired
|
||||
private SessionRepository<S> repository;
|
||||
|
||||
@Autowired
|
||||
private HazelcastInstance hazelcast;
|
||||
|
||||
@Test
|
||||
public void saveSessionTest() throws InterruptedException {
|
||||
|
||||
@@ -169,17 +101,8 @@ public class HazelcastHttpSessionConfigurationXmlTests<S extends ExpiringSession
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(1200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUnderlyingMapSettingsTest() {
|
||||
assertThat(
|
||||
hazelcast.getConfig()
|
||||
.getMapConfig("test-sessions")
|
||||
.getMaxIdleSeconds())
|
||||
.isEqualTo(1200);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableHazelcastHttpSession(sessionMapName = "test-sessions", maxInactiveIntervalInSeconds = "1200")
|
||||
@EnableHazelcastHttpSession(sessionMapName = "test-sessions", maxInactiveIntervalInSeconds = 1200)
|
||||
static class HazelcastSessionXmlConfigCustomMapNameAndIdle {
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user