@Test public void testGetIntPropertyMultipleTimesWithShortExpireTime() throws Exception { String someKey = "someKey"; Integer someValue = 2; Integer someDefaultValue = -1; MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithShortExpireTime()); someProperties = mock(Properties.class); when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue)); when(configRepository.getConfig()).thenReturn(someProperties); DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(1)).getProperty(someKey); TimeUnit.MILLISECONDS.sleep(50); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue)); verify(someProperties, times(2)).getProperty(someKey); }
@Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; }
DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; } }
DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; } DefaultConfig(String namespace, ConfigRepository configRepository); }
DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; } DefaultConfig(String namespace, ConfigRepository configRepository); @Override String getProperty(String key, String defaultValue); @Override Set<String> getPropertyNames(); @Override ConfigSourceType getSourceType(); @Override synchronized void onRepositoryChange(String namespace, Properties newProperties); }
DefaultConfig extends AbstractConfig implements RepositoryChangeListener { @Override public String getProperty(String key, String defaultValue) { String value = System.getProperty(key); if (value == null && m_configProperties.get() != null) { value = m_configProperties.get().getProperty(key); } if (value == null) { value = System.getenv(key); } if (value == null && m_resourceProperties != null) { value = m_resourceProperties.getProperty(key); } if (value == null && m_configProperties.get() == null && m_warnLogRateLimiter.tryAcquire()) { logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace); } return value == null ? defaultValue : value; } DefaultConfig(String namespace, ConfigRepository configRepository); @Override String getProperty(String key, String defaultValue); @Override Set<String> getPropertyNames(); @Override ConfigSourceType getSourceType(); @Override synchronized void onRepositoryChange(String namespace, Properties newProperties);  }