Flowable源码地址:https://github.com/flowable/flowable-engine
包路径:org.flowable.engine.test.api.event
FlowableEventDispatcherTest Flowable事件调度器测试类
/**
* @author Frederik Heremans
*/
public class FlowableEventDispatcherTest extends PluggableFlowableTestCase {
protected FlowableEventDispatcher dispatcher;
@BeforeEach
protected void setUp() throws Exception {
dispatcher = new FlowableEventDispatcherImpl();
}
/**
* 测试添加监听器并检查是否向其发送了事件,还检查删除后是否未收到任何事件.
*/
@Test
public void testAddAndRemoveEventListenerAllEvents() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
// 调度事件
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).hasSize(2);
assertThat(newListener.getEventsReceived().get(0)).isEqualTo(event1);
assertThat(newListener.getEventsReceived().get(1)).isEqualTo(event2);
// 删除监听器并再次分派事件,不应调用监听器
dispatcher.removeEventListener(newListener);
newListener.clearEventsReceived();
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
/**
* 测试添加一个监听器,并检查是否向它发送了事件,以及它注册的类型。还检查删除后是否未收到任何事件.
*/
@Test
public void testAddAndRemoveEventListenerTyped() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener, FlowableEngineEventType.ENTITY_CREATED, FlowableEngineEventType.ENTITY_DELETED);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_DELETED);
FlowableEntityEventImpl event3 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_UPDATED);
// 分派事件,三分之二的事件应该已经进入监听器
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event3, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).hasSize(2);
assertThat(newListener.getEventsReceived().get(0)).isEqualTo(event1);
assertThat(newListener.getEventsReceived().get(1)).isEqualTo(event2);
// 删除监听器并再次分派事件,不应调用监听器
dispatcher.removeEventListener(newListener);
newListener.clearEventsReceived();
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
/**
* 测试是否从未调用添加空类型的监听器。
*/
@Test
public void testAddAndRemoveEventListenerTypedNullType() throws Exception {
// 创建一个只将事件添加到列表的监听器
TestFlowableEventListener newListener = new TestFlowableEventListener();
// 将事件监听器添加到调度程序
dispatcher.addEventListener(newListener, (FlowableEngineEventType) null);
TaskServiceConfiguration taskServiceConfiguration = (TaskServiceConfiguration) processEngineConfiguration.getServiceConfigurations()
.get(EngineConfigurationConstants.KEY_TASK_SERVICE_CONFIG);
FlowableEntityEventImpl event1 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_CREATED);
FlowableEntityEventImpl event2 = new FlowableEntityEventImpl(taskServiceConfiguration.getTaskEntityManager().create(),
FlowableEngineEventType.ENTITY_DELETED);
// 分派事件时,所有事件都应该已进入监听器
dispatcher.dispatchEvent(event1, processEngineConfiguration.getEngineCfgKey());
dispatcher.dispatchEvent(event2, processEngineConfiguration.getEngineCfgKey());
assertThat(newListener.getEventsReceived()).isEmpty();
}
...
}
Flowable事件调度器测试
该博客主要介绍了Flowable事件调度器的测试用例。首先,创建了一个FlowableEventDispatcher实例,然后添加了一个监听器并发送了不同类型的事件,验证了监听器能否正确接收和过滤事件。测试包括添加监听器接收所有事件、指定类型事件以及空类型事件的情况,并检查了移除监听器后不再接收事件的逻辑。
事件调度器测试类(上)&spm=1001.2101.3001.5002&articleId=122772454&d=1&t=3&u=40711097e22e4452b38f6d7817c4642a)
3671

被折叠的 条评论
为什么被折叠?



