提交 ed3dd78f 编写于 作者: J Jason Song

add constraint on bizconfig

上级 7f0fc9d4
......@@ -44,15 +44,18 @@ public class BizConfig extends RefreshableConfig {
}
public int grayReleaseRuleScanInterval() {
return getIntProperty("apollo.gray-release-rule-scan.interval", 60);
int interval = getIntProperty("apollo.gray-release-rule-scan.interval", 60);
return checkInt(interval, 1, Integer.MAX_VALUE, 60);
}
public int itemKeyLengthLimit() {
return getIntProperty("item.key.length.limit", 128);
int limit = getIntProperty("item.key.length.limit", 128);
return checkInt(limit, 5, Integer.MAX_VALUE, 128);
}
public int itemValueLengthLimit() {
return getIntProperty("item.value.length.limit", 20000);
int limit = getIntProperty("item.value.length.limit", 20000);
return checkInt(limit, 5, Integer.MAX_VALUE, 20000);
}
public Map<Long, Integer> namespaceValueLengthLimitOverride() {
......@@ -82,7 +85,8 @@ public class BizConfig extends RefreshableConfig {
}
public int appNamespaceCacheScanInterval() {
return getIntProperty("apollo.app-namespace-cache-scan.interval", 1);
int interval = getIntProperty("apollo.app-namespace-cache-scan.interval", 1);
return checkInt(interval, 1, Integer.MAX_VALUE, 1);
}
public TimeUnit appNamespaceCacheScanIntervalTimeUnit() {
......@@ -90,7 +94,8 @@ public class BizConfig extends RefreshableConfig {
}
public int appNamespaceCacheRebuildInterval() {
return getIntProperty("apollo.app-namespace-cache-rebuild.interval", 60);
int interval = getIntProperty("apollo.app-namespace-cache-rebuild.interval", 60);
return checkInt(interval, 1, Integer.MAX_VALUE, 60);
}
public TimeUnit appNamespaceCacheRebuildIntervalTimeUnit() {
......@@ -98,7 +103,8 @@ public class BizConfig extends RefreshableConfig {
}
public int releaseMessageCacheScanInterval() {
return getIntProperty("apollo.release-message-cache-scan.interval", 1);
int interval = getIntProperty("apollo.release-message-cache-scan.interval", 1);
return checkInt(interval, 1, Integer.MAX_VALUE, 1);
}
public TimeUnit releaseMessageCacheScanIntervalTimeUnit() {
......@@ -106,10 +112,19 @@ public class BizConfig extends RefreshableConfig {
}
public int releaseMessageNotificationBatch() {
return getIntProperty("apollo.release-message.notification.batch", 100);
int batch = getIntProperty("apollo.release-message.notification.batch", 100);
return checkInt(batch, 1, Integer.MAX_VALUE, 100);
}
public int releaseMessageNotificationBatchIntervalInMilli() {
return getIntProperty("apollo.release-message.notification.batch.interval", 100);
int interval = getIntProperty("apollo.release-message.notification.batch.interval", 100);
return checkInt(interval, 1, Integer.MAX_VALUE, 100);
}
int checkInt(int value, int min, int max, int defaultValue) {
if (value >= min && value <= max) {
return value;
}
return defaultValue;
}
}
package com.ctrip.framework.apollo.biz;
import com.ctrip.framework.apollo.biz.config.BizConfigTest;
import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolderTest;
import com.ctrip.framework.apollo.biz.message.DatabaseMessageSenderTest;
import com.ctrip.framework.apollo.biz.message.ReleaseMessageScannerTest;
......@@ -38,7 +39,8 @@ import org.junit.runners.Suite.SuiteClasses;
NamespaceBranchServiceTest.class,
ReleaseCreationTest.class,
NamespacePublishInfoTest.class,
NamespaceServiceTest.class
NamespaceServiceTest.class,
BizConfigTest.class
})
public class AllTests {
......
package com.ctrip.framework.apollo.biz.config;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith(MockitoJUnitRunner.class)
public class BizConfigTest {
@Mock
private ConfigurableEnvironment environment;
private BizConfig bizConfig;
@Before
public void setUp() throws Exception {
bizConfig = new BizConfig();
ReflectionTestUtils.setField(bizConfig, "environment", environment);
}
@Test
public void testReleaseMessageNotificationBatch() throws Exception {
int someBatch = 20;
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(String.valueOf(someBatch));
assertEquals(someBatch, bizConfig.releaseMessageNotificationBatch());
}
@Test
public void testReleaseMessageNotificationBatchWithDefaultValue() throws Exception {
int defaultBatch = 100;
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
}
@Test
public void testReleaseMessageNotificationBatchWithInvalidNumber() throws Exception {
int someBatch = -20;
int defaultBatch = 100;
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(String.valueOf(someBatch));
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
}
@Test
public void testReleaseMessageNotificationBatchWithNAN() throws Exception {
String someNAN = "someNAN";
int defaultBatch = 100;
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(someNAN);
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
}
@Test
public void testCheckInt() throws Exception {
int someInvalidValue = 1;
int anotherInvalidValue = 2;
int someValidValue = 3;
int someDefaultValue = 10;
int someMin = someInvalidValue + 1;
int someMax = anotherInvalidValue - 1;
assertEquals(someDefaultValue, bizConfig.checkInt(someInvalidValue, someMin, Integer.MAX_VALUE, someDefaultValue));
assertEquals(someDefaultValue, bizConfig.checkInt(anotherInvalidValue, Integer.MIN_VALUE, someMax,
someDefaultValue));
assertEquals(someValidValue, bizConfig.checkInt(someValidValue, Integer.MIN_VALUE, Integer.MAX_VALUE,
someDefaultValue));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册