use of io.pravega.client.stream.mock.MockController 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.MockController in project pravega by pravega.
the class ReadTest method readThroughSegmentClient.
@Test
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
String endpoint = "localhost";
String scope = "scope";
String stream = "stream";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
server.startListening();
ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
Controller controller = new MockController(endpoint, port, clientCF);
controller.createScope(scope);
controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup("close") SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
out.flush();
@Cleanup("close") SegmentInputStream in = segmentConsumerClient.createInputStreamForSegment(segment);
ByteBuffer result = in.read();
assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
// Test large write followed by read
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>()));
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 150000);
}
use of io.pravega.client.stream.mock.MockController in project pravega by pravega.
the class AppendTest method appendThroughSegmentClient.
@Test
public void appendThroughSegmentClient() throws Exception {
String endpoint = "localhost";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
String scope = "scope";
String stream = "stream";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
server.startListening();
@Cleanup ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
Controller controller = new MockController(endpoint, port, clientCF);
controller.createScope(scope);
controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
SegmentOutputStreamFactoryImpl segmentClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup SegmentOutputStream out = segmentClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
CompletableFuture<Boolean> ack = new CompletableFuture<>();
out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), ack));
assertTrue(ack.get(5, TimeUnit.SECONDS));
}
use of io.pravega.client.stream.mock.MockController 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.MockController 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());
}
Aggregations