Search in sources :

Example 1 with TestClientStreamTracer

use of io.grpc.internal.testing.TestClientStreamTracer in project grpc-java by grpc.

the class AbstractClientStreamTest method writeMessage_closesStream.

@Test
public void writeMessage_closesStream() throws IOException {
    AbstractClientStream.Sink sink = mock(AbstractClientStream.Sink.class);
    final TestClientStreamTracer tracer = new TestClientStreamTracer();
    StatsTraceContext statsTraceCtx = new StatsTraceContext(new StreamTracer[] { tracer });
    AbstractClientStream stream = new BaseAbstractClientStream(allocator, new BaseTransportState(statsTraceCtx, transportTracer), sink, statsTraceCtx, transportTracer, true);
    stream.start(mockListener);
    InputStream input = mock(InputStream.class, delegatesTo(new ByteArrayInputStream(new byte[1])));
    stream.writeMessage(input);
    verify(input).close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TestClientStreamTracer(io.grpc.internal.testing.TestClientStreamTracer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with TestClientStreamTracer

use of io.grpc.internal.testing.TestClientStreamTracer in project grpc-java by grpc.

the class AbstractClientStreamTest method getRequest.

@Test
public void getRequest() {
    AbstractClientStream.Sink sink = mock(AbstractClientStream.Sink.class);
    final TestClientStreamTracer tracer = new TestClientStreamTracer();
    StatsTraceContext statsTraceCtx = new StatsTraceContext(new StreamTracer[] { tracer });
    AbstractClientStream stream = new BaseAbstractClientStream(allocator, new BaseTransportState(statsTraceCtx, transportTracer), sink, statsTraceCtx, transportTracer, true);
    stream.start(mockListener);
    stream.writeMessage(new ByteArrayInputStream(new byte[1]));
    // writeHeaders will be delayed since we're sending a GET request.
    verify(sink, never()).writeHeaders(any(Metadata.class), any(byte[].class));
    // halfClose will trigger writeHeaders.
    stream.halfClose();
    ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(sink).writeHeaders(any(Metadata.class), payloadCaptor.capture());
    assertTrue(payloadCaptor.getValue() != null);
    // GET requests don't have BODY.
    verify(sink, never()).writeFrame(any(WritableBuffer.class), anyBoolean(), anyBoolean(), anyInt());
    assertThat(tracer.nextOutboundEvent()).isEqualTo("outboundMessage(0)");
    assertThat(tracer.nextOutboundEvent()).matches("outboundMessageSent\\(0, [0-9]+, [0-9]+\\)");
    assertNull(tracer.nextOutboundEvent());
    assertNull(tracer.nextInboundEvent());
    assertEquals(1, tracer.getOutboundWireSize());
    assertEquals(1, tracer.getOutboundUncompressedSize());
}
Also used : ByteWritableBuffer(io.grpc.internal.MessageFramerTest.ByteWritableBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) TestClientStreamTracer(io.grpc.internal.testing.TestClientStreamTracer) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with TestClientStreamTracer

use of io.grpc.internal.testing.TestClientStreamTracer in project grpc-java by grpc.

the class AbstractInteropTest method assertClientStatsTrace.

private void assertClientStatsTrace(String method, Status.Code code, Collection<? extends MessageLite> requests, Collection<? extends MessageLite> responses) {
    // Tracer-based stats
    TestClientStreamTracer tracer = clientStreamTracers.poll();
    assertNotNull(tracer);
    assertTrue(tracer.getOutboundHeaders());
    // but streamClosed() may be called slightly later than that.  So we need a timeout.
    try {
        assertTrue(tracer.await(5, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    }
    assertEquals(code, tracer.getStatus().getCode());
    if (requests != null && responses != null) {
        checkTracers(tracer, requests, responses);
    }
    if (metricsExpected()) {
        // CensusStreamTracerModule records final status in interceptor, which is guaranteed to be
        // done before application receives status.
        MetricsRecord clientStartRecord = clientStatsRecorder.pollRecord();
        checkStartTags(clientStartRecord, method, true);
        MetricsRecord clientEndRecord = clientStatsRecorder.pollRecord();
        checkEndTags(clientEndRecord, method, code, true);
        if (requests != null && responses != null) {
            checkCensus(clientEndRecord, false, requests, responses);
        }
        assertZeroRetryRecorded();
    }
}
Also used : TestClientStreamTracer(io.grpc.internal.testing.TestClientStreamTracer) MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord)

Aggregations

TestClientStreamTracer (io.grpc.internal.testing.TestClientStreamTracer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Test (org.junit.Test)2 Metadata (io.grpc.Metadata)1 ByteWritableBuffer (io.grpc.internal.MessageFramerTest.ByteWritableBuffer)1 MetricsRecord (io.grpc.internal.testing.StatsTestUtils.MetricsRecord)1 InputStream (java.io.InputStream)1