You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/11/22 10:56:42 UTC

[GitHub] [pulsar] BewareMyPower commented on a change in pull request #12917: Protocol Handlers and Proxy Extensions: Fix bootstrap and add tests

BewareMyPower commented on a change in pull request #12917:
URL: https://github.com/apache/pulsar/pull/12917#discussion_r754158725



##########
File path: pulsar-proxy/src/test/java/org/apache/pulsar/proxy/extensions/SimpleProxyExtensionTest.java
##########
@@ -0,0 +1,186 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.proxy.extensions;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.*;
+import io.netty.channel.socket.SocketChannel;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.util.PortManager;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
+import org.apache.pulsar.broker.authentication.AuthenticationService;
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
+import org.apache.pulsar.metadata.impl.ZKMetadataStore;
+import org.apache.pulsar.proxy.server.ProxyConfiguration;
+import org.apache.pulsar.proxy.server.ProxyService;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import static org.mockito.Mockito.doReturn;
+import static org.testng.Assert.assertEquals;
+
+@Slf4j
+@Test(groups = "proxy")
+public class SimpleProxyExtensionTest extends MockedPulsarServiceBaseTest {
+
+    public static final class MyProxyExtension implements ProxyExtension {
+
+        private ProxyConfiguration conf;
+
+        @Override
+        public String extensionName() {
+            return "test";
+        }
+
+        @Override
+        public boolean accept(String protocol) {
+            return "test".equals(protocol);
+        }
+
+        @Override
+        public void initialize(ProxyConfiguration conf) throws Exception {
+            this.conf = conf;
+        }
+
+        @Override
+        public void start(ProxyService service) {
+        }
+
+        @Override
+        public Map<InetSocketAddress, ChannelInitializer<SocketChannel>> newChannelInitializers() {
+            return Collections.singletonMap(new InetSocketAddress(conf.getBindAddress(), PortManager.nextFreePort()),
+                    new ChannelInitializer<SocketChannel>() {
+                @Override
+                protected void initChannel(SocketChannel socketChannel) throws Exception {
+                    socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
+                        @Override
+                        public void channelActive(final ChannelHandlerContext ctx) {
+                            final ByteBuf resp = ctx.alloc().buffer();
+                            resp.writeBytes("ok".getBytes(StandardCharsets.UTF_8));
+
+                            final ChannelFuture f = ctx.writeAndFlush(resp);
+                            f.addListener((ChannelFutureListener) future -> ctx.close());
+                        }
+                        @Override
+                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+                            log.error("error", cause);
+                            ctx.close();
+                        }
+                    });
+                }
+            });
+        }
+
+        @Override
+        public void close() {
+
+        }
+    }
+
+    private File tempDirectory;
+    private ProxyService proxyService;
+    private ProxyConfiguration proxyConfig = new ProxyConfiguration();
+
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        tempDirectory = Files.createTempDirectory("SimpleProxyExtensionTest").toFile();
+
+        super.internalSetup();
+        proxyConfig.setProxyExtensionsDirectory(tempDirectory.getAbsolutePath());
+        proxyConfig.setProxyExtensions(Collections.singleton("test"));
+        buildMockNarFile(tempDirectory);
+        proxyConfig.setServicePort(Optional.ofNullable(0));
+        proxyConfig.setZookeeperServers(DUMMY_VALUE);
+        proxyConfig.setConfigurationStoreServers(GLOBAL_DUMMY_VALUE);
+
+        proxyService = Mockito.spy(new ProxyService(proxyConfig, new AuthenticationService(
+                PulsarConfigurationLoader.convertFrom(proxyConfig))));
+        doReturn(new ZKMetadataStore(mockZooKeeper)).when(proxyService).createLocalMetadataStore();
+        doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(proxyService).createConfigurationMetadataStore();
+
+        proxyService.start();
+    }
+
+    @Test
+    public void testBootstrapProtocolHandler() throws Exception {
+        SocketAddress address =
+                proxyService.getProxyExtensions()
+                      .getEndpoints()
+                        .entrySet()
+                        .stream()
+                        .filter(e -> e.getValue().equals("test"))
+                        .map(Map.Entry::getKey)
+                        .findAny()
+                        .get();
+        try (Socket socket =  new Socket();) {

Review comment:
       ```suggestion
           try (Socket socket = new Socket()) {
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org