From 81b2208b14a444f980a5f76e58509593f4ae5c3c Mon Sep 17 00:00:00 2001 From: loong10k <20489781+loong10k@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:38:44 +0800 Subject: [PATCH 1/3] Use supported string utility for handler definitions --- .../boot/DisruptorEventHandlerCreater.java | 4 +- .../DisruptorEventHandlerCreaterTest.java | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java diff --git a/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java b/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java index 6a79d84..a26e5be 100644 --- a/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java +++ b/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java @@ -10,12 +10,12 @@ import com.lmax.disruptor.event.handler.chain.HandlerChainManager; import com.lmax.disruptor.event.handler.chain.def.DefaultHandlerChainManager; import com.lmax.disruptor.event.handler.chain.def.PathMatchingHandlerChainResolver; -import com.lmax.disruptor.util.StringUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationContext; import org.springframework.core.OrderComparator; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import java.util.*; @@ -131,7 +131,7 @@ public List create(DisruptorProperties properties) { protected DisruptorEventDispatcher createDisruptorEventHandler(EventHandlerDefinition handlerDefinition, Map> eventHandlers) { - if (StringUtils.isNotEmpty(handlerDefinition.getDefinitions())) { + if (StringUtils.hasLength(handlerDefinition.getDefinitions())) { handlerChainDefinitionMap.putAll(this.parseHandlerChainDefinitions(handlerDefinition.getDefinitions())); } else if (!CollectionUtils.isEmpty(handlerDefinition.getDefinitionMap())) { handlerChainDefinitionMap.putAll(handlerDefinition.getDefinitionMap()); diff --git a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java new file mode 100644 index 0000000..f3a2b44 --- /dev/null +++ b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java @@ -0,0 +1,67 @@ +package com.lmax.disruptor.spring.boot; + +import com.lmax.disruptor.config.EventHandlerDefinition; +import com.lmax.disruptor.event.DisruptorEvent; +import com.lmax.disruptor.event.handler.DisruptorHandler; +import com.lmax.disruptor.event.handler.chain.HandlerChainManager; +import com.lmax.disruptor.event.handler.chain.def.DefaultHandlerChainManager; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DisruptorEventHandlerCreaterTest { + + @Test + void parsesNonEmptyDefinitionsBeforeUsingDefinitionMap() { + CapturingCreater creater = new CapturingCreater(); + EventHandlerDefinition definition = new EventHandlerDefinition(); + definition.setDefinitions("[/urls]\n/events = handler"); + definition.setDefinitionMap(Collections.singletonMap("/fallback", "other")); + + creater.createDisruptorEventHandler(definition, Collections.emptyMap()); + + assertEquals(1, creater.parseCalls); + assertEquals(Collections.singletonMap("/events", "handler"), creater.chainDefinitions); + } + + @Test + void usesDefinitionMapWhenDefinitionsAreEmptyOrNull() { + for (String definitions : new String[] {"", ""}) { + CapturingCreater creater = new CapturingCreater(); + EventHandlerDefinition definition = new EventHandlerDefinition(); + definition.setDefinitions("".equals(definitions) ? null : definitions); + definition.setDefinitionMap(Collections.singletonMap("/fallback", "handler")); + + creater.createDisruptorEventHandler(definition, Collections.emptyMap()); + + assertEquals(0, creater.parseCalls); + assertEquals(Collections.singletonMap("/fallback", "handler"), creater.chainDefinitions); + } + } + + private static final class CapturingCreater extends DisruptorEventHandlerCreater { + private int parseCalls; + private Map chainDefinitions; + + private CapturingCreater() { + super(null); + } + + @Override + protected Map parseHandlerChainDefinitions(String definitions) { + parseCalls++; + return Collections.singletonMap("/events", "handler"); + } + + @Override + protected HandlerChainManager createHandlerChainManager( + Map> eventHandlers, + Map handlerChainDefinitionMap) { + chainDefinitions = Map.copyOf(handlerChainDefinitionMap); + return new DefaultHandlerChainManager(); + } + } +} From b4b66247b838b16be7efc6fd6f9ab2e2c20ad45a Mon Sep 17 00:00:00 2001 From: loong10k <20489781+loong10k@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:54:45 +0800 Subject: [PATCH 2/3] Run annotation processors and cover Disruptor behavior --- pom.xml | 1 + .../boot/DisruptorAutoConfigurationTest.java | 38 ++++++++++++ .../DisruptorEventHandlerCreaterTest.java | 44 +++++++++++++- .../boot/context/DisruptorContextTest.java | 58 +++++++++++++++++++ .../EventPublicationInterceptorTest.java | 48 +++++++++++++++ .../event/DisruptorApplicationEventTest.java | 17 ++++++ 6 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java create mode 100644 src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java create mode 100644 src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java create mode 100644 src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java diff --git a/pom.xml b/pom.xml index af45e2c..bf0ad57 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,7 @@ ${java.version} ${java.version} ${project.build.sourceEncoding} + full 512M diff --git a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java new file mode 100644 index 0000000..2485405 --- /dev/null +++ b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java @@ -0,0 +1,38 @@ +package com.lmax.disruptor.spring.boot; + +import com.lmax.disruptor.dsl.Disruptor; +import com.lmax.disruptor.event.DisruptorEvent; +import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent; +import org.junit.jupiter.api.Test; +import org.springframework.context.support.GenericApplicationContext; + +import static org.junit.jupiter.api.Assertions.*; + +class DisruptorAutoConfigurationTest { + @Test + void startsDisruptorAndPublishesApplicationEvents() { + GenericApplicationContext spring = new GenericApplicationContext(); + spring.refresh(); + DisruptorAutoConfiguration configuration = new DisruptorAutoConfiguration(); + configuration.setApplicationContext(spring); + assertSame(spring, configuration.getApplicationContext()); + DisruptorProperties properties = new DisruptorProperties(); + Disruptor disruptor = configuration.disruptor(properties, configuration.eventFactory()); + try { + assertNotNull(disruptor.getRingBuffer()); + assertNotNull(configuration.oneArgEventTranslator()); + assertNotNull(configuration.twoArgEventTranslator()); + assertNotNull(configuration.threeArgEventTranslator()); + assertNotNull(configuration.disruptorTemplate(disruptor, configuration.oneArgEventTranslator())); + assertNotNull(configuration.disruptorEventAwareProcessor()); + long before = disruptor.getRingBuffer().getCursor(); + DisruptorEvent event = new DisruptorEvent("source"); + configuration.disruptorEventListener(disruptor, configuration.oneArgEventTranslator()) + .onApplicationEvent(new DisruptorApplicationEvent(event)); + assertTrue(disruptor.getRingBuffer().getCursor() > before); + } finally { + disruptor.shutdown(); + spring.close(); + } + } +} diff --git a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java index f3a2b44..e2fa241 100644 --- a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java +++ b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java @@ -1,16 +1,20 @@ package com.lmax.disruptor.spring.boot; +import com.lmax.disruptor.annotation.EventRule; import com.lmax.disruptor.config.EventHandlerDefinition; import com.lmax.disruptor.event.DisruptorEvent; import com.lmax.disruptor.event.handler.DisruptorHandler; +import com.lmax.disruptor.event.handler.Nameable; +import com.lmax.disruptor.event.handler.chain.HandlerChain; import com.lmax.disruptor.event.handler.chain.HandlerChainManager; import com.lmax.disruptor.event.handler.chain.def.DefaultHandlerChainManager; import org.junit.jupiter.api.Test; +import org.springframework.context.support.GenericApplicationContext; import java.util.Collections; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; class DisruptorEventHandlerCreaterTest { @@ -42,6 +46,44 @@ void usesDefinitionMapWhenDefinitionsAreEmptyOrNull() { } } + @Test + void discoversAnnotatedHandlersAndBuildsDefaultAndConfiguredChains() { + GenericApplicationContext context = new GenericApplicationContext(); + context.registerBean("handler", RecordingHandler.class); + context.refresh(); + try { + DisruptorEventHandlerCreater creater = new DisruptorEventHandlerCreater(context); + assertSame(context, creater.getApplicationContext()); + Map> handlers = creater.getEventHandlers(); + assertEquals(1, handlers.size()); + assertSame(context.getBean("handler"), handlers.get("handler")); + + HandlerChainManager manager = creater.createHandlerChainManager( + handlers, Collections.singletonMap("/events", "handler")); + assertTrue(manager.hasChains()); + assertTrue(manager.getChainNames().contains("/events")); + assertEquals("handler", ((RecordingHandler) handlers.get("handler")).name); + + DisruptorProperties defaults = new DisruptorProperties(); + assertEquals(1, creater.create(defaults).size()); + EventHandlerDefinition definition = new EventHandlerDefinition(); + definition.setOrder(7); + definition.setDefinitions("[urls]\n/events = handler"); + defaults.setHandlerDefinitions(Collections.singletonList(definition)); + assertEquals(7, creater.create(defaults).get(0).getOrder()); + assertEquals("handler", creater.parseHandlerChainDefinitions(definition.getDefinitions()).get("/events")); + } finally { + context.close(); + } + } + + @EventRule("/events") + public static class RecordingHandler implements DisruptorHandler, Nameable { + private String name; + @Override public void doHandler(DisruptorEvent event, HandlerChain chain) { } + @Override public void setName(String name) { this.name = name; } + } + private static final class CapturingCreater extends DisruptorEventHandlerCreater { private int parseCalls; private Map chainDefinitions; diff --git a/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java b/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java new file mode 100644 index 0000000..6b66d87 --- /dev/null +++ b/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java @@ -0,0 +1,58 @@ +package com.lmax.disruptor.spring.boot.context; + +import com.lmax.disruptor.event.DisruptorEvent; +import com.lmax.disruptor.event.DisruptorEventPublisher; +import com.lmax.disruptor.event.DisruptorEventPublisherAware; +import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.Aware; +import org.springframework.context.support.GenericApplicationContext; + +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.*; + +class DisruptorContextTest { + @Test + void forwardsEventsToSpringContext() { + GenericApplicationContext spring = new GenericApplicationContext(); + AtomicReference received = new AtomicReference<>(); + spring.addApplicationListener(event -> { + if (event instanceof DisruptorApplicationEvent) { + received.set((DisruptorApplicationEvent) event); + } + }); + spring.refresh(); + try { + DisruptorApplicationContext context = new DisruptorApplicationContext(); + context.setApplicationContext(spring); + DisruptorEvent event = new DisruptorEvent("source"); + context.publishEvent(event); + assertSame(spring, context.getApplicationContext()); + assertSame(event, received.get().getSource()); + } finally { + spring.close(); + } + } + + @Test + void injectsPublisherIntoAwareBeans() throws Exception { + GenericApplicationContext spring = new GenericApplicationContext(); + DisruptorEventAwareProcessor processor = new DisruptorEventAwareProcessor(); + processor.setApplicationContext(spring); + processor.afterPropertiesSet(); + PublisherAwareBean bean = new PublisherAwareBean(); + assertSame(bean, processor.postProcessBeforeInitialization(bean, "bean")); + assertNotNull(bean.publisher); + assertSame(bean, processor.postProcessAfterInitialization(bean, "bean")); + Object ordinary = new Object(); + assertSame(ordinary, processor.postProcessBeforeInitialization(ordinary, "ordinary")); + } + + private static final class PublisherAwareBean implements Aware, DisruptorEventPublisherAware { + private DisruptorEventPublisher publisher; + @Override public void setDisruptorEventPublisher(DisruptorEventPublisher publisher) { + this.publisher = publisher; + } + } +} diff --git a/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java b/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java new file mode 100644 index 0000000..e488b77 --- /dev/null +++ b/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java @@ -0,0 +1,48 @@ +package com.lmax.disruptor.spring.boot.context.event; + +import com.lmax.disruptor.event.DisruptorEvent; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.*; + +class EventPublicationInterceptorTest { + @Test + void publishesAnEventAfterTheMethodReturns() throws Throwable { + EventPublicationInterceptor interceptor = new EventPublicationInterceptor(); + AtomicReference published = new AtomicReference<>(); + interceptor.setApplicationEventClass(PublishedEvent.class); + interceptor.setDisruptorEventPublisher(published::set); + interceptor.afterPropertiesSet(); + Object target = new Object(); + MethodInvocation invocation = new MethodInvocation() { + @Override public Method getMethod() { return null; } + @Override public Object[] getArguments() { return new Object[0]; } + @Override public Object proceed() { return "result"; } + @Override public Object getThis() { return target; } + @Override public AccessibleObject getStaticPart() { return null; } + }; + assertEquals("result", interceptor.invoke(invocation)); + assertSame(target, published.get().getSource()); + } + + @Test + void rejectsInvalidEventClassesAndMissingConfiguration() { + EventPublicationInterceptor interceptor = new EventPublicationInterceptor(); + assertThrows(IllegalArgumentException.class, interceptor::afterPropertiesSet); + assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(DisruptorEvent.class)); + assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(String.class)); + assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(NoObjectConstructor.class)); + } + + public static class PublishedEvent extends DisruptorEvent { + public PublishedEvent(Object source) { super(source); } + } + public static class NoObjectConstructor extends DisruptorEvent { + public NoObjectConstructor() { super(); } + } +} diff --git a/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java b/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java new file mode 100644 index 0000000..e511d73 --- /dev/null +++ b/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java @@ -0,0 +1,17 @@ +package com.lmax.disruptor.spring.boot.event; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class DisruptorApplicationEventTest { + @Test + void carriesOptionalBoundPayload() { + Object source = new Object(); + DisruptorApplicationEvent event = new DisruptorApplicationEvent(source); + assertSame(source, event.getSource()); + assertNull(event.getBind()); + event.bind("payload"); + assertEquals("payload", event.getBind()); + assertEquals("bound", new DisruptorApplicationEvent(source, "bound").getBind()); + } +} From 9331f50e47b2e0840e8acbbd0fb4c322e73ca6f2 Mon Sep 17 00:00:00 2001 From: Loong Wan Date: Sat, 31 Oct 2026 14:13:58 +0800 Subject: [PATCH 3/3] Run pinned CI on disruptor-spring-boot-starter 4.0.x repair branch --- .github/workflows/ci.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aabc353..0291b1e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: workflow_dispatch: push: - branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x] + branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x, fix/disruptor-string-utils-4.0.x] pull_request: branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x] @@ -12,5 +12,7 @@ permissions: jobs: build: - uses: easy-4-java/.github/.github/workflows/maven-starter-ci.yml@main + uses: easy-4-java/.github/.github/workflows/maven-starter-ci.yml@bf473d6e9490345a8ac2abeb895639f87448c7ff + with: + version-line: ${{ github.ref_name == 'fix/disruptor-string-utils-4.0.x' && '4.0.x' || '' }} secrets: inherit