Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable idle event handler if echo is disabled #138

Merged
merged 2 commits into from
Jan 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ internal constructor(
if (configuration.replyOnError()) {
pipeline.addLast(workerGroup, "replyOnError", parseExceptionHandler)
}
pipeline.addLast("idleState", IdleStateHandler(0, 0, configuration.idleTimeout))
pipeline.addLast("idleEventHandler", IdleEventHandler(isoMessageFactory))
if (configuration.shouldAddEchoMessageListener()) {
pipeline.addLast("idleState", IdleStateHandler(0, 0, configuration.idleTimeout))
pipeline.addLast("idleEventHandler", IdleEventHandler(isoMessageFactory))
}
pipeline.addLast(workerGroup, *customChannelHandlers)
configurer?.configurePipeline(pipeline, configuration)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,26 @@ class EchoFromClientIT extends AbstractIT {

@Override
protected void configureServer(final Iso8583Server<IsoMessage> server) {
server.setConfigurer(new ConnectorConfigurer<>() {
server.setConfigurer(new ConnectorConfigurer<>() {

@Override
public void configurePipeline(final ChannelPipeline pipeline, final ServerConfiguration configuration) {
pipeline.addBefore("idleEventHandler", "connectListenerHandler", new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
final var message = server.getIsoMessageFactory().newMessage(0x800);
ctx.writeAndFlush(message);
}
});
}
@Override
public void configurePipeline(final ChannelPipeline pipeline,
final ServerConfiguration configuration) {
final var echoHandler = new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
final var message = server.getIsoMessageFactory().newMessage(0x800);
ctx.writeAndFlush(message);
}
};

if (pipeline.get("idleEventHandler") != null) {
pipeline.addBefore("idleEventHandler", "connectListenerHandler", echoHandler);
} else {
pipeline.addLast("connectListenerHandler", echoHandler);
}
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.timeout.IdleStateHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -19,7 +20,7 @@
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class Iso8583ChannelInitializerTest {
class Iso8583ChannelInitializerTest {

@Mock
private EventLoopGroup workerGroup;
Expand All @@ -44,7 +45,7 @@ public void setUp() {
}

@Test
public void testInitChannelWithLogger() {
void testInitChannelWithLogger() {
//given
configurationBuilder.addLoggingHandler(true);
final var channelInitializer = createChannelInitializer(configurer);
Expand All @@ -57,7 +58,7 @@ public void testInitChannelWithLogger() {
}

@Test
public void testInitChannelWithoutLogger() {
void testInitChannelWithoutLogger() {
//given
configurationBuilder.addLoggingHandler(false);

Expand All @@ -67,11 +68,12 @@ public void testInitChannelWithoutLogger() {
channelInitializer.initChannel(channel);

//then
verify(pipeline, never()).addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class));
verify(pipeline, never())
.addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class));
}

@Test
public void testInitChannelWithDefaultLoggingSetting() {
void testInitChannelWithDefaultLoggingSetting() {
//given
final var channelInitializer = createChannelInitializer(configurer);

Expand All @@ -83,6 +85,40 @@ public void testInitChannelWithDefaultLoggingSetting() {
.addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class));
}

@Test
void shouldInitChannelWithEchoEnabled() {
//given
configurationBuilder.addEchoMessageListener(true);
final var channelInitializer = createChannelInitializer(configurer);

//when
channelInitializer.initChannel(channel);

//then
verify(pipeline, times(1))
.addLast(eq("idleState"), any(IdleStateHandler.class));
verify(pipeline, times(1))
.addLast(eq("idleEventHandler"), any(IdleEventHandler.class));

}

@Test
void shouldInitChannelWithEchoDisabled() {
//given
configurationBuilder.addEchoMessageListener(false);

final var channelInitializer = createChannelInitializer(configurer);

//when
channelInitializer.initChannel(channel);

//then
verify(pipeline, never())
.addLast(anyString(), any(IdleStateHandler.class));
verify(pipeline, never())
.addLast(anyString(), any(IdleEventHandler.class));
}

private Iso8583ChannelInitializer<Channel, AbstractBootstrap<?, ?>, ConnectorConfiguration> createChannelInitializer(final ConnectorConfigurer<ConnectorConfiguration, AbstractBootstrap<?, ?>> configurer) {
return new Iso8583ChannelInitializer(
configurationBuilder.build(),
Expand Down