Fix MapReactorSessionRepository's delete().
This commit is contained in:
committed by
Rob Winch
parent
f455df3333
commit
84e7fbace1
@@ -123,17 +123,10 @@ public class MapReactorSessionRepository implements ReactorSessionRepository<Map
|
||||
}
|
||||
|
||||
public Mono<MapSession> findById(String id) {
|
||||
return Mono.defer(() -> {
|
||||
Session saved = this.sessions.get(id);
|
||||
if (saved == null) {
|
||||
return Mono.empty();
|
||||
}
|
||||
if (saved.isExpired()) {
|
||||
delete(saved.getId());
|
||||
return Mono.empty();
|
||||
}
|
||||
return Mono.just(new MapSession(saved));
|
||||
});
|
||||
return Mono.defer(() -> Mono.justOrEmpty(this.sessions.get(id))
|
||||
.filter(session -> !session.isExpired())
|
||||
.map(MapSession::new)
|
||||
.switchIfEmpty(delete(id).then(Mono.empty())));
|
||||
}
|
||||
|
||||
public Mono<Void> delete(String id) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MapReactorSessionRepositoryTests {
|
||||
|
||||
MapReactorSessionRepository repository;
|
||||
|
||||
MapSession session;
|
||||
@@ -124,6 +126,19 @@ public class MapReactorSessionRepositoryTests {
|
||||
assertThat(this.repository.findById(this.session.getId()).block()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByIdWhenExpiredRemovesFromSessionMap() {
|
||||
this.session.setMaxInactiveInterval(Duration.ofMinutes(1));
|
||||
this.session.setLastAccessedTime(Instant.now().minus(5, ChronoUnit.MINUTES));
|
||||
|
||||
Map<String, Session> sessions = new ConcurrentHashMap<>();
|
||||
sessions.put("session-id", this.session);
|
||||
this.repository = new MapReactorSessionRepository(sessions);
|
||||
|
||||
assertThat(this.repository.findById(this.session.getId()).block()).isNull();
|
||||
assertThat(sessions).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSessionWhenDefaultMaxInactiveIntervalThenDefaultMaxInactiveInterval() {
|
||||
Session session = this.repository.createSession().block();
|
||||
|
||||
Reference in New Issue
Block a user