use of io.pravega.client.connection.impl.ClientConnection 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);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
DelegationTokenProvider tokenProvider = mock(DelegationTokenProvider.class);
// return empty token
when(tokenProvider.retrieveToken()).thenReturn(CompletableFuture.completedFuture(""));
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, tokenProvider, dataAvailable);
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, Unpooled.EMPTY_BUFFER, in.getRequestId());
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(in.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).process(segmentRead);
return null;
}
}).when(c).send(any(ReadSegment.class));
assertEquals(0, dataAvailable.availablePermits());
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(segmentRead, readFuture.join());
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(1, dataAvailable.availablePermits());
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
verifyNoMoreInteractions(c);
// ensure retrieve Token is invoked for every retry.
verify(tokenProvider, times(3)).retrieveToken();
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method testTruncate.
@Test(timeout = 10000)
public void testTruncate() throws ConnectionFailedException {
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, true);
@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);
AtomicLong requestId = new AtomicLong();
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.TruncateSegment truncateSegment = invocation.getArgument(0);
processor.process(new SegmentTruncated(truncateSegment.getRequestId(), segment.getScopedName()));
requestId.set(truncateSegment.getRequestId());
return null;
}
}).when(connection).send(any(WireCommands.TruncateSegment.class));
client.truncateSegment(123L).join();
Mockito.verify(connection).send(Mockito.eq(new WireCommands.TruncateSegment(requestId.get(), segment.getScopedName(), 123L, "")));
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method testReconnects.
@Test(timeout = 10000)
public void testReconnects() throws Exception {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup MockConnectionFactoryImpl cf = Mockito.spy(new MockConnectionFactoryImpl());
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
@Cleanup ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(endpoint, connection);
@Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
client.getConnection();
final List<Long> requestIds = new ArrayList<>();
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
requestIds.add(request.getRequestId());
if (requestIds.size() == 1) {
ReplyProcessor processor = cf.getProcessor(endpoint);
processor.connectionDropped();
} else {
ReplyProcessor processor = cf.getProcessor(endpoint);
processor.process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
}
return null;
}
}).when(connection).send(any(WireCommands.GetStreamSegmentInfo.class));
long length = client.fetchCurrentSegmentLength().join();
InOrder order = Mockito.inOrder(connection, cf);
order.verify(cf).establishConnection(eq(endpoint), any(ReplyProcessor.class));
order.verify(connection).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(0), segment.getScopedName(), "")));
order.verify(cf).establishConnection(eq(endpoint), any(ReplyProcessor.class));
order.verify(connection).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(1), segment.getScopedName(), "")));
order.verify(cf).getProcessor(eq(endpoint));
order.verifyNoMoreInteractions();
assertEquals(123, length);
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method testGetProperty.
@Test(timeout = 10000)
public void testGetProperty() throws Exception {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
@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 {
WireCommands.GetSegmentAttribute request = invocation.getArgument(0);
processor.process(new WireCommands.SegmentAttribute(request.getRequestId(), 123));
return null;
}
}).when(connection).send(any(WireCommands.GetSegmentAttribute.class));
long value = client.fetchProperty(SegmentAttribute.RevisionStreamClientMark).join();
assertEquals(123, value);
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method testExceptionOnSend.
@Test(timeout = 10000)
public void testExceptionOnSend() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup("shutdown") InlineExecutor executor = new InlineExecutor();
@Cleanup ConnectionPool cf = Mockito.mock(ConnectionPool.class);
Mockito.when(cf.getInternalExecutor()).thenReturn(executor);
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
ClientConnection connection1 = mock(ClientConnection.class);
ClientConnection connection2 = mock(ClientConnection.class);
AtomicReference<ReplyProcessor> processor = new AtomicReference<>();
Mockito.doAnswer(invocation -> {
final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
future.completeExceptionally(new ConnectionFailedException(new RuntimeException("Mock error")));
return null;
}).doAnswer(invocation -> {
final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
future.complete(connection1);
return null;
}).doAnswer(invocation -> {
final CompletableFuture<ClientConnection> future = invocation.getArgument(3);
processor.set(invocation.getArgument(2));
future.complete(connection2);
return null;
}).when(cf).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(ReplyProcessor.class), Mockito.<CompletableFuture<ClientConnection>>any());
final List<Long> requestIds = new ArrayList<>();
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
requestIds.add(request.getRequestId());
if (requestIds.size() == 1) {
throw new ConnectionFailedException();
} else {
processor.get().process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
}
return null;
}
}).when(connection1).send(any(WireCommands.GetStreamSegmentInfo.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = invocation.getArgument(0);
requestIds.add(request.getRequestId());
processor.get().process(new StreamSegmentInfo(request.getRequestId(), segment.getScopedName(), true, false, false, 0, 123, 121));
return null;
}
}).when(connection2).send(any(WireCommands.GetStreamSegmentInfo.class));
@Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
InOrder order = Mockito.inOrder(connection1, connection2, cf);
long length = client.fetchCurrentSegmentLength().join();
order.verify(cf, Mockito.times(2)).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(), Mockito.<CompletableFuture<ClientConnection>>any());
order.verify(connection1).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(0), segment.getScopedName(), "")));
order.verify(connection1).close();
order.verify(cf).getClientConnection(Mockito.any(Flow.class), Mockito.eq(endpoint), Mockito.any(), Mockito.<CompletableFuture<ClientConnection>>any());
order.verify(connection2).send(Mockito.eq(new WireCommands.GetStreamSegmentInfo(requestIds.get(1), segment.getScopedName(), "")));
order.verifyNoMoreInteractions();
assertEquals(123, length);
}
Aggregations