Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.dromara.dynamictp.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -31,6 +34,7 @@
* @author xs.Tao
* @since 1.1.4
*/
@Slf4j
public class ExtensionServiceLoader {

private static final Map<Class<?>, List<?>> EXTENSION_MAP = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -68,9 +72,22 @@ public static <T> T getFirst(Class<T> clazz) {

private static <T> List<T> load(Class<T> clazz) {
ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz);
Iterator<T> iterator = serviceLoader.iterator();
List<T> services = new ArrayList<>();
for (T service : serviceLoader) {
services.add(service);
while (true) {
try {
if (!iterator.hasNext()) {
break;
}
} catch (ServiceConfigurationError e) {
log.warn("Failed to load {} provider, skip remaining providers.", clazz.getName(), e);
break;
}
try {
services.add(iterator.next());
} catch (ServiceConfigurationError e) {
log.warn("Failed to load {} provider, skip it.", clazz.getName(), e);
}
}
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class AlarmCounter {

private static final Map<String, Cache<String, AlarmInfo>> ALARM_INFO_CACHE = new ConcurrentHashMap<>();

private static final Map<String, Integer> ALARM_PERIOD_CACHE = new ConcurrentHashMap<>();

private static final Map<String, String> LAST_ALARM_TIME_MAP = new ConcurrentHashMap<>();

private AlarmCounter() { }
Expand All @@ -51,10 +53,16 @@ public static void initAlarmCounter(String threadPoolName, NotifyItem notifyItem
}

String key = buildKey(threadPoolName, notifyItem.getType());
if (Objects.nonNull(ALARM_INFO_CACHE.get(key))
&& Objects.equals(ALARM_PERIOD_CACHE.get(key), notifyItem.getPeriod())) {
return;
}

Cache<String, AlarmInfo> cache = CacheBuilder.newBuilder()
.expireAfterWrite(notifyItem.getPeriod(), TimeUnit.SECONDS)
.build();
ALARM_INFO_CACHE.put(key, cache);
ALARM_PERIOD_CACHE.put(key, notifyItem.getPeriod());
}

public static AlarmInfo getAlarmInfo(String threadPoolName, String notifyType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,76 @@
package org.dromara.dynamictp.test.common.manager;

import org.dromara.dynamictp.common.manager.ContextManagerHelper;
import org.junit.jupiter.api.Assertions;
import org.dromara.dynamictp.spring.holder.SpringContextHolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.MapPropertySource;

import java.lang.reflect.Field;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* ContextManagerHelperTest related.
*/
@Execution(ExecutionMode.SAME_THREAD)
class ContextManagerHelperTest {

private static final String TEST_PROPERTY_KEY = "context.manager.test.key";

private GenericApplicationContext context;

private ApplicationContext originalContext;

@BeforeEach
void setUp() throws Exception {
originalContext = springContext();
context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("sampleBean", new SampleBean());
context.getEnvironment().getPropertySources().addFirst(
new MapPropertySource("contextManagerTest", Collections.singletonMap(TEST_PROPERTY_KEY, "test-value")));
context.refresh();
new SpringContextHolder().setApplicationContext(context);
}

@AfterEach
void tearDown() throws Exception {
context.close();
setSpringContext(originalContext);
}

@Test
void testFallbackToNullContextManagerWhenNoImplementationFound() {
Assertions.assertNull(ContextManagerHelper.getBean(Object.class));
Assertions.assertNull(ContextManagerHelper.getBean("bean", Object.class));
Assertions.assertTrue(ContextManagerHelper.getBeansOfType(Object.class).isEmpty());
Assertions.assertNull(ContextManagerHelper.getEnvironment());
Assertions.assertNull(ContextManagerHelper.getEnvironmentProperty("key"));
Assertions.assertNull(ContextManagerHelper.getEnvironmentProperty("key", new Object()));
Assertions.assertNull(ContextManagerHelper.getEnvironmentProperty("key", "default"));
void testDelegateToSpringContextManagerWhenImplementationFound() {
SampleBean sampleBean = ContextManagerHelper.getBean(SampleBean.class);

assertSame(sampleBean, ContextManagerHelper.getBean("sampleBean", SampleBean.class));
assertTrue(ContextManagerHelper.getBeansOfType(SampleBean.class).containsValue(sampleBean));
assertSame(context.getEnvironment(), ContextManagerHelper.getEnvironment());
assertEquals("test-value", ContextManagerHelper.getEnvironmentProperty(TEST_PROPERTY_KEY));
assertEquals("test-value", ContextManagerHelper.getEnvironmentProperty(TEST_PROPERTY_KEY, context.getEnvironment()));
assertEquals("default", ContextManagerHelper.getEnvironmentProperty("missing.key", "default"));
}

private ApplicationContext springContext() throws Exception {
Field field = SpringContextHolder.class.getDeclaredField("context");
field.setAccessible(true);
return (ApplicationContext) field.get(null);
}

private void setSpringContext(ApplicationContext applicationContext) throws Exception {
Field field = SpringContextHolder.class.getDeclaredField("context");
field.setAccessible(true);
field.set(null, applicationContext);
}

private static class SampleBean {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.dromara.dynamictp.test.common.util;

/**
* Broken SPI loading test service.
*/
public interface BrokenTestService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.dromara.dynamictp.test.common.util;

/**
* Broken SPI implementation used to verify bad providers are skipped.
*/
public class BrokenTestServiceBrokenImpl implements BrokenTestService {

public BrokenTestServiceBrokenImpl() {
throw new IllegalStateException("broken spi provider");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.dromara.dynamictp.test.common.util;

/**
* Valid SPI implementation used by ExtensionServiceLoader tests.
*/
public class BrokenTestServiceImpl implements BrokenTestService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.dromara.dynamictp.test.common.util.BrokenTestServiceBrokenImpl
org.dromara.dynamictp.test.common.util.BrokenTestServiceImpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.dromara.dynamictp.test.core.notify.invoker;

import org.dromara.dynamictp.common.em.NotifyItemEnum;
import org.dromara.dynamictp.common.entity.AlarmInfo;
import org.dromara.dynamictp.common.entity.NotifyItem;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.entity.TpMainFields;
Expand Down Expand Up @@ -104,11 +105,14 @@ void testInvokeSendsAlarmResetsCounterAndClearsContext() {
AlarmCounter.initAlarmCounter(poolName, notifyItem);
AlarmCounter.incAlarmCount(poolName, notifyItem.getType());
AlarmCounter.incAlarmCount(poolName, notifyItem.getType());
AlarmInfo alarmInfo = AlarmCounter.getAlarmInfo(poolName, notifyItem.getType());
assertEquals(2, alarmInfo.getCount());

new AlarmInvoker().invoke(new AlarmCtx(new ExecutorWrapper(poolName, executor), notifyItem));

assertEquals(NotifyItemEnum.REJECT, notifier.notifyItemEnum);
assertEquals(0, AlarmCounter.getAlarmInfo(poolName, notifyItem.getType()).getCount());
assertSame(alarmInfo, AlarmCounter.getAlarmInfo(poolName, notifyItem.getType()));
assertEquals(0, alarmInfo.getCount());
assertEquals(platform().getPlatformId(), notifier.platform.getPlatformId());
assertNull(DtpNotifyCtxHolder.get());
}
Expand Down
Loading
Loading