Skip to content

ReactiveHazelcastSessionRepository #2222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions spring-session-hazelcast/hazelcast4/hazelcast4.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ artifacts {
dependencies {
api project(':spring-session-core')
optional "com.hazelcast:hazelcast:4.2.4"
optional "io.projectreactor:reactor-core"
optional "org.springframework:spring-web"
api "org.springframework:spring-context"
api "jakarta.annotation:jakarta.annotation-api"

Expand All @@ -32,10 +34,11 @@ dependencies {
testImplementation "org.springframework:spring-web"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.springframework.security:spring-security-core"
testImplementation "io.projectreactor:reactor-test"
testImplementation "com.hazelcast:hazelcast:4.2.4"
testImplementation project(":spring-session-hazelcast")
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"

integrationTestCompile "org.testcontainers:testcontainers"
integrationTestCompile "com.hazelcast:hazelcast:4.2.4"
integrationTestCompile project(":spring-session-hazelcast")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.session.hazelcast;

import java.time.Duration;
import java.time.Instant;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.MapSession;
import org.springframework.session.hazelcast.ReactiveHazelcastSessionRepository.HazelcastSession;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Base class for {@link ReactiveHazelcastSessionRepository} integration tests.
*
* @author Eleftheria Stein
* @author Didier Loiseau
*/
abstract class AbstractReactiveHazelcastSessionRepositoryITests {

@Autowired
private HazelcastInstance hazelcastInstance;

@Autowired
private ReactiveHazelcastSessionRepository repository;

@Test
void createAndDestroySession() {
HazelcastSession sessionToSave = this.repository.createSession().block();
String sessionId = sessionToSave.getId();

IMap<String, MapSession> hazelcastMap = this.hazelcastInstance
.getMap(ReactiveHazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME);

this.repository.save(sessionToSave).block();

assertThat(hazelcastMap.get(sessionId)).isEqualTo(sessionToSave);

this.repository.deleteById(sessionId).block();

assertThat(hazelcastMap.get(sessionId)).isNull();
}

@Test
void changeSessionIdWhenOnlyChangeId() {
String attrName = "changeSessionId";
String attrValue = "changeSessionId-value";
HazelcastSession toSave = this.repository.createSession().block();
toSave.setAttribute(attrName, attrValue);

this.repository.save(toSave).block();

HazelcastSession findById = this.repository.findById(toSave.getId()).block();

assertThat(findById.<String>getAttribute(attrName)).isEqualTo(attrValue);

String originalFindById = findById.getId();
String changeSessionId = findById.changeSessionId();

this.repository.save(findById).block();

assertThat(this.repository.findById(originalFindById).block()).isNull();

HazelcastSession findByChangeSessionId = this.repository.findById(changeSessionId).block();

assertThat(findByChangeSessionId.<String>getAttribute(attrName)).isEqualTo(attrValue);

this.repository.deleteById(changeSessionId).block();
}

@Test
void changeSessionIdWhenChangeTwice() {
HazelcastSession toSave = this.repository.createSession().block();

this.repository.save(toSave).block();

String originalId = toSave.getId();
String changeId1 = toSave.changeSessionId();
String changeId2 = toSave.changeSessionId();

this.repository.save(toSave).block();

assertThat(this.repository.findById(originalId).block()).isNull();
assertThat(this.repository.findById(changeId1).block()).isNull();
assertThat(this.repository.findById(changeId2).block()).isNotNull();

this.repository.deleteById(changeId2).block();
}

@Test
void changeSessionIdWhenSetAttributeOnChangedSession() {
String attrName = "changeSessionId";
String attrValue = "changeSessionId-value";

HazelcastSession toSave = this.repository.createSession().block();

this.repository.save(toSave).block();

HazelcastSession findById = this.repository.findById(toSave.getId()).block();

findById.setAttribute(attrName, attrValue);

String originalFindById = findById.getId();
String changeSessionId = findById.changeSessionId();

this.repository.save(findById).block();

assertThat(this.repository.findById(originalFindById).block()).isNull();

HazelcastSession findByChangeSessionId = this.repository.findById(changeSessionId).block();

assertThat(findByChangeSessionId.<String>getAttribute(attrName)).isEqualTo(attrValue);

this.repository.deleteById(changeSessionId).block();
}

@Test
void changeSessionIdWhenHasNotSaved() {
HazelcastSession toSave = this.repository.createSession().block();
String originalId = toSave.getId();
toSave.changeSessionId();

this.repository.save(toSave).block();

assertThat(this.repository.findById(toSave.getId()).block()).isNotNull();
assertThat(this.repository.findById(originalId).block()).isNull();

this.repository.deleteById(toSave.getId()).block();
}

@Test // gh-1076
void attemptToUpdateSessionAfterDelete() {
HazelcastSession session = this.repository.createSession().block();
String sessionId = session.getId();
this.repository.save(session).block();
session = this.repository.findById(sessionId).block();
session.setAttribute("attributeName", "attributeValue");
this.repository.deleteById(sessionId).block();
this.repository.save(session).block();

assertThat(this.repository.findById(sessionId).block()).isNull();
}

@Test
void createAndUpdateSession() {
HazelcastSession session = this.repository.createSession().block();
String sessionId = session.getId();

this.repository.save(session).block();

session = this.repository.findById(sessionId).block();
session.setAttribute("attributeName", "attributeValue");

this.repository.save(session).block();

assertThat(this.repository.findById(sessionId).block()).isNotNull()
.extracting((s) -> s.getAttribute("attributeName")).isEqualTo("attributeValue");

this.repository.deleteById(sessionId).block();
}

@Test
void createAndUpdateSessionWhileKeepingOriginalTimeToLiveConfiguredOnRepository() {
final Duration defaultSessionTimeout = Duration.ofSeconds(1800);

final IMap<String, MapSession> hazelcastMap = this.hazelcastInstance
.getMap(ReactiveHazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME);

HazelcastSession session = this.repository.createSession().block();
String sessionId = session.getId();
this.repository.save(session).block();

assertThat(session.getMaxInactiveInterval()).isEqualTo(defaultSessionTimeout);
assertThat(hazelcastMap.getEntryView(sessionId).getTtl()).isEqualTo(defaultSessionTimeout.toMillis());

session = this.repository.findById(sessionId).block();
session.setLastAccessedTime(Instant.now());
this.repository.save(session).block();

session = this.repository.findById(sessionId).block();
assertThat(session.getMaxInactiveInterval()).isEqualTo(defaultSessionTimeout);
assertThat(hazelcastMap.getEntryView(sessionId).getTtl()).isEqualTo(defaultSessionTimeout.toMillis());
}

@Test
void createAndUpdateSessionWhileKeepingTimeToLiveSetOnSession() {
final Duration individualSessionTimeout = Duration.ofSeconds(23);

final IMap<String, MapSession> hazelcastMap = this.hazelcastInstance
.getMap(ReactiveHazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME);

HazelcastSession session = this.repository.createSession().block();
session.setMaxInactiveInterval(individualSessionTimeout);
String sessionId = session.getId();
this.repository.save(session).block();

assertThat(session.getMaxInactiveInterval()).isEqualTo(individualSessionTimeout);
assertThat(hazelcastMap.getEntryView(sessionId).getTtl()).isEqualTo(individualSessionTimeout.toMillis());

session = this.repository.findById(sessionId).block();
session.setAttribute("attribute", "value");
this.repository.save(session).block();

session = this.repository.findById(sessionId).block();
assertThat(session.getMaxInactiveInterval()).isEqualTo(individualSessionTimeout);
assertThat(hazelcastMap.getEntryView(sessionId).getTtl()).isEqualTo(individualSessionTimeout.toMillis());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.session.hazelcast;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.session.hazelcast.config.annotation.web.server.EnableHazelcastWebSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;

/**
* Integration tests for {@link ReactiveHazelcastSessionRepository} using client-server
* topology.
*
* @author Eleftheria Stein
* @author Didier Loiseau
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@WebAppConfiguration
class ClientServerReactiveHazelcastSessionRepositoryITests extends AbstractReactiveHazelcastSessionRepositoryITests {

private static GenericContainer container = new GenericContainer<>("hazelcast/hazelcast:4.2.4")
.withExposedPorts(5701).withCopyFileToContainer(MountableFile.forClasspathResource("/hazelcast-server.xml"),
"/opt/hazelcast/hazelcast.xml");

@BeforeAll
static void setUpClass() {
container.start();
}

@AfterAll
static void tearDownClass() {
container.stop();
}

@Configuration
@EnableHazelcastWebSession
static class HazelcastSessionConfig {

@Bean
HazelcastInstance hazelcastInstance() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig()
.addAddress(container.getContainerIpAddress() + ":" + container.getFirstMappedPort());
clientConfig.getUserCodeDeploymentConfig().setEnabled(true).addClass(Session.class)
.addClass(MapSession.class).addClass(Hazelcast4SessionUpdateEntryProcessor.class);
return HazelcastClient.newHazelcastClient(clientConfig);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.session.hazelcast;

import com.hazelcast.core.HazelcastInstance;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.hazelcast.config.annotation.web.server.EnableHazelcastWebSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;

/**
* Integration tests for {@link ReactiveHazelcastSessionRepository} using embedded
* topology.
*
* @author Eleftheria Stein
* @author Didier Loiseau
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@WebAppConfiguration
class EmbeddedReactiveHazelcastSessionRepositoryITests extends AbstractReactiveHazelcastSessionRepositoryITests {

@EnableHazelcastWebSession
@Configuration
static class HazelcastSessionConfig {

@Bean
HazelcastInstance hazelcastInstance() {
return Hazelcast4ITestUtils.embeddedHazelcastServer();
}

}

}
Loading