Search in sources :

Example 21 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProtocolHandlerWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader loader = mock(NarClassLoader.class);
    String protocol = "test-protocol";
    ProtocolHandler h = new ProtocolHandler() {

        @Override
        public String protocolName() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return protocol;
        }

        @Override
        public boolean accept(String protocol) {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return true;
        }

        @Override
        public void initialize(ServiceConfiguration conf) throws Exception {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            throw new Exception("test exception");
        }

        @Override
        public String getProtocolDataToAdvertise() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return "test-protocol-data";
        }

        @Override
        public void start(BrokerService service) {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
        }

        @Override
        public Map<InetSocketAddress, ChannelInitializer<SocketChannel>> newChannelInitializers() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
            return null;
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), loader);
        }
    };
    ProtocolHandlerWithClassLoader wrapper = new ProtocolHandlerWithClassLoader(h, loader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    assertEquals(wrapper.protocolName(), protocol);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    assertTrue(wrapper.accept(protocol));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    ServiceConfiguration conf = new ServiceConfiguration();
    expectThrows(Exception.class, () -> wrapper.initialize(conf));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    assertEquals(wrapper.getProtocolDataToAdvertise(), "test-protocol-data");
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    BrokerService service = mock(BrokerService.class);
    wrapper.start(service);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    assertNull(wrapper.newChannelInitializers());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    wrapper.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) InetSocketAddress(java.net.InetSocketAddress) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) ChannelInitializer(io.netty.channel.ChannelInitializer) BrokerService(org.apache.pulsar.broker.service.BrokerService) Test(org.testng.annotations.Test)

Example 22 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class BrokerInterceptorUtilsTest method testLoadBrokerEventListener.

@Test
public void testLoadBrokerEventListener() throws Exception {
    BrokerInterceptorDefinition def = new BrokerInterceptorDefinition();
    def.setInterceptorClass(MockBrokerInterceptor.class.getName());
    def.setDescription("test-broker-listener");
    String archivePath = "/path/to/broker/listener/nar";
    BrokerInterceptorMetadata metadata = new BrokerInterceptorMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(BrokerInterceptorUtils.BROKER_INTERCEPTOR_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockBrokerInterceptor.class;
    when(mockLoader.loadClass(eq(MockBrokerInterceptor.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        BrokerInterceptorWithClassLoader returnedPhWithCL = BrokerInterceptorUtils.load(metadata, "");
        BrokerInterceptor returnedPh = returnedPhWithCL.getInterceptor();
        assertSame(mockLoader, returnedPhWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockBrokerInterceptor);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 23 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class BrokerInterceptorUtilsTest method testLoadBrokerEventListenerWithBlankListerClass.

@Test(expectedExceptions = IOException.class)
public void testLoadBrokerEventListenerWithBlankListerClass() throws Exception {
    BrokerInterceptorDefinition def = new BrokerInterceptorDefinition();
    def.setDescription("test-broker-listener");
    String archivePath = "/path/to/broker/listener/nar";
    BrokerInterceptorMetadata metadata = new BrokerInterceptorMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(BrokerInterceptorUtils.BROKER_INTERCEPTOR_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockBrokerInterceptor.class;
    when(mockLoader.loadClass(eq(MockBrokerInterceptor.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        BrokerInterceptorUtils.load(metadata, "");
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 24 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class BrokerInterceptorWithClassLoaderTest method testWrapper.

@Test
public void testWrapper() throws Exception {
    BrokerInterceptor h = mock(BrokerInterceptor.class);
    NarClassLoader loader = mock(NarClassLoader.class);
    BrokerInterceptorWithClassLoader wrapper = new BrokerInterceptorWithClassLoader(h, loader);
    PulsarService pulsarService = mock(PulsarService.class);
    wrapper.initialize(pulsarService);
    verify(h, times(1)).initialize(same(pulsarService));
}
Also used : PulsarService(org.apache.pulsar.broker.PulsarService) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Test(org.testng.annotations.Test)

Example 25 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class AdditionalServletUtilsTest method testLoadEventListener.

@Test
public void testLoadEventListener() throws Exception {
    AdditionalServletDefinition def = new AdditionalServletDefinition();
    def.setAdditionalServletClass(MockAdditionalServlet.class.getName());
    def.setDescription("test-proxy-listener");
    String archivePath = "/path/to/proxy/listener/nar";
    AdditionalServletMetadata metadata = new AdditionalServletMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(AdditionalServletUtils.ADDITIONAL_SERVLET_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class listenerClass = MockAdditionalServlet.class;
    when(mockLoader.loadClass(eq(MockAdditionalServlet.class.getName()))).thenReturn(listenerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        AdditionalServletWithClassLoader returnedPhWithCL = AdditionalServletUtils.load(metadata, "");
        AdditionalServlet returnedPh = returnedPhWithCL.getServlet();
        assertSame(mockLoader, returnedPhWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockAdditionalServlet);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Aggregations

NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)126 Test (org.testng.annotations.Test)84 IOException (java.io.IOException)53 NarClassLoaderBuilder (org.apache.pulsar.common.nar.NarClassLoaderBuilder)48 File (java.io.File)39 FileInputStream (java.io.FileInputStream)15 UtilityClass (lombok.experimental.UtilityClass)15 Namespace (org.apache.distributedlog.api.namespace.Namespace)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)12 Lists (com.google.common.collect.Lists)9 InputStream (java.io.InputStream)9 Files (java.nio.file.Files)9 Paths (java.nio.file.Paths)9 StandardCopyOption (java.nio.file.StandardCopyOption)9 LinkedList (java.util.LinkedList)9 List (java.util.List)9 Consumer (java.util.function.Consumer)9 Response (javax.ws.rs.core.Response)9