Search in sources :

Example 21 with MockConnectionFactoryImpl

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

the class ReaderGroupStateManagerTest method testSegmentMerge.

@Test(timeout = 20000)
public void testSegmentMerge() throws ReinitializationRequiredException {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    Segment initialSegmentA = new Segment(scope, stream, 0);
    Segment initialSegmentB = new Segment(scope, stream, 1);
    Segment successor = new Segment(scope, stream, 2);
    MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(Collections.singletonMap(new SegmentWithRange(successor, 0.0, 1.0), ImmutableList.of(0, 1)), ""));
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
    Map<Segment, Long> segments = new HashMap<>();
    segments.put(initialSegmentA, 1L);
    segments.put(initialSegmentB, 2L);
    ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
    val readerState = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, null);
    readerState.initializeReader(0);
    Map<Segment, Long> newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertEquals(2, newSegments.size());
    assertEquals(Long.valueOf(1), newSegments.get(initialSegmentA));
    assertEquals(Long.valueOf(2), newSegments.get(initialSegmentB));
    readerState.handleEndOfSegment(initialSegmentA);
    newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertTrue(newSegments.isEmpty());
    readerState.handleEndOfSegment(initialSegmentB);
    newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertEquals(1, newSegments.size());
    assertEquals(Long.valueOf(0), newSegments.get(successor));
    newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertTrue(newSegments.isEmpty());
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 22 with MockConnectionFactoryImpl

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

the class BatchClientImplTest method testSegmentIterator.

@Test(timeout = 5000)
public void testSegmentIterator() throws ConnectionFailedException {
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CreateSegment request = (CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            GetStreamSegmentInfo request = (GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(GetStreamSegmentInfo.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory);
    BatchClientImpl client = new BatchClientImpl(mockController, connectionFactory);
    Stream stream = new StreamImpl("scope", "stream");
    mockController.createScope("scope");
    mockController.createStream(StreamConfiguration.builder().scope("scope").streamName("stream").scalingPolicy(ScalingPolicy.fixed(3)).build()).join();
    Iterator<SegmentRange> segments = client.getSegments(stream, null, null).getIterator();
    assertTrue(segments.hasNext());
    assertEquals(0, segments.next().asImpl().getSegment().getSegmentNumber());
    assertTrue(segments.hasNext());
    assertEquals(1, segments.next().asImpl().getSegment().getSegmentNumber());
    assertTrue(segments.hasNext());
    assertEquals(2, segments.next().asImpl().getSegment().getSegmentNumber());
    assertFalse(segments.hasNext());
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) GetStreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.GetStreamSegmentInfo) StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) GetStreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.GetStreamSegmentInfo) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamImpl(io.pravega.client.stream.impl.StreamImpl) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) Stream(io.pravega.client.stream.Stream) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Test(org.junit.Test)

Example 23 with MockConnectionFactoryImpl

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

the class RawClientTest method testRequestReply.

@Test
public void testRequestReply() throws ConnectionFailedException, InterruptedException, ExecutionException {
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", -1);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, connection);
    @Cleanup RawClient rawClient = new RawClient(controller, connectionFactory, new Segment("scope", "testHello", 0));
    UUID id = UUID.randomUUID();
    ConditionalAppend request = new ConditionalAppend(id, 1, 0, Unpooled.EMPTY_BUFFER);
    CompletableFuture<Reply> future = rawClient.sendRequest(1, request);
    Mockito.verify(connection).send(request);
    assertFalse(future.isDone());
    ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
    DataAppended reply = new DataAppended(id, 1, 0);
    processor.process(reply);
    assertTrue(future.isDone());
    assertEquals(reply, future.get());
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Reply(io.pravega.shared.protocol.netty.Reply) UUID(java.util.UUID) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 24 with MockConnectionFactoryImpl

use of io.pravega.client.stream.mock.MockConnectionFactoryImpl 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 25 with MockConnectionFactoryImpl

use of io.pravega.client.stream.mock.MockConnectionFactoryImpl 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)

Aggregations

MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)47 MockController (io.pravega.client.stream.mock.MockController)47 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)47 Test (org.junit.Test)46 Cleanup (lombok.Cleanup)35 ClientConnection (io.pravega.client.netty.impl.ClientConnection)29 UUID (java.util.UUID)22 WireCommands (io.pravega.shared.protocol.netty.WireCommands)19 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)19 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)16 ClientFactory (io.pravega.client.ClientFactory)15 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)15 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)15 PendingEvent (io.pravega.client.stream.impl.PendingEvent)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 Segment (io.pravega.client.segment.impl.Segment)13 InOrder (org.mockito.InOrder)13 Append (io.pravega.shared.protocol.netty.Append)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12