Flowable源码注释(十三)事件调度器测试类(上)

该博客主要介绍了Flowable事件调度器的测试用例。首先,创建了一个FlowableEventDispatcher实例,然后添加了一个监听器并发送了不同类型的事件,验证了监听器能否正确接收和过滤事件。测试包括添加监听器接收所有事件、指定类型事件以及空类型事件的情况,并检查了移除监听器后不再接收事件的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }
	
    ...
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值