Search in sources :

Example 76 with ClientConnection

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

the class AsyncSegmentInputStreamTest method testRetry.

@Test(timeout = 10000)
public void testRetry() throws ConnectionFailedException {
    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);
    DelegationTokenProvider tokenProvider = mock(DelegationTokenProvider.class);
    // return empty token
    when(tokenProvider.retrieveToken()).thenReturn(CompletableFuture.completedFuture(""));
    Semaphore dataAvailable = new Semaphore(0);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, tokenProvider, dataAvailable);
    ClientConnection c = mock(ClientConnection.class);
    InOrder inOrder = Mockito.inOrder(c);
    connectionFactory.provideConnection(endpoint, c);
    WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            connectionFactory.getProcessor(endpoint).connectionDropped();
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            connectionFactory.getProcessor(endpoint).authTokenCheckFailed(new WireCommands.AuthTokenCheckFailed(in.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            connectionFactory.getProcessor(endpoint).process(segmentRead);
            return null;
        }
    }).when(c).send(any(ReadSegment.class));
    assertEquals(0, dataAvailable.availablePermits());
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    assertEquals(segmentRead, readFuture.join());
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(1, dataAvailable.availablePermits());
    inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    inOrder.verify(c).close();
    inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    inOrder.verify(c).close();
    inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
    verifyNoMoreInteractions(c);
    // ensure retrieve Token is invoked for every retry.
    verify(tokenProvider, times(3)).retrieveToken();
}
Also used : InOrder(org.mockito.InOrder) 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) Answer(org.mockito.stubbing.Answer) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Test(org.junit.Test)

Example 77 with ClientConnection

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

the class SegmentMetadataClientTest method testTruncate.

@Test(timeout = 10000)
public void testTruncate() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testTruncate", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
    @Cleanup ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(endpoint, connection);
    @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    client.getConnection();
    ReplyProcessor processor = cf.getProcessor(endpoint);
    AtomicLong requestId = new AtomicLong();
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.TruncateSegment truncateSegment = invocation.getArgument(0);
            processor.process(new SegmentTruncated(truncateSegment.getRequestId(), segment.getScopedName()));
            requestId.set(truncateSegment.getRequestId());
            return null;
        }
    }).when(connection).send(any(WireCommands.TruncateSegment.class));
    client.truncateSegment(123L).join();
    Mockito.verify(connection).send(Mockito.eq(new WireCommands.TruncateSegment(requestId.get(), segment.getScopedName(), 123L, "")));
}
Also used : SegmentTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentTruncated) Cleanup(lombok.Cleanup) AtomicLong(java.util.concurrent.atomic.AtomicLong) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 78 with ClientConnection

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

the class SegmentMetadataClientTest method testReconnects.

@Test(timeout = 10000)
public void testReconnects() throws Exception {
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl cf = Mockito.spy(new MockConnectionFactoryImpl());
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
    @Cleanup ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(endpoint, connection);
    @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    client.getConnection();
    final List<Long> requestIds = new ArrayList<>();
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
            requestIds.add(request.getRequestId());
            if (requestIds.size() == 1) {
                ReplyProcessor processor = cf.getProcessor(endpoint);
                processor.connectionDropped();
            } else {
                ReplyProcessor processor = cf.getProcessor(endpoint);
                processor.process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
            }
            return null;
        }
    }).when(connection).send(any(WireCommands.GetStreamSegmentInfo.class));
    long length = client.fetchCurrentSegmentLength().join();
    InOrder order = Mockito.inOrder(connection, cf);
    order.verify(cf).establishConnection(eq(endpoint), any(ReplyProcessor.class));
    order.verify(connection).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(0), segment.getScopedName(), "")));
    order.verify(cf).establishConnection(eq(endpoint), any(ReplyProcessor.class));
    order.verify(connection).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(1), segment.getScopedName(), "")));
    order.verify(cf).getProcessor(eq(endpoint));
    order.verifyNoMoreInteractions();
    assertEquals(123, length);
}
Also used : StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 79 with ClientConnection

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

the class SegmentMetadataClientTest method testGetProperty.

@Test(timeout = 10000)
public void testGetProperty() throws Exception {
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
    @Cleanup ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(endpoint, connection);
    @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    client.getConnection();
    ReplyProcessor processor = cf.getProcessor(endpoint);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetSegmentAttribute request = invocation.getArgument(0);
            processor.process(new WireCommands.SegmentAttribute(request.getRequestId(), 123));
            return null;
        }
    }).when(connection).send(any(WireCommands.GetSegmentAttribute.class));
    long value = client.fetchProperty(SegmentAttribute.RevisionStreamClientMark).join();
    assertEquals(123, value);
}
Also used : Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 80 with ClientConnection

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

the class SegmentMetadataClientTest method testExceptionOnSend.

@Test(timeout = 10000)
public void testExceptionOnSend() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor();
    @Cleanup ConnectionPool cf = Mockito.mock(ConnectionPool.class);
    Mockito.when(cf.getInternalExecutor()).thenReturn(executor);
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
    ClientConnection connection1 = mock(ClientConnection.class);
    ClientConnection connection2 = mock(ClientConnection.class);
    AtomicReference<ReplyProcessor> processor = new AtomicReference<>();
    Mockito.doAnswer(invocation -> {
        final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
        future.completeExceptionally(new ConnectionFailedException(new RuntimeException("Mock error")));
        return null;
    }).doAnswer(invocation -> {
        final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
        future.complete(connection1);
        return null;
    }).doAnswer(invocation -> {
        final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
        processor.set(invocation.getArgument(2));
        future.complete(connection2);
        return null;
    }).when(cf).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(ReplyProcessor.class), Mockito.<CompletableFuture<ClientConnection>>any());
    final List<Long> requestIds = new ArrayList<>();
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
            requestIds.add(request.getRequestId());
            if (requestIds.size() == 1) {
                throw new ConnectionFailedException();
            } else {
                processor.get().process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
            }
            return null;
        }
    }).when(connection1).send(any(WireCommands.GetStreamSegmentInfo.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
            requestIds.add(request.getRequestId());
            processor.get().process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
            return null;
        }
    }).when(connection2).send(any(WireCommands.GetStreamSegmentInfo.class));
    @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    InOrder order = Mockito.inOrder(connection1, connection2, cf);
    long length = client.fetchCurrentSegmentLength().join();
    order.verify(cf, Mockito.times(2)).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(), Mockito.<CompletableFuture<ClientConnection>>any());
    order.verify(connection1).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(0), segment.getScopedName(), "")));
    order.verify(connection1).close();
    order.verify(cf).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(), Mockito.<CompletableFuture<ClientConnection>>any());
    order.verify(connection2).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(1), segment.getScopedName(), "")));
    order.verifyNoMoreInteractions();
    assertEquals(123, length);
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AssertExtensions(io.pravega.test.common.AssertExtensions) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) SegmentIsTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentIsTruncated) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockController(io.pravega.client.stream.mock.MockController) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(io.pravega.client.connection.impl.ClientConnection) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Flow(io.pravega.client.connection.impl.Flow) StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) InOrder(org.mockito.InOrder) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WireCommands(io.pravega.shared.protocol.netty.WireCommands) SegmentAttributeUpdated(io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) InvalidTokenException(io.pravega.auth.InvalidTokenException) SegmentTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentTruncated) InlineExecutor(io.pravega.test.common.InlineExecutor) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InlineExecutor(io.pravega.test.common.InlineExecutor) ClientConnection(io.pravega.client.connection.impl.ClientConnection) InOrder(org.mockito.InOrder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Flow(io.pravega.client.connection.impl.Flow) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) 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