Skip to content

Commit

Permalink
Separate adding push handlers in initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik Yagna committed Oct 20, 2023
1 parent 8eeb23e commit d7f3cb0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@
* Date: 5/16/18
*/
public abstract class PushMessageSenderInitializer extends ChannelInitializer<Channel> {

private final PushConnectionRegistry pushConnectionRegistry;

public PushMessageSenderInitializer(PushConnectionRegistry pushConnectionRegistry) {
this.pushConnectionRegistry = pushConnectionRegistry;
}

@Override
protected void initChannel(Channel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(getPushMessageSender(pushConnectionRegistry));
addPushMessageHandlers(pipeline);
}

protected abstract PushMessageSender getPushMessageSender(PushConnectionRegistry pushConnectionRegistry);
protected abstract void addPushMessageHandlers(final ChannelPipeline pipeline);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.netflix.zuul.netty.server.push;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link PushMessageSenderInitializer}.
*/
public class PushMessageSenderInitializerTest {
private PushMessageSenderInitializer initializer;
private Channel channel;
private ChannelHandler handler;

@BeforeEach
public void setUp() {
handler = mock(ChannelHandler.class); // Initialize mock handler

initializer = new PushMessageSenderInitializer() {
@Override
protected void addPushMessageHandlers(ChannelPipeline pipeline) {
pipeline.addLast("mockHandler", handler);
}
};

channel = new EmbeddedChannel();
}

@Test
public void testInitChannel() throws Exception {
initializer.initChannel(channel);

assertNotNull(channel.pipeline().context(HttpServerCodec.class));
assertNotNull(channel.pipeline().context(HttpObjectAggregator.class));
assertNotNull(channel.pipeline().get("mockHandler"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.netflix.zuul.netty.server.push.PushConnectionRegistry;
import com.netflix.zuul.netty.server.push.PushMessageSender;
import com.netflix.zuul.netty.server.push.PushMessageSenderInitializer;

import io.netty.channel.ChannelPipeline;
import javax.inject.Inject;
import javax.inject.Singleton;

Expand All @@ -33,12 +33,12 @@ public class SamplePushMessageSenderInitializer extends PushMessageSenderInitial

@Inject
public SamplePushMessageSenderInitializer(PushConnectionRegistry pushConnectionRegistry) {
super(pushConnectionRegistry);
super();
pushMessageSender = new SamplePushMessageSender(pushConnectionRegistry);
}

@Override
protected PushMessageSender getPushMessageSender(PushConnectionRegistry pushConnectionRegistry) {
return pushMessageSender;
protected void addPushMessageHandlers(ChannelPipeline pipeline) {
pipeline.addLast(pushMessageSender);
}
}

0 comments on commit d7f3cb0

Please sign in to comment.