Add support for configuring Redis session cleanup cron
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.session.data.redis.config.annotation.web.http;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
public class RedisHttpSessionConfigurationCustomCronTests {
|
||||
|
||||
AnnotationConfigApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeContext() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideCron() {
|
||||
this.context.register(Config.class);
|
||||
|
||||
assertThatThrownBy(() ->
|
||||
RedisHttpSessionConfigurationCustomCronTests.this.context.refresh())
|
||||
.hasStackTraceContaining(
|
||||
"Encountered invalid @Scheduled method 'cleanupExpiredSessions': Cron expression must consist of 6 fields (found 1 in \"oops\")");
|
||||
}
|
||||
|
||||
@EnableRedisHttpSession
|
||||
@Configuration
|
||||
@PropertySource("classpath:spring-session-cleanup-cron-expression-oops.properties")
|
||||
static class Config {
|
||||
@Bean
|
||||
public RedisConnectionFactory connectionFactory() {
|
||||
RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
|
||||
RedisConnection connection = mock(RedisConnection.class);
|
||||
given(factory.getConnection()).willReturn(connection);
|
||||
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,8 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
private static final String CLEANUP_CRON_EXPRESSION = "0 0 * * * *";
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -90,6 +92,30 @@ public class RedisHttpSessionConfigurationTests {
|
||||
.isEqualTo("customRedisNamespace");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customCleanupCronAnnotation() {
|
||||
registerAndRefresh(RedisConfig.class,
|
||||
CustomCleanupCronExpressionAnnotationConfiguration.class);
|
||||
|
||||
RedisHttpSessionConfiguration configuration = this.context
|
||||
.getBean(RedisHttpSessionConfiguration.class);
|
||||
assertThat(configuration).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(configuration, "cleanupCron"))
|
||||
.isEqualTo(CLEANUP_CRON_EXPRESSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customCleanupCronSetter() {
|
||||
registerAndRefresh(RedisConfig.class,
|
||||
CustomCleanupCronExpressionSetterConfiguration.class);
|
||||
|
||||
RedisHttpSessionConfiguration configuration = this.context
|
||||
.getBean(RedisHttpSessionConfiguration.class);
|
||||
assertThat(configuration).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(configuration, "cleanupCron"))
|
||||
.isEqualTo(CLEANUP_CRON_EXPRESSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void qualifiedConnectionFactoryRedisConfig() {
|
||||
registerAndRefresh(RedisConfig.class,
|
||||
@@ -126,7 +152,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void qualifiedAndPrimaryDataSourceConfiguration() {
|
||||
public void qualifiedAndPrimaryConnectionFactoryRedisConfig() {
|
||||
registerAndRefresh(RedisConfig.class,
|
||||
QualifiedAndPrimaryConnectionFactoryRedisConfig.class);
|
||||
|
||||
@@ -144,7 +170,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedDataSourceConfiguration() {
|
||||
public void namedConnectionFactoryRedisConfig() {
|
||||
registerAndRefresh(RedisConfig.class, NamedConnectionFactoryRedisConfig.class);
|
||||
|
||||
RedisOperationsSessionRepository repository = this.context
|
||||
@@ -161,12 +187,11 @@ public class RedisHttpSessionConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleDataSourceConfiguration() {
|
||||
public void multipleConnectionFactoryRedisConfig() {
|
||||
this.thrown.expect(BeanCreationException.class);
|
||||
this.thrown.expectMessage(
|
||||
"secondaryRedisConnectionFactory,defaultRedisConnectionFactory");
|
||||
this.thrown.expectMessage("expected single matching bean but found 2");
|
||||
|
||||
registerAndRefresh(MultipleConnectionFactoryRedisConfig.class);
|
||||
registerAndRefresh(RedisConfig.class, MultipleConnectionFactoryRedisConfig.class);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
@@ -202,9 +227,24 @@ public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@EnableRedisHttpSession(cleanupCron = CLEANUP_CRON_EXPRESSION)
|
||||
static class CustomCleanupCronExpressionAnnotationConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomCleanupCronExpressionSetterConfiguration
|
||||
extends RedisHttpSessionConfiguration {
|
||||
|
||||
CustomCleanupCronExpressionSetterConfiguration() {
|
||||
setCleanupCron(CLEANUP_CRON_EXPRESSION);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class QualifiedConnectionFactoryRedisConfig extends RedisConfig {
|
||||
static class QualifiedConnectionFactoryRedisConfig {
|
||||
|
||||
@Bean
|
||||
@SpringSessionRedisConnectionFactory
|
||||
@@ -216,7 +256,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class PrimaryConnectionFactoryRedisConfig extends RedisConfig {
|
||||
static class PrimaryConnectionFactoryRedisConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@@ -228,7 +268,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class QualifiedAndPrimaryConnectionFactoryRedisConfig extends RedisConfig {
|
||||
static class QualifiedAndPrimaryConnectionFactoryRedisConfig {
|
||||
|
||||
@Bean
|
||||
@SpringSessionRedisConnectionFactory
|
||||
@@ -246,7 +286,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class NamedConnectionFactoryRedisConfig extends RedisConfig {
|
||||
static class NamedConnectionFactoryRedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisConnectionFactory redisConnectionFactory() {
|
||||
@@ -257,7 +297,7 @@ public class RedisHttpSessionConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
static class MultipleConnectionFactoryRedisConfig extends RedisConfig {
|
||||
static class MultipleConnectionFactoryRedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisConnectionFactory secondaryRedisConnectionFactory() {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
spring.session.cleanup.cron.expression=oops
|
||||
Reference in New Issue
Block a user