From ae282d5e12f37b6afe6a3522b0e6f07f156e2f29 Mon Sep 17 00:00:00 2001 From: Karthik Yagna Date: Thu, 19 Oct 2023 22:15:46 -0700 Subject: [PATCH 1/2] Separate adding push handlers in initializer --- .../push/PushMessageSenderInitializer.java | 11 +---- .../PushMessageSenderInitializerTest.java | 44 +++++++++++++++++++ .../SamplePushMessageSenderInitializer.java | 8 ++-- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java diff --git a/zuul-core/src/main/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializer.java b/zuul-core/src/main/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializer.java index 4106372f31..25d9558a15 100644 --- a/zuul-core/src/main/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializer.java +++ b/zuul-core/src/main/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializer.java @@ -26,20 +26,13 @@ * Date: 5/16/18 */ public abstract class PushMessageSenderInitializer extends ChannelInitializer { - - 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); } diff --git a/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java b/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java new file mode 100644 index 0000000000..caa673c80c --- /dev/null +++ b/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java @@ -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}. + */ +class PushMessageSenderInitializerTest { + private PushMessageSenderInitializer initializer; + private Channel channel; + private ChannelHandler handler; + + @BeforeEach + 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 + void testInitChannel() throws Exception { + initializer.initChannel(channel); + + assertNotNull(channel.pipeline().context(HttpServerCodec.class)); + assertNotNull(channel.pipeline().context(HttpObjectAggregator.class)); + assertNotNull(channel.pipeline().get("mockHandler")); + } +} diff --git a/zuul-sample/src/main/java/com/netflix/zuul/sample/push/SamplePushMessageSenderInitializer.java b/zuul-sample/src/main/java/com/netflix/zuul/sample/push/SamplePushMessageSenderInitializer.java index b0de46d4de..ea61c8f46c 100644 --- a/zuul-sample/src/main/java/com/netflix/zuul/sample/push/SamplePushMessageSenderInitializer.java +++ b/zuul-sample/src/main/java/com/netflix/zuul/sample/push/SamplePushMessageSenderInitializer.java @@ -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; @@ -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); } } From 4b850f6e692a9a39bc08481f86e82fb8447d9951 Mon Sep 17 00:00:00 2001 From: Karthik Yagna Date: Thu, 19 Oct 2023 22:39:32 -0700 Subject: [PATCH 2/2] Add license header --- .../push/PushMessageSenderInitializerTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java b/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java index caa673c80c..b5e5805ef1 100644 --- a/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java +++ b/zuul-core/src/test/java/com/netflix/zuul/netty/server/push/PushMessageSenderInitializerTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2023 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.netflix.zuul.netty.server.push; import static org.junit.jupiter.api.Assertions.assertNotNull;