Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2025-06-10 11:52:37 +02:00
5 changed files with 90 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -19,11 +19,11 @@ package org.springframework.web.server.session;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.web.server.WebSession;
@@ -35,10 +35,11 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* Tests for {@link InMemoryWebSessionStore}.
*
* @author Rob Winch
* @author Sam Brannen
*/
class InMemoryWebSessionStoreTests {
private InMemoryWebSessionStore store = new InMemoryWebSessionStore();
private final InMemoryWebSessionStore store = new InMemoryWebSessionStore();
@Test
@@ -53,13 +54,14 @@ class InMemoryWebSessionStoreTests {
void startsSessionImplicitly() {
WebSession session = this.store.createWebSession().block();
assertThat(session).isNotNull();
session.start();
// We intentionally do not invoke start().
// session.start();
session.getAttributes().put("foo", "bar");
assertThat(session.isStarted()).isTrue();
}
@Test // gh-24027, gh-26958
public void createSessionDoesNotBlock() {
void createSessionDoesNotBlock() {
this.store.createWebSession()
.doOnNext(session -> assertThat(Schedulers.isInNonBlockingThread()).isTrue())
.block();
@@ -103,7 +105,7 @@ class InMemoryWebSessionStoreTests {
}
@Test // SPR-17051
public void sessionInvalidatedBeforeSave() {
void sessionInvalidatedBeforeSave() {
// Request 1 creates session
WebSession session1 = this.store.createWebSession().block();
assertThat(session1).isNotNull();
@@ -132,33 +134,69 @@ class InMemoryWebSessionStoreTests {
@Test
void expirationCheckPeriod() {
DirectFieldAccessor accessor = new DirectFieldAccessor(this.store);
Map<?,?> sessions = (Map<?, ?>) accessor.getPropertyValue("sessions");
assertThat(sessions).isNotNull();
// Create 100 sessions
IntStream.range(0, 100).forEach(i -> insertSession());
assertThat(sessions).hasSize(100);
IntStream.rangeClosed(1, 100).forEach(i -> insertSession());
assertNumSessions(100);
// Force a new clock (31 min later), don't use setter which would clean expired sessions
// Force a new clock (31 min later). Don't use setter which would clean expired sessions.
DirectFieldAccessor accessor = new DirectFieldAccessor(this.store);
accessor.setPropertyValue("clock", Clock.offset(this.store.getClock(), Duration.ofMinutes(31)));
assertThat(sessions).hasSize(100);
assertNumSessions(100);
// Create 1 more which forces a time-based check (clock moved forward)
// Create 1 more which forces a time-based check (clock moved forward).
insertSession();
assertThat(sessions).hasSize(1);
assertNumSessions(1);
}
@Test
void maxSessions() {
this.store.setMaxSessions(10);
IntStream.range(0, 10000).forEach(i -> insertSession());
assertThatIllegalStateException().isThrownBy(
this::insertSession)
.withMessage("Max sessions limit reached: 10000");
IntStream.rangeClosed(1, 10).forEach(i -> insertSession());
assertThatIllegalStateException()
.isThrownBy(this::insertSession)
.withMessage("Max sessions limit reached: 10");
}
@Test
void updateSession() {
WebSession session = insertSession();
StepVerifier.create(session.save())
.expectComplete()
.verify();
}
@Test // gh-35013
void updateSessionAfterMaxSessionLimitIsExceeded() {
this.store.setMaxSessions(10);
WebSession session = insertSession();
assertNumSessions(1);
IntStream.rangeClosed(1, 9).forEach(i -> insertSession());
assertNumSessions(10);
// Updating an existing session should succeed.
StepVerifier.create(session.save())
.expectComplete()
.verify();
assertNumSessions(10);
// Saving an additional new session should fail.
assertThatIllegalStateException()
.isThrownBy(this::insertSession)
.withMessage("Max sessions limit reached: 10");
assertNumSessions(10);
// Updating an existing session again should still succeed.
StepVerifier.create(session.save())
.expectComplete()
.verify();
assertNumSessions(10);
}
private WebSession insertSession() {
WebSession session = this.store.createWebSession().block();
assertThat(session).isNotNull();
@@ -167,4 +205,8 @@ class InMemoryWebSessionStoreTests {
return session;
}
private void assertNumSessions(int numSessions) {
assertThat(store.getSessions()).hasSize(numSessions);
}
}