11/*
2- * Copyright 2002-2024 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import java .time .Clock ;
2020import java .time .Duration ;
2121import java .time .Instant ;
22- import java .util .Map ;
2322import java .util .stream .IntStream ;
2423
2524import org .junit .jupiter .api .Test ;
3534 * Tests for {@link InMemoryWebSessionStore}.
3635 *
3736 * @author Rob Winch
37+ * @author Sam Brannen
3838 */
3939class InMemoryWebSessionStoreTests {
4040
41- private InMemoryWebSessionStore store = new InMemoryWebSessionStore ();
41+ private final InMemoryWebSessionStore store = new InMemoryWebSessionStore ();
4242
4343
4444 @ Test
@@ -59,7 +59,7 @@ void startsSessionImplicitly() {
5959 }
6060
6161 @ Test // gh-24027, gh-26958
62- public void createSessionDoesNotBlock () {
62+ void createSessionDoesNotBlock () {
6363 this .store .createWebSession ()
6464 .doOnNext (session -> assertThat (Schedulers .isInNonBlockingThread ()).isTrue ())
6565 .block ();
@@ -103,7 +103,7 @@ void lastAccessTimeIsUpdatedOnRetrieve() {
103103 }
104104
105105 @ Test // SPR-17051
106- public void sessionInvalidatedBeforeSave () {
106+ void sessionInvalidatedBeforeSave () {
107107 // Request 1 creates session
108108 WebSession session1 = this .store .createWebSession ().block ();
109109 assertThat (session1 ).isNotNull ();
@@ -132,33 +132,31 @@ public void sessionInvalidatedBeforeSave() {
132132
133133 @ Test
134134 void expirationCheckPeriod () {
135-
136- DirectFieldAccessor accessor = new DirectFieldAccessor (this .store );
137- Map <?,?> sessions = (Map <?, ?>) accessor .getPropertyValue ("sessions" );
138- assertThat (sessions ).isNotNull ();
139-
140135 // Create 100 sessions
141- IntStream .range ( 0 , 100 ).forEach (i -> insertSession ());
142- assertThat ( sessions ). hasSize (100 );
136+ IntStream .rangeClosed ( 1 , 100 ).forEach (i -> insertSession ());
137+ assertNumSessions (100 );
143138
144- // Force a new clock (31 min later), don't use setter which would clean expired sessions
139+ // Force a new clock (31 min later). Don't use setter which would clean expired sessions.
140+ DirectFieldAccessor accessor = new DirectFieldAccessor (this .store );
145141 accessor .setPropertyValue ("clock" , Clock .offset (this .store .getClock (), Duration .ofMinutes (31 )));
146- assertThat ( sessions ). hasSize (100 );
142+ assertNumSessions (100 );
147143
148- // Create 1 more which forces a time-based check (clock moved forward)
144+ // Create 1 more which forces a time-based check (clock moved forward).
149145 insertSession ();
150- assertThat ( sessions ). hasSize (1 );
146+ assertNumSessions (1 );
151147 }
152148
153149 @ Test
154150 void maxSessions () {
151+ this .store .setMaxSessions (10 );
155152
156- IntStream .range ( 0 , 10000 ).forEach (i -> insertSession ());
157- assertThatIllegalStateException (). isThrownBy (
158- this ::insertSession )
159- .withMessage ("Max sessions limit reached: 10000 " );
153+ IntStream .rangeClosed ( 1 , 10 ).forEach (i -> insertSession ());
154+ assertThatIllegalStateException ()
155+ . isThrownBy ( this ::insertSession )
156+ .withMessage ("Max sessions limit reached: 10 " );
160157 }
161158
159+
162160 private WebSession insertSession () {
163161 WebSession session = this .store .createWebSession ().block ();
164162 assertThat (session ).isNotNull ();
@@ -167,4 +165,8 @@ private WebSession insertSession() {
167165 return session ;
168166 }
169167
168+ private void assertNumSessions (int numSessions ) {
169+ assertThat (store .getSessions ()).hasSize (numSessions );
170+ }
171+
170172}
0 commit comments