Search in sources :

Example 1 with WritableBuffer

use of io.grpc.internal.WritableBuffer in project grpc-java by grpc.

the class OkHttpClientStream method sendFrame.

@Override
protected void sendFrame(WritableBuffer frame, boolean endOfStream, boolean flush) {
    Buffer buffer;
    if (frame == null) {
        buffer = EMPTY_BUFFER;
    } else {
        buffer = ((OkHttpWritableBuffer) frame).buffer();
        int size = (int) buffer.size();
        if (size > 0) {
            onSendingBytes(size);
        }
    }
    synchronized (lock) {
        if (cancelSent) {
            return;
        }
        if (pendingData != null) {
            // Stream is pending start, queue the data.
            pendingData.add(new PendingData(buffer, endOfStream, flush));
        } else {
            checkState(id() != ABSENT_ID, "streamId should be set");
            // If buffer > frameWriter.maxDataLength() the flow-controller will ensure that it is
            // properly chunked.
            outboundFlow.data(endOfStream, id(), buffer, flush);
        }
    }
}
Also used : Buffer(okio.Buffer) WritableBuffer(io.grpc.internal.WritableBuffer)

Example 2 with WritableBuffer

use of io.grpc.internal.WritableBuffer in project grpc-java by grpc.

the class CronetClientStreamTest method write.

@Test
public void write() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    // Create 5 frames to send.
    CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator();
    String[] requests = new String[5];
    WritableBuffer[] buffers = new WritableBuffer[5];
    for (int i = 0; i < 5; ++i) {
        requests[i] = new String("request" + String.valueOf(i));
        buffers[i] = allocator.allocate(requests[i].length());
        buffers[i].write(requests[i].getBytes(Charset.forName("UTF-8")), 0, requests[i].length());
        // The 3rd and 5th writeFrame calls have flush=true.
        clientStream.abstractClientStreamSink().writeFrame(buffers[i], false, i == 2 || i == 4, 1);
    }
    // BidirectionalStream.write is not called because stream is not ready yet.
    verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
    // Stream is ready.
    callback.onStreamReady(cronetStream);
    // 5 writes are called.
    verify(cronetStream, times(5)).write(isA(ByteBuffer.class), eq(false));
    ByteBuffer fakeBuffer = ByteBuffer.allocateDirect(8);
    ((Buffer) fakeBuffer).position(8);
    verify(cronetStream, times(2)).flush();
    // 5 onWriteCompleted callbacks for previous writes.
    callback.onWriteCompleted(cronetStream, null, fakeBuffer, false);
    callback.onWriteCompleted(cronetStream, null, fakeBuffer, false);
    callback.onWriteCompleted(cronetStream, null, fakeBuffer, false);
    callback.onWriteCompleted(cronetStream, null, fakeBuffer, false);
    callback.onWriteCompleted(cronetStream, null, fakeBuffer, false);
    // All pending data has been sent. onWriteCompleted callback will not trigger any additional
    // write call.
    verify(cronetStream, times(5)).write(isA(ByteBuffer.class), eq(false));
    // Send end of stream. write will be immediately called since stream is ready.
    clientStream.abstractClientStreamSink().writeFrame(null, true, true, 1);
    verify(cronetStream, times(1)).write(isA(ByteBuffer.class), eq(true));
    verify(cronetStream, times(3)).flush();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) WritableBuffer(io.grpc.internal.WritableBuffer) ByteBuffer(java.nio.ByteBuffer) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) WritableBuffer(io.grpc.internal.WritableBuffer) Executor(java.util.concurrent.Executor) Test(org.junit.Test)

Example 3 with WritableBuffer

use of io.grpc.internal.WritableBuffer in project grpc-java by grpc.

the class CronetWritableBufferAllocatorTest method testAllocateLargeBuffer.

@Test
public void testAllocateLargeBuffer() throws Exception {
    CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator();
    // Ask for 1GB
    WritableBuffer buffer = allocator.allocate(1024 * 1024 * 1024);
    // Only get 1MB
    assertEquals(1024 * 1024, buffer.writableBytes());
}
Also used : WritableBuffer(io.grpc.internal.WritableBuffer) Test(org.junit.Test)

Example 4 with WritableBuffer

use of io.grpc.internal.WritableBuffer in project grpc-java by grpc.

the class OkHttpWritableBufferAllocatorTest method testInitialCapacityHasMaximum.

@Test
public void testInitialCapacityHasMaximum() {
    WritableBuffer buffer = allocator().allocate(1024 * 1025);
    assertEquals(0, buffer.readableBytes());
    assertEquals(1024 * 1024, buffer.writableBytes());
}
Also used : WritableBuffer(io.grpc.internal.WritableBuffer) Test(org.junit.Test)

Example 5 with WritableBuffer

use of io.grpc.internal.WritableBuffer in project grpc-java by grpc.

the class NettyWritableBufferAllocatorTest method testCapacityIsCappedAtMaximum.

@Test
public void testCapacityIsCappedAtMaximum() {
    // Current max is 1MB
    WritableBuffer buffer = allocator().allocate(1024 * 1025);
    assertEquals(0, buffer.readableBytes());
    assertEquals(1024 * 1024, buffer.writableBytes());
}
Also used : WritableBuffer(io.grpc.internal.WritableBuffer) Test(org.junit.Test)

Aggregations

WritableBuffer (io.grpc.internal.WritableBuffer)12 Test (org.junit.Test)10 Buffer (java.nio.Buffer)2 ByteBuffer (java.nio.ByteBuffer)2 Executor (java.util.concurrent.Executor)2 BidirectionalStream (org.chromium.net.BidirectionalStream)2 ExperimentalBidirectionalStream (org.chromium.net.ExperimentalBidirectionalStream)2 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 RpcProgress (io.grpc.internal.ClientStreamListener.RpcProgress)1 MessageFramer (io.grpc.internal.MessageFramer)1 MessageProducer (io.grpc.internal.StreamListener.MessageProducer)1 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DefaultHttp2FrameWriter (io.netty.handler.codec.http2.DefaultHttp2FrameWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Buffer (okio.Buffer)1 UrlResponseInfo (org.chromium.net.UrlResponseInfo)1 UrlResponseInfoImpl (org.chromium.net.impl.UrlResponseInfoImpl)1