use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class StreamManagerImplTest method testSealedStream.
@Test(timeout = 10000)
public void testSealedStream() throws ConnectionFailedException {
final String streamName = "stream";
final Stream stream = new StreamImpl(defaultScope, streamName);
// Setup Mocks
ClientConnection connection = mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = spy(new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true));
doReturn(CompletableFuture.completedFuture(true)).when(mockController).sealStream(defaultScope, streamName);
StreamSegments empty = new StreamSegments(new TreeMap<>());
doReturn(CompletableFuture.completedFuture(empty)).when(mockController).getCurrentSegments(defaultScope, streamName);
ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
// Create a StreamManager
@Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
// Create a scope and stream and seal it.
streamManager.createScope(defaultScope);
streamManager.createStream(defaultScope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streamManager.sealStream(defaultScope, streamName);
// Fetch StreamInfo
StreamInfo info = streamManager.getStreamInfo(defaultScope, streamName);
// validate results.
assertEquals(defaultScope, info.getScope());
assertEquals(streamName, info.getStreamName());
assertNotNull(info.getTailStreamCut());
assertEquals(stream, info.getTailStreamCut().asImpl().getStream());
assertEquals(0, info.getTailStreamCut().asImpl().getPositions().size());
assertNotNull(info.getHeadStreamCut());
assertEquals(stream, info.getHeadStreamCut().asImpl().getStream());
assertEquals(3, info.getHeadStreamCut().asImpl().getPositions().size());
assertTrue(info.isSealed());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class StreamManagerImplTest method testListStreamInScope.
@Test(timeout = 10000)
public void testListStreamInScope() throws ConnectionFailedException {
// Setup Mocks
ClientConnection connection = mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true);
ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
@Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
String scope = "scope";
String stream1 = "stream1";
String stream2 = "stream2";
String stream3 = "stream3";
streamManager.createScope(scope);
streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).tag("t1").build());
streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streamManager.createStream(scope, stream3, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
Iterator<Stream> m = streamManager.listStreams(scope);
Set<Stream> streams = new HashSet<>();
assertTrue(m.hasNext());
streams.add(m.next());
assertTrue(m.hasNext());
streams.add(m.next());
assertTrue(m.hasNext());
streams.add(m.next());
assertFalse(m.hasNext());
assertEquals(3, streams.size());
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream3)));
assertEquals(Collections.singleton("t1"), streamManager.getStreamTags(scope, stream1));
assertEquals(Collections.singletonList(Stream.of(scope, stream1)), newArrayList(streamManager.listStreams(scope, "t1")));
streamManager.updateStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
assertEquals(Collections.emptySet(), streamManager.getStreamTags(scope, stream1));
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class BatchClientImplTest method getMockConnectionFactory.
private MockConnectionFactoryImpl getMockConnectionFactory(PravegaNodeUri location) throws ConnectionFailedException {
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
ClientConnection connection = mock(ClientConnection.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);
return connectionFactory;
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class LargeEventWriterTest method testBufferSplitting.
@Test(timeout = 5000)
public void testBufferSplitting() throws NoSuchSegmentException, AuthenticationException, SegmentSealedException, ConnectionFailedException {
Segment segment = Segment.fromScopedName("foo/bar/1");
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = new MockController("localhost", 0, connectionFactory, false);
ClientConnection connection = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, connection);
ArrayList<ByteBuf> written = new ArrayList<>();
answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
ByteBuf data = r.getData();
written.add(data);
return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
});
answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
});
LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
ArrayList<ByteBuffer> buffers = new ArrayList<>();
buffers.add(ByteBuffer.allocate(Serializer.MAX_EVENT_SIZE * 2 + 1));
buffers.add(ByteBuffer.allocate(Serializer.MAX_EVENT_SIZE));
buffers.add(ByteBuffer.allocate(5));
writer.writeLargeEvent(segment, buffers, tokenProvider, EventWriterConfig.builder().enableLargeEvents(true).build());
assertEquals(4, written.size());
assertEquals(Serializer.MAX_EVENT_SIZE, written.get(0).readableBytes());
assertEquals(Serializer.MAX_EVENT_SIZE, written.get(1).readableBytes());
assertEquals(Serializer.MAX_EVENT_SIZE, written.get(2).readableBytes());
assertEquals(6 + WireCommands.TYPE_PLUS_LENGTH_SIZE * 3, written.get(3).readableBytes());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class LargeEventWriterTest method testRetriedErrors.
@Test(timeout = 5000)
public void testRetriedErrors() throws ConnectionFailedException, NoSuchSegmentException, AuthenticationException, SegmentSealedException {
Segment segment = Segment.fromScopedName("foo/bar/1");
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = new MockController("localhost", 0, connectionFactory, false);
ClientConnection connection = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, connection);
EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
ArrayList<ByteBuffer> events = new ArrayList<>();
events.add(ByteBuffer.allocate(1));
AtomicBoolean failed = new AtomicBoolean(false);
AtomicBoolean succeeded = new AtomicBoolean(false);
answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
ByteBuf data = r.getData();
return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
});
answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
});
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
failed.set(true);
connectionFactory.getProcessor(location).process(new AuthTokenCheckFailed(argument.getRequestId(), "stacktrace", ErrorCode.TOKEN_EXPIRED));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
succeeded.set(true);
connectionFactory.getProcessor(location).process(new SegmentCreated(argument.getRequestId(), "transient-segment"));
return null;
}
}).when(connection).send(any(CreateTransientSegment.class));
LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
assertTrue(failed.getAndSet(false));
assertTrue(succeeded.getAndSet(false));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
failed.set(true);
connectionFactory.getProcessor(location).process(new WrongHost(argument.getRequestId(), "foo/bar/1", null, "stacktrace"));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
succeeded.set(true);
connectionFactory.getProcessor(location).process(new SegmentCreated(argument.getRequestId(), "transient-segment"));
return null;
}
}).when(connection).send(any(CreateTransientSegment.class));
writer = new LargeEventWriter(writerId, controller, connectionFactory);
writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
assertTrue(failed.getAndSet(false));
assertTrue(succeeded.getAndSet(false));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
failed.set(true);
connectionFactory.getProcessor(location).processingFailure(new ConnectionFailedException());
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
succeeded.set(true);
connectionFactory.getProcessor(location).process(new SegmentCreated(argument.getRequestId(), "transient-segment"));
return null;
}
}).when(connection).send(any(CreateTransientSegment.class));
writer = new LargeEventWriter(writerId, controller, connectionFactory);
writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
assertTrue(failed.getAndSet(false));
assertTrue(succeeded.getAndSet(false));
}
Aggregations