use of org.mockito.Mockito.inOrder in project pravega by pravega.
the class SegmentOutputStreamTest method testSegmentSealedFollowedbyConnectionDrop.
@Test(timeout = 10000)
public void testSegmentSealedFollowedbyConnectionDrop() throws Exception {
@Cleanup("shutdownNow") ScheduledExecutorService executor = ExecutorServiceHelpers.newScheduledThreadPool(2, "netty-callback");
// Segment sealed callback will finish execution only when the releaseCallbackLatch is released;
ReusableLatch releaseCallbackLatch = new ReusableLatch(false);
ReusableLatch callBackInvokedLatch = new ReusableLatch(false);
final Consumer<Segment> segmentSealedCallback = segment -> Exceptions.handleInterrupted(() -> {
callBackInvokedLatch.release();
releaseCallbackLatch.await();
});
// Setup mocks.
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
cf.setExecutor(executorService());
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
// Mock client connection that is returned for every invocation of ConnectionFactory#establishConnection.
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
InOrder order = Mockito.inOrder(connection);
// Create a Segment writer.
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
// trigger establishment of connection.
output.reconnect();
// Verify if SetupAppend is sent over the connection.
order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
// Write an event and ensure inflight has an event.
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> ack = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack));
order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertFalse(ack.isDone());
// Simulate a SegmentIsSealed WireCommand from SegmentStore.
executor.submit(() -> cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(output.getRequestId(), SEGMENT, "SomeException", 1)));
// Wait until callback invocation has been triggered, but has not completed.
// If the callback is not invoked the test will fail due to a timeout.
callBackInvokedLatch.await();
// Now trigger a connection drop netty callback and wait until it is executed.
executor.submit(() -> cf.getProcessor(uri).connectionDropped()).get();
// close is invoked on the connection.
order.verify(connection).close();
// Verify no further reconnection attempts which involves sending of SetupAppend wire command.
order.verifyNoMoreInteractions();
// Release latch to ensure the callback is completed.
releaseCallbackLatch.release();
// Verify no further reconnection attempts which involves sending of SetupAppend wire command.
order.verifyNoMoreInteractions();
// Trigger a reconnect again and verify if any new connections are initiated.
output.reconnect();
// Reconnect operation will be executed on the executor service.
ScheduledExecutorService service = executorService();
service.shutdown();
// Wait until all the tasks for reconnect have been completed.
service.awaitTermination(10, TimeUnit.SECONDS);
// Verify no further reconnection attempts which involves sending of SetupAppend wire command.
order.verifyNoMoreInteractions();
}
use of org.mockito.Mockito.inOrder in project pravega by pravega.
the class SegmentOutputStreamTest method testFlushIsBlockedUntilCallBackInvoked.
/**
* This test ensures that the flush() on a segment is released only after sealed segment callback is invoked.
* The callback implemented in EventStreamWriter appends this segment to its sealedSegmentQueue.
*/
@Test(timeout = 10000)
public void testFlushIsBlockedUntilCallBackInvoked() throws Exception {
// Segment sealed callback will finish execution only when the latch is released;
ReusableLatch latch = new ReusableLatch(false);
final Consumer<Segment> segmentSealedCallback = segment -> Exceptions.handleInterrupted(() -> latch.await());
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
cf.setExecutor(executorService());
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
InOrder order = Mockito.inOrder(connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> ack = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack));
order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, ack.isDone());
@Cleanup("shutdownNow") ScheduledExecutorService executor = ExecutorServiceHelpers.newScheduledThreadPool(1, "netty-callback");
// simulate a SegmentIsSealed WireCommand from SegmentStore.
executor.submit(() -> cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(output.getRequestId(), SEGMENT, "SomeException", 1)));
AssertExtensions.assertBlocks(() -> {
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}, () -> latch.release());
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}
Aggregations