Search in sources :

Example 26 with MockController

use of io.pravega.client.stream.mock.MockController 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);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.allocate(0));
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    Async.testBlocking(() -> readFuture.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.segmentRead(segmentRead);
    });
    verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(segmentRead, readFuture.join());
    verifyNoMoreInteractions(c);
}
Also used : 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) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 27 with MockController

use of io.pravega.client.stream.mock.MockController 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);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
    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, ByteBuffer.allocate(0));
    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(100));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            connectionFactory.getProcessor(endpoint).segmentRead(segmentRead);
            return null;
        }
    }).when(c).sendAsync(Mockito.any(ReadSegment.class));
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    assertEquals(segmentRead, readFuture.join());
    assertTrue(Futures.isSuccessful(readFuture));
    inOrder.verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    inOrder.verify(c).close();
    inOrder.verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    inOrder.verify(c).close();
    inOrder.verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    verifyNoMoreInteractions(c);
}
Also used : InOrder(org.mockito.InOrder) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) 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) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 28 with MockController

use of io.pravega.client.stream.mock.MockController 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);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
    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();
    AssertExtensions.assertThrows(ConnectionClosedException.class, () -> Futures.getThrowingException(read));
    verify(c).close();
}
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.netty.impl.ClientConnection) Cleanup(lombok.Cleanup) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Test(org.junit.Test)

Example 29 with MockController

use of io.pravega.client.stream.mock.MockController 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);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    Async.testBlocking(() -> readFuture.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.segmentRead(new WireCommands.SegmentRead(segment.getScopedName(), 1235, false, false, ByteBuffer.wrap(bad)));
        processor.segmentRead(new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.wrap(good)));
    });
    verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(ByteBuffer.wrap(good), readFuture.join().getData());
    verifyNoMoreInteractions(c);
}
Also used : 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) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 30 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentMetadataClientTest method testTruncate.

@Test(timeout = 10000)
public void testTruncate() throws Exception {
    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);
    @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 {
            processor.process(new SegmentTruncated(1, segment.getScopedName()));
            return null;
        }
    }).when(connection).send(new WireCommands.TruncateSegment(1, segment.getScopedName(), 123L, ""));
    client.truncateSegment(segment, 123L);
    Mockito.verify(connection).send(new WireCommands.TruncateSegment(1, segment.getScopedName(), 123L, ""));
}
Also used : SegmentTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentTruncated) 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.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Aggregations

MockController (io.pravega.client.stream.mock.MockController)50 Test (org.junit.Test)49 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)48 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)47 Cleanup (lombok.Cleanup)38 ClientConnection (io.pravega.client.netty.impl.ClientConnection)30 UUID (java.util.UUID)22 WireCommands (io.pravega.shared.protocol.netty.WireCommands)20 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)19 CompletableFuture (java.util.concurrent.CompletableFuture)17 PendingEvent (io.pravega.client.stream.impl.PendingEvent)16 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)16 ClientFactory (io.pravega.client.ClientFactory)15 Segment (io.pravega.client.segment.impl.Segment)15 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)15 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)15 InOrder (org.mockito.InOrder)14 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 Append (io.pravega.shared.protocol.netty.Append)12 ByteBuffer (java.nio.ByteBuffer)12