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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations