Search in sources :

Example 11 with ClientConnection

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

the class SegmentMetadataClientTest method testTokenCheckFailure.

@Test(timeout = 10000)
public void testTokenCheckFailure() throws ConnectionFailedException {
    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.GetStreamSegmentInfo getStreamInfo = invocation.getArgument(0);
            processor.process(new WireCommands.AuthTokenCheckFailed(getStreamInfo.getRequestId(), "server-stacktrace", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED));
            return null;
        }
    }).when(connection).send(any(WireCommands.GetStreamSegmentInfo.class));
    AssertExtensions.assertThrows("TokenException was not thrown or server stacktrace contained unexpected content.", () -> client.fetchCurrentSegmentLength().join(), e -> e instanceof InvalidTokenException && e.getMessage().contains("serverStackTrace=server-stacktrace"));
}
Also used : InvalidTokenException(io.pravega.auth.InvalidTokenException) 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 12 with ClientConnection

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

the class SegmentMetadataClientTest method testTruncateNoSuchSegmentError.

@Test(timeout = 10000)
public void testTruncateNoSuchSegmentError() 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 WireCommands.NoSuchSegment(truncateSegment.getRequestId(), segment.getScopedName(), "", 123L));
            requestId.set(truncateSegment.getRequestId());
            return null;
        }
    }).when(connection).send(any(WireCommands.TruncateSegment.class));
    AssertExtensions.assertThrows(NoSuchSegmentException.class, () -> client.truncateSegment(123L).join());
    Mockito.verify(connection).send(Mockito.eq(new WireCommands.TruncateSegment(requestId.get(), segment.getScopedName(), 123L, "")));
}
Also used : 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 13 with ClientConnection

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

the class SegmentMetadataClientTest method testSeal.

@Test(timeout = 10000)
public void testSeal() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testSeal", 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.SealSegment sealSegment = invocation.getArgument(0);
            processor.process(new WireCommands.SegmentSealed(sealSegment.getRequestId(), segment.getScopedName()));
            requestId.set(sealSegment.getRequestId());
            return null;
        }
    }).when(connection).send(any(WireCommands.SealSegment.class));
    client.sealSegment().join();
    Mockito.verify(connection).send(Mockito.eq(new WireCommands.SealSegment(requestId.get(), segment.getScopedName(), "")));
}
Also used : 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 14 with ClientConnection

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

the class SegmentMetadataClientTest method testCurrentStreamLength.

@Test(timeout = 10000)
public void testCurrentStreamLength() throws ConnectionFailedException {
    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.GetStreamSegmentInfo getStreamInfo = invocation.getArgument(0);
            processor.process(new StreamSegmentInfo(getStreamInfo.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
            return null;
        }
    }).when(connection).send(any(WireCommands.GetStreamSegmentInfo.class));
    long head = client.fetchCurrentSegmentHeadOffset().join();
    long length = client.fetchCurrentSegmentLength().join();
    assertEquals(121, head);
    assertEquals(123, length);
}
Also used : StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) 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 15 with ClientConnection

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

the class SegmentOutputStreamTest method testConnectWithMultipleFailures.

@Test(timeout = 10000)
public void testConnectWithMultipleFailures() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    RetryWithBackoff retryConfig = Retry.withExpBackoff(1, 1, 4);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    InOrder verify = inOrder(connection);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, retryConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    try {
        output.reconnect();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        // simulate a processing Failure and ensure SetupAppend is executed.
        ReplyProcessor processor = cf.getProcessor(uri);
        processor.processingFailure(new IOException());
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        verifyNoMoreInteractions(connection);
        processor.connectionDropped();
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        processor.wrongHost(new WireCommands.WrongHost(output.getRequestId(), SEGMENT, "newHost", "SomeException"));
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        verifyNoMoreInteractions(connection);
        processor.processingFailure(new IOException());
        assertTrue("Connection is  exceptionally closed with RetriesExhaustedException", output.getConnection().isCompletedExceptionally());
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(output.getConnection()));
        verify.verify(connection).close();
        verifyNoMoreInteractions(connection);
    } finally {
        // Verify that a close on the SegmentOutputStream does throw a RetriesExhaustedException.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, output::close);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) IOException(java.io.IOException) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) UUID(java.util.UUID) 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