use of io.pravega.test.common.AssertExtensions.assertEventuallyEquals in project pravega by pravega.
the class AppendProcessorTest method testOutstandingByteTracking.
/**
* Simulates multiple connections being set up and all sending appends (conditional or unconditional). Some may be
* failed by the store. Verifies that {@link ConnectionTracker#getTotalOutstanding()} does not drift with time,
* regardless of append outcome.
*/
@Test
public void testOutstandingByteTracking() throws Exception {
final int connectionCount = 5;
final int writersCount = 5;
final int segmentCount = 5;
val tracker = new ConnectionTracker();
val context = mock(ChannelHandlerContext.class);
val channel = mock(Channel.class);
val channelConfig = mock(ChannelConfig.class);
when(context.channel()).thenReturn(channel);
when(channel.config()).thenReturn(channelConfig);
val eventLoop = mock(EventLoop.class);
when(channel.eventLoop()).thenReturn(eventLoop);
when(eventLoop.inEventLoop()).thenReturn(false);
val channelFuture = mock(ChannelFuture.class);
when(channel.writeAndFlush(any())).thenReturn(channelFuture);
val segments = IntStream.range(0, segmentCount).mapToObj(Integer::toString).collect(Collectors.toList());
val writers = IntStream.range(0, writersCount).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
val store = mock(StreamSegmentStore.class);
val processors = new ArrayList<AppendProcessor>();
for (int i = 0; i < connectionCount; i++) {
val h = new ServerConnectionInboundHandler();
h.channelRegistered(context);
val p = AppendProcessor.defaultBuilder().store(store).connection(new TrackedConnection(h, tracker)).build();
processors.add(p);
}
// Setup appends.
for (int connectionId = 0; connectionId < processors.size(); connectionId++) {
for (val s : segments) {
for (val w : writers) {
when(store.getAttributes(s, Collections.singleton(AttributeId.fromUUID(w)), true, AppendProcessor.TIMEOUT)).thenReturn(CompletableFuture.completedFuture(Collections.singletonMap(AttributeId.fromUUID(w), 0L)));
processors.get(connectionId).setupAppend(new WireCommands.SetupAppend(0, w, s, null));
}
}
}
// Divide the segments into conditional and unconditional.
val conditionalSegments = segments.subList(0, segments.size() / 2);
val unconditionalSegments = segments.subList(conditionalSegments.size(), segments.size() - 1);
// Send a few appends to each connection from each writer.
val appendData = Unpooled.wrappedBuffer(new byte[1]);
when(store.append(any(), any(), any(), any())).thenReturn(delayedResponse(0L));
for (val s : unconditionalSegments) {
for (val p : processors) {
for (val w : writers) {
p.append(new Append(s, w, 1, new WireCommands.Event(appendData.retain()), 0));
}
}
}
// Send a few conditional appends to each connection from each writer. Fail some along the way.
int appendOffset = 0;
for (val s : conditionalSegments) {
for (val p : processors) {
for (val w : writers) {
boolean fail = appendOffset % 3 == 0;
if (fail) {
when(store.append(any(), any(long.class), any(), any(), any())).thenReturn(delayedFailure(new BadOffsetException(s, appendOffset, appendOffset)));
} else {
when(store.append(any(), any(long.class), any(), any(), any())).thenReturn(delayedResponse(0L));
}
p.append(new Append(s, w, 1, new WireCommands.Event(appendData.retain()), appendOffset, 0));
appendOffset++;
}
}
}
// Fail (attributes) all connections.
when(store.append(any(), any(), any(), any())).thenReturn(delayedFailure(new BadAttributeUpdateException("s", null, false, "intentional")));
for (val s : conditionalSegments) {
for (val p : processors) {
for (val w : writers) {
p.append(new Append(s, w, 1, new WireCommands.Event(appendData.retain()), 0));
}
}
}
// Verify that there is no drift in the ConnectionTracker#getTotalOutstanding value. Due to the async nature
// of the calls, this value may not immediately be updated.
AssertExtensions.assertEventuallyEquals(0L, tracker::getTotalOutstanding, 10000);
}
Aggregations