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