Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-3.4.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]

Expand All @@ -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-3.4.x' && '3.4.x' || '' }}
secrets: inherit
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<proc>full</proc>
<maxmem>512M</maxmem>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -131,7 +131,7 @@ public List<DisruptorEventDispatcher> create(DisruptorProperties properties) {
protected DisruptorEventDispatcher createDisruptorEventHandler(EventHandlerDefinition handlerDefinition,
Map<String, DisruptorHandler<DisruptorEvent>> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DisruptorEvent> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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.*;

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[] {"", "<null>"}) {
CapturingCreater creater = new CapturingCreater();
EventHandlerDefinition definition = new EventHandlerDefinition();
definition.setDefinitions("<null>".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);
}
}

@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<String, DisruptorHandler<DisruptorEvent>> handlers = creater.getEventHandlers();
assertEquals(1, handlers.size());
assertSame(context.getBean("handler"), handlers.get("handler"));

HandlerChainManager<DisruptorEvent> 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<DisruptorEvent>, Nameable {
private String name;
@Override public void doHandler(DisruptorEvent event, HandlerChain<DisruptorEvent> chain) { }
@Override public void setName(String name) { this.name = name; }
}

private static final class CapturingCreater extends DisruptorEventHandlerCreater {
private int parseCalls;
private Map<String, String> chainDefinitions;

private CapturingCreater() {
super(null);
}

@Override
protected Map<String, String> parseHandlerChainDefinitions(String definitions) {
parseCalls++;
return Collections.singletonMap("/events", "handler");
}

@Override
protected HandlerChainManager<DisruptorEvent> createHandlerChainManager(
Map<String, DisruptorHandler<DisruptorEvent>> eventHandlers,
Map<String, String> handlerChainDefinitionMap) {
chainDefinitions = Map.copyOf(handlerChainDefinitionMap);
return new DefaultHandlerChainManager();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<DisruptorApplicationEvent> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<DisruptorEvent> 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(); }
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading