use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentMetadataClientTest method testExceptionOnSend.
@Test(timeout = 10000)
public void testExceptionOnSend() throws Exception {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup("shutdown") InlineExecutor executor = new InlineExecutor();
@Cleanup ConnectionFactory cf = Mockito.mock(ConnectionFactory.class);
Mockito.when(cf.getInternalExecutor()).thenReturn(executor);
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf);
ClientConnection connection1 = mock(ClientConnection.class);
ClientConnection connection2 = mock(ClientConnection.class);
AtomicReference<ReplyProcessor> processor = new AtomicReference<>();
Mockito.when(cf.establishConnection(Mockito.eq(endpoint), Mockito.any())).thenReturn(Futures.failedFuture(new ConnectionFailedException())).thenReturn(CompletableFuture.completedFuture(connection1)).thenAnswer(new Answer<CompletableFuture<ClientConnection>>() {
@Override
public CompletableFuture<ClientConnection> answer(InvocationOnMock invocation) throws Throwable {
processor.set(invocation.getArgument(1));
return CompletableFuture.completedFuture(connection2);
}
});
WireCommands.GetStreamSegmentInfo getSegmentInfo1 = new WireCommands.GetStreamSegmentInfo(2, segment.getScopedName(), "");
Mockito.doThrow(new ConnectionFailedException()).when(connection1).send(getSegmentInfo1);
WireCommands.GetStreamSegmentInfo getSegmentInfo2 = new WireCommands.GetStreamSegmentInfo(3, segment.getScopedName(), "");
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
processor.get().process(new StreamSegmentInfo(3, segment.getScopedName(), true, false, false, 0, 123, 121));
return null;
}
}).when(connection2).send(getSegmentInfo2);
@Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
InOrder order = Mockito.inOrder(connection1, connection2, cf);
long length = client.fetchCurrentSegmentLength();
order.verify(cf, Mockito.times(2)).establishConnection(Mockito.eq(endpoint), Mockito.any());
order.verify(connection1).send(getSegmentInfo1);
order.verify(connection1).close();
order.verify(cf).establishConnection(Mockito.eq(endpoint), Mockito.any());
order.verify(connection2).send(getSegmentInfo2);
order.verifyNoMoreInteractions();
assertEquals(123, length);
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentOutputStreamTest method testConnectionFailure.
@Test
public void testConnectionFailure() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
@Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
cf.setExecutor(inlineExecutor);
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
output.reconnect();
InOrder inOrder = Mockito.inOrder(connection);
inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Boolean> acked = new CompletableFuture<>();
Append append = new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null);
CompletableFuture<Boolean> acked2 = new CompletableFuture<>();
Append append2 = new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(data), null);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).connectionDropped();
throw new ConnectionFailedException();
}
}).when(connection).send(append);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
callback.complete(null);
return null;
}
}).when(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
Async.testBlocking(() -> {
output.write(new PendingEvent(null, data, acked, null));
output.write(new PendingEvent(null, data, acked2, null));
}, () -> {
cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0));
});
inOrder.verify(connection).send(append);
inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
inOrder.verify(connection).send(append2);
assertEquals(false, acked.isDone());
assertEquals(false, acked2.isDone());
inOrder.verifyNoMoreInteractions();
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentOutputStreamTest method testFailureWhileRetransmittingInflight.
@Test(timeout = 10000)
public void testFailureWhileRetransmittingInflight() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
// Ensure task submitted to executor is run inline.
implementAsDirectExecutor(executor);
cf.setExecutor(executor);
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
ClientConnection connection = mock(ClientConnection.class);
InOrder inOrder = inOrder(connection);
cf.provideConnection(uri, connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
output.reconnect();
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
output.write(new PendingEvent(null, getBuffer("test1"), new CompletableFuture<>()));
output.write(new PendingEvent(null, getBuffer("test2"), new CompletableFuture<>()));
doAnswer(new Answer<Void>() {
boolean failed = false;
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
if (failed) {
callback.complete(null);
} else {
failed = true;
callback.complete(new ConnectionFailedException("Injected"));
}
return null;
}
}).when(connection).sendAsync(Mockito.any(), Mockito.any());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0));
return null;
}
}).when(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
cf.getProcessor(uri).connectionDropped();
Async.testBlocking(() -> output.write(new PendingEvent(null, getBuffer("test3"), new CompletableFuture<>())), () -> {
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
});
output.write(new PendingEvent(null, getBuffer("test4"), new CompletableFuture<>()));
Append append1 = new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null);
Append append2 = new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(getBuffer("test2")), null);
Append append3 = new Append(SEGMENT, cid, 3, Unpooled.wrappedBuffer(getBuffer("test3")), null);
Append append4 = new Append(SEGMENT, cid, 4, Unpooled.wrappedBuffer(getBuffer("test4")), null);
inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
inOrder.verify(connection).send(append1);
inOrder.verify(connection).send(append2);
inOrder.verify(connection).close();
inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
inOrder.verify(connection).close();
inOrder.verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
inOrder.verify(connection).send(append3);
inOrder.verify(connection).send(append4);
verifyNoMoreInteractions(connection);
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentOutputStreamTest method testExceptionSealedCallback.
@Test(timeout = 10000)
public void testExceptionSealedCallback() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
// Ensure task submitted to executor is run inline.
implementAsDirectExecutor(executor);
cf.setExecutor(executor);
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
AtomicBoolean shouldThrow = new AtomicBoolean(true);
// call back which throws an exception.
Consumer<Segment> exceptionCallback = s -> {
if (shouldThrow.getAndSet(false)) {
throw new IllegalStateException();
}
};
SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, exceptionCallback, RETRY_SCHEDULE, "");
output.reconnect();
verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Boolean> ack = new CompletableFuture<>();
output.write(new PendingEvent(null, data, ack));
assertEquals(false, ack.isDone());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(3, SEGMENT, cid, 0));
return null;
}
}).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
Async.testBlocking(() -> {
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}, () -> {
cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(1, SEGMENT));
output.getUnackedEventsOnSeal();
});
verify(connection).send(new WireCommands.KeepAlive());
verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
assertEquals(false, ack.isDone());
}
Aggregations