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);
}
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);
}
}
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, "");
}
}
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));
}
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);
}
}
Aggregations