use of io.netty.channel.WriteBufferWaterMark in project netty by netty.
the class SocketShutdownOutputBySelfTest method testWriteAfterShutdownOutputNoWritabilityChange.
public void testWriteAfterShutdownOutputNoWritabilityChange(Bootstrap cb) throws Throwable {
final TestHandler h = new TestHandler();
ServerSocket ss = new ServerSocket();
Socket s = null;
SocketChannel ch = null;
try {
ss.bind(newSocketAddress());
cb.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(2, 4));
ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel();
assumeFalse(ch instanceof OioSocketChannel);
assertTrue(ch.isActive());
assertFalse(ch.isOutputShutdown());
s = ss.accept();
byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 5, 6 };
ChannelFuture writeFuture = ch.write(Unpooled.wrappedBuffer(expectedBytes));
h.assertWritability(false);
ch.flush();
writeFuture.sync();
h.assertWritability(true);
for (int i = 0; i < expectedBytes.length; ++i) {
assertEquals(expectedBytes[i], s.getInputStream().read());
}
assertTrue(h.ch.isOpen());
assertTrue(h.ch.isActive());
assertFalse(h.ch.isInputShutdown());
assertFalse(h.ch.isOutputShutdown());
// Make the connection half-closed and ensure read() returns -1.
ch.shutdownOutput().sync();
assertEquals(-1, s.getInputStream().read());
assertTrue(h.ch.isOpen());
assertTrue(h.ch.isActive());
assertFalse(h.ch.isInputShutdown());
assertTrue(h.ch.isOutputShutdown());
try {
// If half-closed, the local endpoint shouldn't be able to write
ch.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 2 })).sync();
fail();
} catch (Throwable cause) {
checkThrowable(cause);
}
assertNull(h.writabilityQueue.poll());
} finally {
if (s != null) {
s.close();
}
if (ch != null) {
ch.close();
}
ss.close();
}
}
use of io.netty.channel.WriteBufferWaterMark in project neo4j by neo4j.
the class TransportWriteThrottle method install.
@Override
public void install(Channel channel, MemoryTracker memoryTracker) {
ThrottleLock lock = lockSupplier.get();
memoryTracker.allocateHeap(HeapEstimator.sizeOf(lock) + WRITE_BUFFER_WATER_MARK_SHALLOW_SIZE);
channel.attr(LOCK_KEY).set(lock);
channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(lowWaterMark, highWaterMark));
channel.pipeline().addLast(listener);
}
use of io.netty.channel.WriteBufferWaterMark in project grpc-java by grpc.
the class UtilsTest method setAndValidateGeneric.
private static InternalChannelz.SocketOptions setAndValidateGeneric(Channel channel) {
channel.config().setOption(ChannelOption.SO_LINGER, 3);
// only applicable for OIO channels:
channel.config().setOption(ChannelOption.SO_TIMEOUT, 250);
// Test some arbitrarily chosen options with a non numeric values
channel.config().setOption(ChannelOption.SO_KEEPALIVE, true);
WriteBufferWaterMark writeBufWaterMark = new WriteBufferWaterMark(10, 20);
channel.config().setOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufWaterMark);
InternalChannelz.SocketOptions socketOptions = Utils.getSocketOptions(channel);
assertEquals(3, (int) socketOptions.lingerSeconds);
assertEquals("true", socketOptions.others.get("SO_KEEPALIVE"));
assertEquals(writeBufWaterMark.toString(), socketOptions.others.get(ChannelOption.WRITE_BUFFER_WATER_MARK.toString()));
return socketOptions;
}
use of io.netty.channel.WriteBufferWaterMark in project grpc-java by grpc.
the class NettyServerTest method connectionSettingsPropagated.
@Test(timeout = 60000)
public void connectionSettingsPropagated() throws Exception {
final int originalLowWaterMark = 2097169;
final int originalHighWaterMark = 2097211;
Map<ChannelOption<?>, Object> childChannelOptions = new HashMap<>();
childChannelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(originalLowWaterMark, originalHighWaterMark));
class TestChannelHandler extends ChannelHandlerAdapter {
CountDownLatch countDownLatch = new CountDownLatch(1);
int lowWaterMark;
int highWaterMark;
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
Channel channel = ctx.channel();
WriteBufferWaterMark writeBufferWaterMark = channel.config().getOption(ChannelOption.WRITE_BUFFER_WATER_MARK);
lowWaterMark = writeBufferWaterMark.low();
highWaterMark = writeBufferWaterMark.high();
countDownLatch.countDown();
}
}
final TestChannelHandler channelHandler = new TestChannelHandler();
class TestProtocolNegotiator implements ProtocolNegotiator {
Attributes eagAttributes;
@Override
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler handler) {
eagAttributes = handler.getEagAttributes();
return channelHandler;
}
@Override
public void close() {
}
@Override
public AsciiString scheme() {
return Utils.HTTP;
}
}
Attributes eagAttributes = Attributes.newBuilder().set(Attributes.Key.create("foo"), "bar").build();
TestProtocolNegotiator protocolNegotiator = new TestProtocolNegotiator();
InetSocketAddress addr = new InetSocketAddress(0);
NettyServer ns = new NettyServer(Arrays.asList(addr), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), childChannelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, protocolNegotiator, Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
1, // ignore
false, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
true, // ignore
0, eagAttributes, channelz);
ns.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
return new NoopServerTransportListener();
}
@Override
public void serverShutdown() {
}
});
Socket socket = new Socket();
socket.connect(ns.getListenSocketAddress(), /* timeout= */
8000);
channelHandler.countDownLatch.await();
socket.close();
assertThat(protocolNegotiator.eagAttributes).isSameInstanceAs(eagAttributes);
assertThat(channelHandler.lowWaterMark).isEqualTo(originalLowWaterMark);
assertThat(channelHandler.highWaterMark).isEqualTo(originalHighWaterMark);
ns.shutdown();
}
Aggregations