Skip to content

Commit

Permalink
MongoSession should be constructed using defaultMaxInactiveInterval
Browse files Browse the repository at this point in the history
Closes gh-2910
  • Loading branch information
dlscjf151 authored and marcusdacoregio committed Aug 21, 2024
1 parent ad333a4 commit d989024
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public MongoIndexedSessionRepository(MongoOperations mongoOperations) {
@Override
public MongoSession createSession() {

MongoSession session = new MongoSession(this.sessionIdGenerator);

session.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
MongoSession session = new MongoSession(this.sessionIdGenerator, this.defaultMaxInactiveInterval.toSeconds());

publishEvent(new SessionCreatedEvent(this, session));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public ReactiveMongoSessionRepository(ReactiveMongoOperations mongoOperations) {
public Mono<MongoSession> createSession() {
// @formatter:off
return Mono.fromSupplier(() -> this.sessionIdGenerator.generate())
.map(MongoSession::new)
.zipWith(Mono.just(this.defaultMaxInactiveInterval.toSeconds()))
.map((tuple) -> new MongoSession(tuple.getT1(), tuple.getT2()))
.doOnNext((mongoSession) -> mongoSession.setMaxInactiveInterval(this.defaultMaxInactiveInterval))
.doOnNext(
(mongoSession) -> mongoSession.setSessionIdGenerator(this.sessionIdGenerator))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.session.data.mongo;

import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -254,6 +256,14 @@ void findByIdWhenChangeSessionIdThenUsesSessionIdGenerator() {
assertThat(newSessionId).isEqualTo("456");
}

@Test
void createSessionWhenMaxInactiveIntervalSetThenUse() {
this.repository.setDefaultMaxInactiveInterval(Duration.ofSeconds(60));
MongoSession session = this.repository.createSession();
Instant now = Instant.now();
assertThat(session.getExpireAt()).isBetween(now.plusSeconds(59), Instant.now().plusSeconds(61));
}

static class FixedSessionIdGenerator implements SessionIdGenerator {

private final String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.session.data.mongo;

import java.time.Duration;
import java.time.Instant;
import java.util.UUID;

import com.mongodb.BasicDBObject;
Expand Down Expand Up @@ -259,4 +260,12 @@ void findByIdWhenChangeSessionIdThenUsesSessionIdGenerator() {
}).verifyComplete();
}

@Test
void createSessionWhenMaxInactiveIntervalSetThenUse() {
this.repository.setDefaultMaxInactiveInterval(Duration.ofSeconds(60));
MongoSession session = this.repository.createSession().block();
Instant now = Instant.now();
assertThat(session.getExpireAt()).isBetween(now.plusSeconds(59), Instant.now().plusSeconds(61));
}

}

0 comments on commit d989024

Please sign in to comment.