Skip to content

Commit

Permalink
Fix MapReactorSessionRepository's delete().
Browse files Browse the repository at this point in the history
  • Loading branch information
gregturn authored and rwinch committed Aug 24, 2017
1 parent f455df3 commit 84e7fba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,10 @@ private void performSave(MapSession session) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -33,6 +34,7 @@
* @since 2.0
*/
public class MapReactorSessionRepositoryTests {

MapReactorSessionRepository repository;

MapSession session;
Expand Down Expand Up @@ -124,6 +126,19 @@ public void findByIdWhenExpiredThenEmpty() {
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();
Expand Down

0 comments on commit 84e7fba

Please sign in to comment.