|
@@ -0,0 +1,53 @@
|
|
|
|
|
+package com.payment.platform.module.payment.stream;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.transaction.event.TransactionPhase;
|
|
|
|
|
+import org.springframework.transaction.event.TransactionalEventListener;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+
|
|
|
|
|
+class OrderEventRelayTest {
|
|
|
|
|
+
|
|
|
|
|
+ static class CapturingHub extends OrderEventHub {
|
|
|
|
|
+ final List<String> submitted = new CopyOnWriteArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void submitBroadcast(String channel, Long tenantId) {
|
|
|
|
|
+ submitted.add(channel + "#" + tenantId);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private final CapturingHub hub = new CapturingHub();
|
|
|
|
|
+ private final OrderEventRelay relay = new OrderEventRelay(hub);
|
|
|
|
|
+
|
|
|
|
|
+ @AfterEach
|
|
|
|
|
+ void tearDown() {
|
|
|
|
|
+ hub.shutdown();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void onOrderChanged_delegatesToHub() {
|
|
|
|
|
+ relay.onOrderChanged(new OrderChangedEvent("lsy_order", 5L));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(List.of("lsy_order#5"), hub.submitted);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void theAfterCommitSemanticsArePinnedInCode() throws Exception {
|
|
|
|
|
+ // 容器级测试要拉 Spring 上下文(本仓 81 个测试类一个都没拉过,会撞 leshouying.enabled
|
|
|
|
|
+ // 启动闸与库隧道依赖),所以「提交后才投递 + 无事务也投递」这条语义在这里用注解断言钉住;
|
|
|
|
|
+ // 真事务行为由 Task 14 的开放 API 建单 E2E 端到端验(信号早于提交 ⇒ 重拉看不到新行 ⇒ 红)。
|
|
|
|
|
+ Method m = OrderEventRelay.class.getMethod("onOrderChanged", OrderChangedEvent.class);
|
|
|
|
|
+ TransactionalEventListener ann = m.getAnnotation(TransactionalEventListener.class);
|
|
|
|
|
+ assertNotNull(ann, "没有这个注解 = 事件永远不被投递");
|
|
|
|
|
+ assertEquals(TransactionPhase.AFTER_COMMIT, ann.phase(),
|
|
|
|
|
+ "改回 BEFORE_COMMIT 会造出「信号先到、库还没提交」的竞态");
|
|
|
|
|
+ assertTrue(ann.fallbackExecution(),
|
|
|
|
|
+ "false = 乐收赢那些刻意无事务的写库腿一帧都发不出去(静默失效)");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|