|
| 1 | +package com.codingapi.flow.event; |
| 2 | + |
| 3 | +import com.codingapi.springboot.framework.event.IHandler; |
| 4 | +import com.codingapi.springboot.framework.event.SpringDefaultEventHandler; |
| 5 | +import com.codingapi.springboot.framework.properties.FrameworkProperties; |
| 6 | +import com.codingapi.springboot.framework.properties.PropertiesContext; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 13 | +import org.springframework.context.annotation.Bean; |
| 14 | +import org.springframework.context.annotation.Configuration; |
| 15 | +import org.springframework.transaction.PlatformTransactionManager; |
| 16 | +import org.springframework.transaction.TransactionDefinition; |
| 17 | +import org.springframework.transaction.support.AbstractPlatformTransactionManager; |
| 18 | +import org.springframework.transaction.support.DefaultTransactionStatus; |
| 19 | +import org.springframework.transaction.support.TransactionTemplate; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 28 | + |
| 29 | +/** |
| 30 | + * 事件发布器与底层事件框架(17.3.3 起默认 {@code @TransactionalEventListener(AFTER_COMMIT, fallbackExecution)}) |
| 31 | + * 的集成测试:使用真实 Spring 上下文与真实事务同步机制,验证 |
| 32 | + * 「事务提交后才投递 + 同流程严格按顺序 + 回滚不投递 + 在事件分区线程内执行」。 |
| 33 | + * |
| 34 | + * <p>与 {@link SpringFlowEventPublisherTest} 的区别:后者直接调用 handler,本测试经过 |
| 35 | + * Spring 的事件多播与事务监听器装配,覆盖两套机制的交互。</p> |
| 36 | + */ |
| 37 | +class SpringFlowEventPublisherIntegrationTest { |
| 38 | + |
| 39 | + private static final List<Integer> HANDLED = new CopyOnWriteArrayList<>(); |
| 40 | + private static final List<String> HANDLED_THREADS = new CopyOnWriteArrayList<>(); |
| 41 | + |
| 42 | + /** |
| 43 | + * 上下文只创建一次:底层 ApplicationHandlerUtils 是静态注册表,反复创建上下文会重复注册 handler |
| 44 | + */ |
| 45 | + private static AnnotationConfigApplicationContext context; |
| 46 | + private static PlatformTransactionManager transactionManager; |
| 47 | + |
| 48 | + private SpringFlowEventPublisher publisher; |
| 49 | + |
| 50 | + static class RecEvent implements IFlowEvent { |
| 51 | + |
| 52 | + private final int id; |
| 53 | + private final String eventKey; |
| 54 | + private final boolean mock; |
| 55 | + |
| 56 | + RecEvent(int id, String eventKey, boolean mock) { |
| 57 | + this.id = id; |
| 58 | + this.eventKey = eventKey; |
| 59 | + this.mock = mock; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean isMock() { |
| 64 | + return mock; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String eventKey() { |
| 69 | + return eventKey; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Configuration |
| 74 | + static class TestConfig { |
| 75 | + |
| 76 | + @Bean |
| 77 | + public PlatformTransactionManager transactionManager() { |
| 78 | + return new NoopTransactionManager(); |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + public IHandler<RecEvent> recEventHandler() { |
| 83 | + return event -> { |
| 84 | + HANDLED.add(event.id); |
| 85 | + HANDLED_THREADS.add(Thread.currentThread().getName()); |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + public SpringDefaultEventHandler springDefaultEventHandler(List<IHandler> handlers) { |
| 91 | + return new SpringDefaultEventHandler(handlers); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * 无实际资源的事务管理器:仅驱动 Spring 的事务同步(begin/commit/rollback)流程。 |
| 97 | + */ |
| 98 | + static class NoopTransactionManager extends AbstractPlatformTransactionManager { |
| 99 | + |
| 100 | + @Override |
| 101 | + protected Object doGetTransaction() { |
| 102 | + return new Object(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected void doBegin(Object transaction, TransactionDefinition definition) { |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected void doCommit(DefaultTransactionStatus status) { |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void doRollback(DefaultTransactionStatus status) { |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @BeforeAll |
| 119 | + static void initContext() { |
| 120 | + PropertiesContext.getInstance().setProperties(new FrameworkProperties()); |
| 121 | + context = new AnnotationConfigApplicationContext(TestConfig.class); |
| 122 | + transactionManager = context.getBean(PlatformTransactionManager.class); |
| 123 | + } |
| 124 | + |
| 125 | + @AfterAll |
| 126 | + static void closeContext() { |
| 127 | + if (context != null) { |
| 128 | + context.close(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @BeforeEach |
| 133 | + void setUp() { |
| 134 | + HANDLED.clear(); |
| 135 | + HANDLED_THREADS.clear(); |
| 136 | + publisher = new SpringFlowEventPublisher(context, 1); |
| 137 | + publisher.afterPropertiesSet(); |
| 138 | + } |
| 139 | + |
| 140 | + @AfterEach |
| 141 | + void tearDown() { |
| 142 | + if (publisher != null) { |
| 143 | + publisher.destroy(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * 测试目标:事务内推送的事件在提交后才由订阅方处理,且同一流程(同 eventKey)严格按顺序。 |
| 149 | + * 前置条件:真实 Spring 上下文 + 真实事务同步 + 事件发布器已注册。 |
| 150 | + * 执行步骤:事务内连续推送两个事件 → 提交前观察 → 提交后观察。 |
| 151 | + * 期望断言:提交前无 handler 执行;提交后按推送顺序执行,且执行线程为事件分区线程。 |
| 152 | + */ |
| 153 | + @Test |
| 154 | + void shouldDispatchAfterCommitInOrderThroughSpringListener() throws Exception { |
| 155 | + TransactionTemplate template = new TransactionTemplate(transactionManager); |
| 156 | + |
| 157 | + template.executeWithoutResult(status -> { |
| 158 | + publisher.push(new RecEvent(1, "process-integration", false)); |
| 159 | + publisher.push(new RecEvent(2, "process-integration", false)); |
| 160 | + assertTrue(HANDLED.isEmpty(), "事务提交前不应有 handler 执行"); |
| 161 | + }); |
| 162 | + |
| 163 | + awaitHandled(2); |
| 164 | + assertEquals(List.of(1, 2), HANDLED, "提交后应按推送顺序投递"); |
| 165 | + assertTrue(HANDLED_THREADS.stream().allMatch(name -> name.startsWith("flow-event-dispatch-")), |
| 166 | + "handler 应在事件分区线程内执行,实际:" + HANDLED_THREADS); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 测试目标:事务回滚时不投递任何事件。 |
| 171 | + * 前置条件:同上。 |
| 172 | + * 执行步骤:事务内推送事件后抛出异常触发回滚。 |
| 173 | + * 期望断言:订阅方始终收不到事件。 |
| 174 | + */ |
| 175 | + @Test |
| 176 | + void shouldNotDispatchWhenTransactionRolledBack() throws Exception { |
| 177 | + TransactionTemplate template = new TransactionTemplate(transactionManager); |
| 178 | + |
| 179 | + assertThrows(IllegalStateException.class, () -> template.executeWithoutResult(status -> { |
| 180 | + publisher.push(new RecEvent(3, "process-rollback", false)); |
| 181 | + throw new IllegalStateException("rollback"); |
| 182 | + })); |
| 183 | + |
| 184 | + Thread.sleep(300); |
| 185 | + assertTrue(HANDLED.isEmpty(), "事务回滚后不应投递事件"); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * 测试目标:无事务时的事件立即投递(模拟 mock 模式 / 定时任务)。 |
| 190 | + * 前置条件:不在事务上下文中。 |
| 191 | + * 执行步骤:直接推送事件。 |
| 192 | + * 期望断言:订阅方收到事件。 |
| 193 | + */ |
| 194 | + @Test |
| 195 | + void shouldDispatchImmediatelyWithoutTransaction() throws Exception { |
| 196 | + publisher.push(new RecEvent(4, "process-no-tx", true)); |
| 197 | + |
| 198 | + awaitHandled(1); |
| 199 | + assertEquals(List.of(4), HANDLED); |
| 200 | + } |
| 201 | + |
| 202 | + private void awaitHandled(int expected) throws InterruptedException { |
| 203 | + long deadline = System.currentTimeMillis() + 3000; |
| 204 | + while (System.currentTimeMillis() < deadline && HANDLED.size() < expected) { |
| 205 | + Thread.sleep(10); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments