use of io.netty.util.concurrent.DefaultThreadFactory in project incubator-pulsar by apache.
the class ZookeeperCacheTest method testInvalidateCacheOnFailure.
/**
* <pre>
* Verifies that if {@link ZooKeeperCache} fails to fetch data into the cache then
* (1) it invalidates failed future so, next time it helps to get fresh data from zk
* (2) handles zk.getData() unexpected exception if zkSession is lost
* </pre>
*
* @throws Exception
*/
@Test(timeOut = 10000)
public void testInvalidateCacheOnFailure() throws Exception {
ExecutorService zkExecutor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("mockZk"));
// add readOpDelayMs so, main thread will not serve zkCacahe-returned future and let zkExecutor-thread handle
// callback-result process
MockZooKeeper zkClient = MockZooKeeper.newInstance(zkExecutor, 100);
ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor);
final AtomicInteger count = new AtomicInteger(0);
ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) {
@Override
public String deserialize(String key, byte[] content) throws Exception {
if (count.getAndIncrement() == 0) {
throw new NullPointerException("data is null");
} else {
return new String(content);
}
}
};
String value = "test";
String key1 = "/zkDesrializationExceptionTest";
String key2 = "/zkSessionExceptionTest";
zkClient.create(key1, value.getBytes(), null, null);
zkClient.create(key2, value.getBytes(), null, null);
// (1) deserialization will fail so, result should be exception
try {
zkCache.getAsync(key1).get();
fail("it should have failed with NPE");
} catch (Exception e) {
assertTrue(e.getCause() instanceof NullPointerException);
}
// (2) sleep to let cache to be invalidated async
Thread.sleep(1000);
// (3) now, cache should be invalidate failed-future and should refetch the data
assertEquals(zkCache.getAsync(key1).get().get(), value);
// (4) make zk-session invalid
ZooKeeper zkSession = zkCacheService.zkSession.get();
zkCacheService.zkSession.set(null);
try {
zkCache.getAsync(key2).get();
fail("it should have failed with NPE");
} catch (Exception e) {
assertTrue(e.getCause() instanceof NullPointerException);
}
// global-Zk session is connected now
zkCacheService.zkSession.set(zkSession);
// (5) sleep to let cache to be invalidated async
Thread.sleep(1000);
// (6) now, cache should be invalidate failed-future and should refetch the data
assertEquals(zkCache.getAsync(key1).get().get(), value);
zkExecutor.shutdown();
}
use of io.netty.util.concurrent.DefaultThreadFactory in project incubator-pulsar by apache.
the class ConnectionPoolTest method testSingleIpAddress.
@Test
public void testSingleIpAddress() throws Exception {
ClientConfigurationData conf = new ClientConfigurationData();
EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, new DefaultThreadFactory("test"));
ConnectionPool pool = Mockito.spy(new ConnectionPool(conf, eventLoop));
conf.setServiceUrl(serviceUrl);
PulsarClientImpl client = new PulsarClientImpl(conf, eventLoop, pool);
List<InetAddress> result = Lists.newArrayList();
result.add(InetAddress.getByName("127.0.0.1"));
Mockito.when(pool.resolveName("non-existing-dns-name")).thenReturn(CompletableFuture.completedFuture(result));
client.createProducer("persistent://sample/standalone/ns/my-topic");
client.close();
}
use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.
the class MsgEchoPeerBase method run.
public void run() throws Exception {
// Configure the peer.
final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
try {
final Bootstrap boot = new Bootstrap();
boot.group(connectGroup).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).handler(new ChannelInitializer<UdtChannel>() {
@Override
public void initChannel(final UdtChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new MsgEchoPeerHandler(messageSize));
}
});
// Start the peer.
final ChannelFuture f = boot.connect(peer, self).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
connectGroup.shutdownGracefully();
}
}
use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.
the class ByteEchoClient method main.
public static void main(String[] args) throws Exception {
// Configure the client.
final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);
try {
final Bootstrap boot = new Bootstrap();
boot.group(connectGroup).channelFactory(NioUdtProvider.BYTE_CONNECTOR).handler(new ChannelInitializer<UdtChannel>() {
@Override
public void initChannel(final UdtChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoClientHandler());
}
});
// Start the client.
final ChannelFuture f = boot.connect(HOST, PORT).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
connectGroup.shutdownGracefully();
}
}
use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.
the class ByteEchoServer method main.
public static void main(String[] args) throws Exception {
final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);
// Configure the server.
try {
final ServerBootstrap boot = new ServerBootstrap();
boot.group(acceptGroup, connectGroup).channelFactory(NioUdtProvider.BYTE_ACCEPTOR).option(ChannelOption.SO_BACKLOG, 10).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<UdtChannel>() {
@Override
public void initChannel(final UdtChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoServerHandler());
}
});
// Start the server.
final ChannelFuture future = boot.bind(PORT).sync();
// Wait until the server socket is closed.
future.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
acceptGroup.shutdownGracefully();
connectGroup.shutdownGracefully();
}
}
Aggregations