use of io.netty.buffer.ByteBuf in project netty by netty.
the class LoggingHandlerTest method shouldLogByteBufDataRead.
@Test
public void shouldLogByteBufDataRead() throws Exception {
ByteBuf msg = Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+RECEIVED: " + msg.readableBytes() + "B$")));
ByteBuf handledMsg = channel.readInbound();
assertThat(msg, is(sameInstance(handledMsg)));
handledMsg.release();
assertThat(channel.readInbound(), is(nullValue()));
}
use of io.netty.buffer.ByteBuf in project grpc-java by grpc.
the class AbstractBenchmark method startUnaryCalls.
/**
* Start a continuously executing set of unary calls that will terminate when
* {@code done.get()} is true. Each completed call will increment the counter by the specified
* delta which benchmarks can use to measure QPS or bandwidth.
*/
protected void startUnaryCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean done, final long counterDelta) {
for (final ManagedChannel channel : channels) {
for (int i = 0; i < callsPerChannel; i++) {
StreamObserver<ByteBuf> observer = new StreamObserver<ByteBuf>() {
@Override
public void onNext(ByteBuf value) {
counter.addAndGet(counterDelta);
}
@Override
public void onError(Throwable t) {
done.set(true);
}
@Override
public void onCompleted() {
if (!done.get()) {
ByteBuf slice = request.slice();
ClientCalls.asyncUnaryCall(channel.newCall(unaryMethod, CALL_OPTIONS), slice, this);
}
}
};
observer.onCompleted();
}
}
}
use of io.netty.buffer.ByteBuf in project grpc-java by grpc.
the class AbstractBenchmark method startFlowControlledStreamingCalls.
/**
* Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
* {@code done.get()} is true. Each completed call will increment the counter by the specified
* delta which benchmarks can use to measure messages per second or bandwidth.
*/
protected CountDownLatch startFlowControlledStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
for (final ManagedChannel channel : channels) {
for (int i = 0; i < callsPerChannel; i++) {
final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(flowControlledStreaming, CALL_OPTIONS);
final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<StreamObserver<ByteBuf>>();
final AtomicBoolean ignoreMessages = new AtomicBoolean();
StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {
@Override
public void onNext(ByteBuf value) {
StreamObserver<ByteBuf> obs = requestObserverRef.get();
if (done.get()) {
if (!ignoreMessages.getAndSet(true)) {
obs.onCompleted();
}
return;
}
if (record.get()) {
counter.addAndGet(counterDelta);
}
// request is called automatically because the observer implicitly has auto
// inbound flow control
}
@Override
public void onError(Throwable t) {
logger.log(Level.WARNING, "call error", t);
latch.countDown();
}
@Override
public void onCompleted() {
latch.countDown();
}
});
requestObserverRef.set(requestObserver);
// Add some outstanding requests to ensure the server is filling the connection
streamingCall.request(5);
requestObserver.onNext(request.slice());
}
}
return latch;
}
use of io.netty.buffer.ByteBuf in project grpc-java by grpc.
the class NettyClientHandler method sendPingFrame.
/**
* Sends a PING frame. If a ping operation is already outstanding, the callback in the message is
* registered to be called when the existing operation completes, and no new frame is sent.
*/
private void sendPingFrame(ChannelHandlerContext ctx, SendPingCommand msg, ChannelPromise promise) {
// Don't check lifecycleManager.getShutdownStatus() since we want to allow pings after shutdown
// but before termination. After termination, messages will no longer arrive because the
// pipeline clears all handlers on channel close.
PingCallback callback = msg.callback();
Executor executor = msg.executor();
// any outstanding operation
if (ping != null) {
promise.setSuccess();
ping.addCallback(callback, executor);
return;
}
// Use a new promise to prevent calling the callback twice on write failure: here and in
// NettyClientTransport.ping(). It may appear strange, but it will behave the same as if
// ping != null above.
promise.setSuccess();
promise = ctx().newPromise();
// set outstanding operation
long data = USER_PING_PAYLOAD;
ByteBuf buffer = ctx.alloc().buffer(8);
buffer.writeLong(data);
Stopwatch stopwatch = Stopwatch.createStarted(ticker);
ping = new Http2Ping(data, stopwatch);
ping.addCallback(callback, executor);
// and then write the ping
encoder().writePing(ctx, false, buffer, promise);
ctx.flush();
final Http2Ping finalPing = ping;
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
Throwable cause = future.cause();
if (cause instanceof ClosedChannelException) {
cause = lifecycleManager.getShutdownThrowable();
if (cause == null) {
cause = Status.UNKNOWN.withDescription("Ping failed but for unknown reason.").withCause(future.cause()).asException();
}
}
finalPing.failed(cause);
if (ping == finalPing) {
ping = null;
}
}
}
});
}
use of io.netty.buffer.ByteBuf in project grpc-java by grpc.
the class NettyClientHandlerTest method oustandingUserPingShouldNotInteractWithDataPing.
@Test
public void oustandingUserPingShouldNotInteractWithDataPing() throws Exception {
createStream();
handler().setAutoTuneFlowControl(true);
PingCallbackImpl callback = new PingCallbackImpl();
sendPing(callback);
ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
verifyWrite().writePing(eq(ctx()), eq(false), captor.capture(), any(ChannelPromise.class));
ByteBuf payload = captor.getValue();
channelRead(grpcDataFrame(3, false, contentAsArray()));
long pingData = handler().flowControlPing().payload();
ByteBuf buffer = handler().ctx().alloc().buffer(8);
buffer.writeLong(pingData);
channelRead(pingFrame(true, buffer));
assertEquals(1, handler().flowControlPing().getPingReturn());
assertEquals(0, callback.invocationCount);
channelRead(pingFrame(true, payload));
assertEquals(1, handler().flowControlPing().getPingReturn());
assertEquals(1, callback.invocationCount);
}
Aggregations