Search in sources :

Example 71 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class AsyncSegmentInputStreamTest method testCloseAbortsRead.

@Test(timeout = 10000)
public void testCloseAbortsRead() throws InterruptedException, ExecutionException {
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    Semaphore dataAvailable = new Semaphore(0);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    // Make sure connection is established.
    in.getConnection().get();
    CompletableFuture<SegmentRead> read = in.read(1234, 5678);
    assertFalse(read.isDone());
    in.close();
    assertThrows(ConnectionClosedException.class, () -> Futures.getThrowingException(read));
    verify(c).close();
    assertEquals(0, dataAvailable.availablePermits());
}
Also used : PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Test(org.junit.Test)

Example 72 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class AsyncSegmentInputStreamTest method testRead.

@Test(timeout = 10000)
public void testRead() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testRead", 1);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    Semaphore dataAvailable = new Semaphore(0);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    assertEquals(0, dataAvailable.availablePermits());
    AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.process(segmentRead);
    });
    assertEquals(1, dataAvailable.availablePermits());
    verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(segmentRead, readFuture.join());
    verifyNoMoreInteractions(c);
}
Also used : ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 73 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class AsyncSegmentInputStreamTest method testWrongOffsetReturned.

@Test(timeout = 10000)
public void testWrongOffsetReturned() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testWrongOffsetReturned", 0);
    byte[] good = new byte[] { 0, 1, 2, 3, 4 };
    byte[] bad = new byte[] { 9, 8, 7, 6 };
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    Semaphore dataAvailable = new Semaphore(0);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    assertEquals(0, dataAvailable.availablePermits());
    AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1235, false, false, Unpooled.wrappedBuffer(bad), in.getRequestId()));
        processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.wrappedBuffer(good), in.getRequestId()));
    });
    assertEquals(2, dataAvailable.availablePermits());
    verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(Unpooled.wrappedBuffer(good), readFuture.join().getData());
    verifyNoMoreInteractions(c);
}
Also used : ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 74 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class AsyncSegmentInputStreamTest method testRecvErrorMessage.

@Test
public void testRecvErrorMessage() throws ExecutionException, InterruptedException {
    int requestId = 0;
    Segment segment = new Segment("scope", "testWrongOffsetReturned", requestId);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    Semaphore dataAvailable = new Semaphore(requestId);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    in.getConnection().get();
    ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
    WireCommands.ErrorMessage reply = new WireCommands.ErrorMessage(requestId, segment.getScopedName(), "error.", WireCommands.ErrorMessage.ErrorCode.ILLEGAL_ARGUMENT_EXCEPTION);
    processor.process(reply);
    verify(c).close();
}
Also used : Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 75 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class AsyncSegmentInputStreamTest method testSegmentTruncated.

@Test(timeout = 10000)
public void testSegmentTruncated() throws ConnectionFailedException {
    String mockClientReplyStackTrace = "SomeException";
    Segment segment = new Segment("scope", "testRead", 1);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    Semaphore dataAvailable = new Semaphore(0);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    // segment truncated response from Segment store.
    WireCommands.SegmentIsTruncated segmentIsTruncated = new WireCommands.SegmentIsTruncated(in.getRequestId(), segment.getScopedName(), 1234, mockClientReplyStackTrace, 1234L);
    // Trigger read.
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    assertEquals(0, dataAvailable.availablePermits());
    // verify that a response from Segment store completes the readFuture and the future completes with SegmentTruncatedException.
    AssertExtensions.assertBlocks(() -> assertThrows(SegmentTruncatedException.class, () -> readFuture.get()), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.process(segmentIsTruncated);
    });
    verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    // verify read future completedExceptionally
    assertTrue(!Futures.isSuccessful(readFuture));
    assertThrows(SegmentTruncatedException.class, () -> readFuture.get());
    verifyNoMoreInteractions(c);
    // Ensure that reads at a different offset can still happen on the same instance.
    WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 5656, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
    CompletableFuture<SegmentRead> readFuture2 = in.read(5656, 5678);
    AssertExtensions.assertBlocks(() -> readFuture2.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.segmentRead(segmentRead);
    });
    verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 5656, 5678, "", in.getRequestId())));
    assertTrue(Futures.isSuccessful(readFuture2));
    assertEquals(segmentRead, readFuture2.join());
    verifyNoMoreInteractions(c);
    assertEquals(1, dataAvailable.availablePermits());
}
Also used : ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Aggregations

ClientConnection (io.pravega.client.connection.impl.ClientConnection)87 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)81 MockController (io.pravega.client.stream.mock.MockController)79 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)78 Test (org.junit.Test)77 Cleanup (lombok.Cleanup)51 InvocationOnMock (org.mockito.invocation.InvocationOnMock)43 WireCommands (io.pravega.shared.protocol.netty.WireCommands)40 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)35 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)34 UUID (java.util.UUID)34 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)33 ByteBuffer (java.nio.ByteBuffer)31 CompletableFuture (java.util.concurrent.CompletableFuture)30 InOrder (org.mockito.InOrder)27 Append (io.pravega.shared.protocol.netty.Append)23 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)23 Answer (org.mockito.stubbing.Answer)17 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)16 ArrayList (java.util.ArrayList)14