mirror of https://github.com/longtai-cn/hippo4j
commit
f9c2fab83b
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.common.executor.support;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link RejectedPolicyTypeEnum}
|
||||||
|
*/
|
||||||
|
public class RejectedPolicyTypeEnumTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetType() {
|
||||||
|
Assertions.assertEquals(1, RejectedPolicyTypeEnum.CALLER_RUNS_POLICY.getType());
|
||||||
|
Assertions.assertEquals(2, RejectedPolicyTypeEnum.ABORT_POLICY.getType());
|
||||||
|
Assertions.assertEquals(3, RejectedPolicyTypeEnum.DISCARD_POLICY.getType());
|
||||||
|
Assertions.assertEquals(4, RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY.getType());
|
||||||
|
Assertions.assertEquals(5, RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY.getType());
|
||||||
|
Assertions.assertEquals(6, RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetName() {
|
||||||
|
Assertions.assertEquals("CallerRunsPolicy", RejectedPolicyTypeEnum.CALLER_RUNS_POLICY.getName());
|
||||||
|
Assertions.assertEquals("AbortPolicy", RejectedPolicyTypeEnum.ABORT_POLICY.getName());
|
||||||
|
Assertions.assertEquals("DiscardPolicy", RejectedPolicyTypeEnum.DISCARD_POLICY.getName());
|
||||||
|
Assertions.assertEquals("DiscardOldestPolicy", RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY.getName());
|
||||||
|
Assertions.assertEquals("RunsOldestTaskPolicy", RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY.getName());
|
||||||
|
Assertions.assertEquals("SyncPutQueuePolicy", RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValues() {
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValueOf() {
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.CALLER_RUNS_POLICY, RejectedPolicyTypeEnum.valueOf("CALLER_RUNS_POLICY"));
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.ABORT_POLICY, RejectedPolicyTypeEnum.valueOf("ABORT_POLICY"));
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.DISCARD_POLICY, RejectedPolicyTypeEnum.valueOf("DISCARD_POLICY"));
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY, RejectedPolicyTypeEnum.valueOf("DISCARD_OLDEST_POLICY"));
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY, RejectedPolicyTypeEnum.valueOf("RUNS_OLDEST_TASK_POLICY"));
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY, RejectedPolicyTypeEnum.valueOf("SYNC_PUT_QUEUE_POLICY"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreatePolicy() {
|
||||||
|
// check legal param: name and type
|
||||||
|
Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> {
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(each.getName()));
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(each.getType()));
|
||||||
|
});
|
||||||
|
// check illegal null name
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(null));
|
||||||
|
// check nonexistent name
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy("ABC"));
|
||||||
|
// check nonexistent type
|
||||||
|
Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRejectedNameByType() {
|
||||||
|
// check legal range of type
|
||||||
|
Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> Assertions.assertEquals(each.getName(), RejectedPolicyTypeEnum.getRejectedNameByType(each.getType())));
|
||||||
|
// check illegal range of type
|
||||||
|
Assertions.assertEquals("AbortPolicy", RejectedPolicyTypeEnum.getRejectedNameByType(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRejectedPolicyTypeEnumByName() {
|
||||||
|
// check legal range of name
|
||||||
|
Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> Assertions.assertEquals(each, RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(each.getName())));
|
||||||
|
// check illegal name
|
||||||
|
Assertions.assertEquals(RejectedPolicyTypeEnum.ABORT_POLICY,
|
||||||
|
RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName("XXX"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.common.executor.support;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class RunsOldestTaskPolicyTest {
|
||||||
|
|
||||||
|
private final RunsOldestTaskPolicy runsOldestTaskPolicy = new RunsOldestTaskPolicy();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Runnable runnable;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Runnable runnableInTheQueue;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ThreadPoolExecutor threadPoolExecutor;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private BlockingQueue<Runnable> workQueue;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRejectedExecutionWhenExecutorIsShutDown() {
|
||||||
|
when(threadPoolExecutor.isShutdown()).thenReturn(true);
|
||||||
|
|
||||||
|
runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor);
|
||||||
|
|
||||||
|
verify(threadPoolExecutor, never()).execute(runnable);
|
||||||
|
verify(runnable, never()).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldNotExecute() {
|
||||||
|
when(threadPoolExecutor.isShutdown()).thenReturn(false);
|
||||||
|
when(threadPoolExecutor.getQueue()).thenReturn(workQueue);
|
||||||
|
when(workQueue.poll()).thenReturn(runnableInTheQueue);
|
||||||
|
when(workQueue.offer(runnable)).thenReturn(true);
|
||||||
|
|
||||||
|
runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor);
|
||||||
|
|
||||||
|
verify(runnableInTheQueue).run();
|
||||||
|
verify(threadPoolExecutor, never()).execute(runnable);
|
||||||
|
verify(runnable, never()).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldExecute() {
|
||||||
|
when(threadPoolExecutor.isShutdown()).thenReturn(false);
|
||||||
|
when(threadPoolExecutor.getQueue()).thenReturn(workQueue);
|
||||||
|
when(workQueue.poll()).thenReturn(runnableInTheQueue);
|
||||||
|
when(workQueue.offer(runnable)).thenReturn(false);
|
||||||
|
|
||||||
|
runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor);
|
||||||
|
|
||||||
|
verify(runnableInTheQueue).run();
|
||||||
|
verify(threadPoolExecutor).execute(runnable);
|
||||||
|
verify(runnable, never()).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRejectedExecutionWhenATaskIsInTheQueueAndThePollReturnANullValue() {
|
||||||
|
when(threadPoolExecutor.isShutdown()).thenReturn(false);
|
||||||
|
when(threadPoolExecutor.getQueue()).thenReturn(workQueue);
|
||||||
|
when(workQueue.poll()).thenReturn(null);
|
||||||
|
when(workQueue.offer(runnable)).thenReturn(false);
|
||||||
|
|
||||||
|
runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor);
|
||||||
|
|
||||||
|
verify(runnableInTheQueue, never()).run();
|
||||||
|
verify(threadPoolExecutor).execute(runnable);
|
||||||
|
verify(runnable, never()).run();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.common.executor.support;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.toolkit.ThreadUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronous placement queue policy implementation test
|
||||||
|
*/
|
||||||
|
public class SyncPutQueuePolicyTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test thread pool rejected execution
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testRejectedExecution() {
|
||||||
|
SyncPutQueuePolicy syncPutQueuePolicy = new SyncPutQueuePolicy();
|
||||||
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2,
|
||||||
|
60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), syncPutQueuePolicy);
|
||||||
|
threadPoolExecutor.prestartAllCoreThreads();
|
||||||
|
|
||||||
|
Assert.assertSame(syncPutQueuePolicy, threadPoolExecutor.getRejectedExecutionHandler());
|
||||||
|
IntStream.range(0, 4).forEach(s -> {
|
||||||
|
threadPoolExecutor.execute(() -> ThreadUtil.sleep(200L));
|
||||||
|
});
|
||||||
|
threadPoolExecutor.shutdown();
|
||||||
|
while (!threadPoolExecutor.isTerminated()) {
|
||||||
|
}
|
||||||
|
Assert.assertEquals(4, threadPoolExecutor.getCompletedTaskCount());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.common.extension.design;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class AbstractSubjectCenterTest {
|
||||||
|
|
||||||
|
private Map<String, List<Observer>> OBSERVERS_MAP;
|
||||||
|
|
||||||
|
private SubjectNotifyListener subjectNotifyListener;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||||
|
subjectNotifyListener = new SubjectNotifyListener();
|
||||||
|
|
||||||
|
Field field = AbstractSubjectCenter.class.getDeclaredField("OBSERVERS_MAP");
|
||||||
|
field.setAccessible(true);
|
||||||
|
OBSERVERS_MAP = (Map<String, List<Observer>>) field.get(AbstractSubjectCenter.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test register listener
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDefaultRegister() {
|
||||||
|
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
Assert.assertEquals(1, list.size());
|
||||||
|
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||||
|
OBSERVERS_MAP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test register listener
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSubjectTypeEnumRegister() {
|
||||||
|
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
Assert.assertEquals(1, list.size());
|
||||||
|
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||||
|
OBSERVERS_MAP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test register listener
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSubjectTypeNameRegister() {
|
||||||
|
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
Assert.assertEquals(1, list.size());
|
||||||
|
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||||
|
OBSERVERS_MAP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test remove listener
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDefaultRemoveListener() {
|
||||||
|
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
Assert.assertEquals(1, list.size());
|
||||||
|
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||||
|
|
||||||
|
AbstractSubjectCenter.remove(subjectNotifyListener);
|
||||||
|
Assert.assertEquals(0, list.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test remove listener
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testRemoveSubjectTypeNameListener() {
|
||||||
|
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
Assert.assertEquals(1, list.size());
|
||||||
|
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||||
|
|
||||||
|
AbstractSubjectCenter.remove(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener);
|
||||||
|
Assert.assertEquals(0, list.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test notify
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testNotifyBySubjectType() {
|
||||||
|
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
|
||||||
|
NotifyMessage notifyMessage = new NotifyMessage();
|
||||||
|
Assert.assertEquals(0, notifyMessage.getCount().get());
|
||||||
|
AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED, () -> notifyMessage);
|
||||||
|
Assert.assertEquals(1, notifyMessage.getCount().get());
|
||||||
|
OBSERVERS_MAP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test notify
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testNotifyBySubjectTypeName() {
|
||||||
|
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||||
|
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||||
|
Assert.assertNotNull(list);
|
||||||
|
|
||||||
|
NotifyMessage notifyMessage = new NotifyMessage();
|
||||||
|
Assert.assertEquals(0, notifyMessage.getCount().get());
|
||||||
|
AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name(), () -> notifyMessage);
|
||||||
|
Assert.assertEquals(1, notifyMessage.getCount().get());
|
||||||
|
OBSERVERS_MAP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private static final class NotifyMessage {
|
||||||
|
|
||||||
|
private final AtomicInteger count = new AtomicInteger(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subject Response Listener
|
||||||
|
*/
|
||||||
|
private static final class SubjectNotifyListener implements Observer<NotifyMessage> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(ObserverMessage<NotifyMessage> observerMessage) {
|
||||||
|
observerMessage.message().getCount().incrementAndGet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.common.toolkit.logtracing;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.util.Strings;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class LogMessageTest {
|
||||||
|
|
||||||
|
private final static String MESSAGE = "message";
|
||||||
|
private final static String THROWABLE_MESSAGE = "throwable message";
|
||||||
|
private LogMessage logMessage;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
logMessage = LogMessage.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetInstanceShouldReturnANewLogMessageInstance() {
|
||||||
|
final LogMessage newInstance = LogMessage.getInstance();
|
||||||
|
assertNotNull(newInstance);
|
||||||
|
assertNotSame(logMessage, newInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToStringShouldHaveAnEmptyMessage() {
|
||||||
|
assertEquals(Strings.EMPTY, logMessage.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetMsgShouldSetAnewMessageInLogMessage() {
|
||||||
|
logMessage.setMsg(MESSAGE);
|
||||||
|
assertEquals(MESSAGE, logMessage.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMsgShouldContainsMessageAndThrowableMessage() {
|
||||||
|
final String message = logMessage.msg(MESSAGE, new Throwable(THROWABLE_MESSAGE));
|
||||||
|
assertNotNull(message);
|
||||||
|
assertTrue(message.contains(MESSAGE));
|
||||||
|
assertTrue(message.contains(THROWABLE_MESSAGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKvShouldPutKeyAndValue() {
|
||||||
|
logMessage.kv("key", "value");
|
||||||
|
assertEquals("key=value", logMessage.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKvShouldPutAllKeyAndValuePairs() {
|
||||||
|
logMessage.kv("key1", "value1");
|
||||||
|
logMessage.kv("key2", "value2");
|
||||||
|
assertEquals("key1=value1||key2=value2", logMessage.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToStringShouldPrintMessageAndAllKeyAndValuePairs() {
|
||||||
|
logMessage.setMsg(MESSAGE);
|
||||||
|
logMessage.kv("key1", "value1");
|
||||||
|
logMessage.kv("key2", "value2");
|
||||||
|
assertEquals("messagekey1=value1||key2=value2", logMessage.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKv2StringShouldPrintMessageAndAllKeyAndValuePairs() {
|
||||||
|
String result = logMessage.kv2String("key", "value");
|
||||||
|
assertEquals("key=value", result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue