25
25
26
26
import org .springframework .boot .autoconfigure .AutoConfigurations ;
27
27
import org .springframework .boot .autoconfigure .data .redis .RedisAutoConfiguration ;
28
- import org .springframework .boot .autoconfigure .session .RedisSessionConfiguration .SpringBootRedisHttpSessionConfiguration ;
28
+ import org .springframework .boot .autoconfigure .session .RedisSessionConfiguration .IndexedRedisSessionConfiguration . SpringBootRedisIndexedHttpSessionConfiguration ;
29
29
import org .springframework .boot .autoconfigure .web .ServerProperties ;
30
30
import org .springframework .boot .test .context .FilteredClassLoader ;
31
31
import org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
38
38
import org .springframework .session .SaveMode ;
39
39
import org .springframework .session .data .mongo .MongoIndexedSessionRepository ;
40
40
import org .springframework .session .data .redis .RedisIndexedSessionRepository ;
41
+ import org .springframework .session .data .redis .RedisSessionRepository ;
41
42
import org .springframework .session .data .redis .config .ConfigureNotifyKeyspaceEventsAction ;
42
43
import org .springframework .session .data .redis .config .ConfigureRedisAction ;
43
44
import org .springframework .session .hazelcast .HazelcastIndexedSessionRepository ;
@@ -70,17 +71,17 @@ void defaultConfig() {
70
71
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
71
72
"spring.data.redis.port=" + redis .getFirstMappedPort ())
72
73
.withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
73
- .run (validateSpringSessionUsesRedis ("spring:session:event:0:created :" , FlushMode .ON_SAVE ,
74
- SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
74
+ .run (validateSpringSessionUsesDefaultRedis ("spring:session:" , FlushMode .ON_SAVE ,
75
+ SaveMode .ON_SET_ATTRIBUTE ));
75
76
}
76
77
77
78
@ Test
78
79
void redisTakesPrecedenceMultipleImplementations () {
79
80
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
80
81
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
81
82
"spring.data.redis.port=" + redis .getFirstMappedPort ())
82
- .run (validateSpringSessionUsesRedis ("spring:session:event:0:created :" , FlushMode .ON_SAVE ,
83
- SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
83
+ .run (validateSpringSessionUsesDefaultRedis ("spring:session:" , FlushMode .ON_SAVE ,
84
+ SaveMode .ON_SET_ATTRIBUTE ));
84
85
}
85
86
86
87
@ Test
@@ -89,64 +90,97 @@ void defaultConfigWithCustomTimeout() {
89
90
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
90
91
"spring.data.redis.port=" + redis .getFirstMappedPort (), "spring.session.timeout=1m" )
91
92
.withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class )).run ((context ) -> {
92
- RedisIndexedSessionRepository repository = validateSessionRepository (context ,
93
- RedisIndexedSessionRepository .class );
94
- assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" , 60 );
93
+ RedisSessionRepository repository = validateSessionRepository (context ,
94
+ RedisSessionRepository .class );
95
+ assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
96
+ Duration .ofMinutes (1 ));
95
97
});
96
98
}
97
99
98
100
@ Test
99
- void redisSessionStoreWithCustomizations () {
101
+ void defaultRedisSessionStoreWithCustomizations () {
100
102
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
101
103
.withPropertyValues ("spring.session.redis.namespace=foo" , "spring.session.redis.flush-mode=immediate" ,
102
- "spring.session.redis.save-mode=on-get-attribute" ,
104
+ "spring.session.redis.save-mode=on-get-attribute" , "spring.data.redis.host=" + redis .getHost (),
105
+ "spring.data.redis.port=" + redis .getFirstMappedPort ())
106
+ .run (validateSpringSessionUsesDefaultRedis ("foo:" , FlushMode .IMMEDIATE , SaveMode .ON_GET_ATTRIBUTE ));
107
+ }
108
+
109
+ @ Test
110
+ void indexedRedisSessionDefaultConfig () {
111
+ this .contextRunner .withPropertyValues ("spring.session.redis.repository=indexed" ,
112
+ "spring.data.redis.host=" + redis .getHost (), "spring.data.redis.port=" + redis .getFirstMappedPort ())
113
+ .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
114
+ .run (validateSpringSessionUsesIndexedRedis ("spring:session:" , FlushMode .ON_SAVE ,
115
+ SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
116
+ }
117
+
118
+ @ Test
119
+ void indexedRedisSessionStoreWithCustomizations () {
120
+ this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
121
+ .withPropertyValues ("spring.session.redis.repository=indexed" , "spring.session.redis.namespace=foo" ,
122
+ "spring.session.redis.flush-mode=immediate" , "spring.session.redis.save-mode=on-get-attribute" ,
103
123
"spring.session.redis.cleanup-cron=0 0 12 * * *" , "spring.data.redis.host=" + redis .getHost (),
104
124
"spring.data.redis.port=" + redis .getFirstMappedPort ())
105
- .run (validateSpringSessionUsesRedis ("foo:event:0:created: " , FlushMode .IMMEDIATE ,
106
- SaveMode . ON_GET_ATTRIBUTE , "0 0 12 * * *" ));
125
+ .run (validateSpringSessionUsesIndexedRedis ("foo:" , FlushMode .IMMEDIATE , SaveMode . ON_GET_ATTRIBUTE ,
126
+ "0 0 12 * * *" ));
107
127
}
108
128
109
129
@ Test
110
- void redisSessionWithConfigureActionNone () {
130
+ void indexedRedisSessionWithConfigureActionNone () {
111
131
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
112
- .withPropertyValues ("spring.session.redis.configure-action=none " ,
113
- "spring.data.redis.host=" + redis .getHost (),
132
+ .withPropertyValues ("spring.session.redis.repository=indexed " ,
133
+ "spring.session.redis.configure-action=none" , "spring. data.redis.host=" + redis .getHost (),
114
134
"spring.data.redis.port=" + redis .getFirstMappedPort ())
115
135
.run (validateStrategy (ConfigureRedisAction .NO_OP .getClass ()));
116
136
}
117
137
118
138
@ Test
119
- void redisSessionWithDefaultConfigureActionNone () {
139
+ void indexedRedisSessionWithDefaultConfigureActionNone () {
120
140
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
121
- .withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
141
+ .withPropertyValues ("spring.session.redis.repository=indexed" ,
142
+ "spring.data.redis.host=" + redis .getHost (),
122
143
"spring.data.redis.port=" + redis .getFirstMappedPort ())
123
144
.run (validateStrategy (ConfigureNotifyKeyspaceEventsAction .class ,
124
145
entry ("notify-keyspace-events" , "gxE" )));
125
146
}
126
147
127
148
@ Test
128
- void redisSessionWithCustomConfigureRedisActionBean () {
149
+ void indexedRedisSessionWithCustomConfigureRedisActionBean () {
129
150
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
130
151
.withUserConfiguration (MaxEntriesRedisAction .class )
131
- .withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
152
+ .withPropertyValues ("spring.session.redis.repository=indexed" ,
153
+ "spring.data.redis.host=" + redis .getHost (),
132
154
"spring.data.redis.port=" + redis .getFirstMappedPort ())
133
155
.run (validateStrategy (MaxEntriesRedisAction .class , entry ("set-max-intset-entries" , "1024" )));
134
156
135
157
}
136
158
137
- private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesRedis (
138
- String sessionCreatedChannelPrefix , FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
159
+ private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesDefaultRedis (String keyNamespace ,
160
+ FlushMode flushMode , SaveMode saveMode ) {
161
+ return (context ) -> {
162
+ RedisSessionRepository repository = validateSessionRepository (context , RedisSessionRepository .class );
163
+ assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
164
+ new ServerProperties ().getServlet ().getSession ().getTimeout ());
165
+ assertThat (repository ).hasFieldOrPropertyWithValue ("keyNamespace" , keyNamespace );
166
+ assertThat (repository ).hasFieldOrPropertyWithValue ("flushMode" , flushMode );
167
+ assertThat (repository ).hasFieldOrPropertyWithValue ("saveMode" , saveMode );
168
+ };
169
+ }
170
+
171
+ private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesIndexedRedis (String keyNamespace ,
172
+ FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
139
173
return (context ) -> {
140
174
RedisIndexedSessionRepository repository = validateSessionRepository (context ,
141
175
RedisIndexedSessionRepository .class );
142
176
assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
143
177
(int ) new ServerProperties ().getServlet ().getSession ().getTimeout ().getSeconds ());
144
- assertThat (repository . getSessionCreatedChannelPrefix ()). isEqualTo ( sessionCreatedChannelPrefix );
178
+ assertThat (repository ). hasFieldOrPropertyWithValue ( "namespace" , keyNamespace );
145
179
assertThat (repository ).hasFieldOrPropertyWithValue ("flushMode" , flushMode );
146
- SpringBootRedisHttpSessionConfiguration configuration = context
147
- .getBean (SpringBootRedisHttpSessionConfiguration .class );
148
- assertThat (configuration ).hasFieldOrPropertyWithValue ("cleanupCron" , cleanupCron );
149
180
assertThat (repository ).hasFieldOrPropertyWithValue ("saveMode" , saveMode );
181
+ SpringBootRedisIndexedHttpSessionConfiguration configuration = context
182
+ .getBean (SpringBootRedisIndexedHttpSessionConfiguration .class );
183
+ assertThat (configuration ).hasFieldOrPropertyWithValue ("cleanupCron" , cleanupCron );
150
184
};
151
185
}
152
186
0 commit comments