use of io.pravega.common.util.CompositeByteArraySegment in project pravega by pravega.
the class WriteQueueTests method testAdd.
/**
* Tests the basic functionality of the add() method.
*/
@Test
public void testAdd() {
// Just over 1ms.
final int timeIncrement = 1234 * 1000;
AtomicLong time = new AtomicLong();
val q = new WriteQueue(time::get);
val initialStats = q.getStatistics();
Assert.assertEquals("Unexpected getSize on empty queue.", 0, initialStats.getSize());
Assert.assertEquals("Unexpected getAverageFillRate on empty queue.", 0, initialStats.getAverageItemFillRatio(), 0);
Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis on empty queue.", 0, initialStats.getExpectedProcessingTimeMillis());
int expectedSize = 0;
long firstItemTime = 0;
val writeResults = new ArrayList<CompletableFuture<LogAddress>>();
for (int i = 0; i < ITEM_COUNT; i++) {
time.addAndGet(timeIncrement);
if (i == 0) {
firstItemTime = time.get();
}
int writeSize = i * 10000;
val writeResult = new CompletableFuture<LogAddress>();
q.add(new Write(new CompositeByteArraySegment(writeSize), new TestWriteLedger(i), writeResult));
writeResults.add(writeResult);
expectedSize += writeSize;
q.removeFinishedWrites();
val stats = q.getStatistics();
val expectedFillRatio = (double) expectedSize / stats.getSize() / BookKeeperConfig.MAX_APPEND_LENGTH;
val expectedProcTime = (time.get() - firstItemTime) / AbstractTimer.NANOS_TO_MILLIS;
Assert.assertEquals("Unexpected getSize.", i + 1, stats.getSize());
Assert.assertEquals("Unexpected getAverageFillRate.", expectedFillRatio, stats.getAverageItemFillRatio(), 0.01);
Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis.", expectedProcTime, stats.getExpectedProcessingTimeMillis());
}
// Now verify the stats are also updated when finishing writes.
for (int i = 0; i < writeResults.size(); i++) {
writeResults.get(i).complete(null);
val cs = q.removeFinishedWrites();
Assert.assertEquals("Unexpected number of removed items", 1, cs.getRemovedCount());
Assert.assertEquals("Unexpected size after removing " + (i + 1), ITEM_COUNT - i - 1, q.getStatistics().getSize());
}
}
use of io.pravega.common.util.CompositeByteArraySegment in project pravega by pravega.
the class WriteQueueTests method testClose.
/**
* Tests the close() method.
*/
@Test
public void testClose() {
val q = new WriteQueue();
val expectedWrites = new ArrayList<Write>();
for (int i = 0; i < ITEM_COUNT; i++) {
val w = new Write(new CompositeByteArraySegment(i), new TestWriteLedger(i), CompletableFuture.completedFuture(null));
q.add(w);
expectedWrites.add(w);
}
val removedWrites = q.close();
AssertExtensions.assertListEquals("Unexpected writes removed.", expectedWrites, removedWrites, Object::equals);
val clearStats = q.getStatistics();
Assert.assertEquals("Unexpected getSize after clear.", 0, clearStats.getSize());
Assert.assertEquals("Unexpected getAverageFillRate after clear.", 0, clearStats.getAverageItemFillRatio(), 0);
Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis after clear.", 0, clearStats.getExpectedProcessingTimeMillis());
AssertExtensions.assertThrows("add() worked after close().", () -> q.add(new Write(new CompositeByteArraySegment(1), new TestWriteLedger(0), CompletableFuture.completedFuture(null))), ex -> ex instanceof ObjectClosedException);
AssertExtensions.assertThrows("getWritesToExecute() worked after close().", () -> q.getWritesToExecute(1), ex -> ex instanceof ObjectClosedException);
AssertExtensions.assertThrows("removeFinishedWrites() worked after close().", q::removeFinishedWrites, ex -> ex instanceof ObjectClosedException);
}
use of io.pravega.common.util.CompositeByteArraySegment in project pravega by pravega.
the class BookKeeperLogUnitTests method testHandleWriteException.
private static Write testHandleWriteException(Throwable ex, Class expectedErrorType) {
Write result = new Write(new CompositeByteArraySegment(new byte[0]), mock(WriteLedger.class), new CompletableFuture<>());
BookKeeperLog bookKeeperLog = mock(BookKeeperLog.class);
BookKeeperLog.handleWriteException(ex, result, bookKeeperLog);
assertTrue("Unexpected failure cause " + result.getFailureCause(), expectedErrorType.isInstance(result.getFailureCause()));
assertSame(ex, result.getFailureCause().getCause());
return result;
}
use of io.pravega.common.util.CompositeByteArraySegment in project pravega by pravega.
the class DurableDataLogTestBase method populate.
protected TreeMap<LogAddress, byte[]> populate(DurableDataLog log, int writeCount) {
TreeMap<LogAddress, byte[]> writtenData = new TreeMap<>(Comparator.comparingLong(LogAddress::getSequence));
val data = new ArrayList<byte[]>();
val futures = new ArrayList<CompletableFuture<LogAddress>>();
for (int i = 0; i < writeCount; i++) {
byte[] writeData = getWriteData();
futures.add(log.append(new CompositeByteArraySegment(writeData), TIMEOUT));
data.add(writeData);
}
val addresses = Futures.allOfWithResults(futures).join();
for (int i = 0; i < data.size(); i++) {
writtenData.put(addresses.get(i), data.get(i));
}
return writtenData;
}
use of io.pravega.common.util.CompositeByteArraySegment in project pravega by pravega.
the class DurableDataLogTestBase method testRegisterQueueStateListener.
/**
* Tests the ability to register a {@link ThrottleSourceListener} and notify it of updates.
* @throws Exception If an error occurred.
*/
@Test(timeout = 30000)
public void testRegisterQueueStateListener() throws Exception {
val listener = new TestThrottleSourceListener();
try (DurableDataLog log = createDurableDataLog()) {
log.initialize(TIMEOUT);
log.registerQueueStateChangeListener(listener);
// Only verify sequence number monotonicity. We'll verify reads in its own test.
int writeCount = getWriteCount();
for (int i = 0; i < writeCount; i++) {
log.append(new CompositeByteArraySegment(getWriteData()), TIMEOUT).join();
}
// Verify the correct number of invocations.
TestUtils.await(() -> listener.getCount() == writeCount, 10, TIMEOUT.toMillis());
// Verify that the listener is unregistered when closed.
listener.close();
log.append(new CompositeByteArraySegment(getWriteData()), TIMEOUT).join();
try {
TestUtils.await(() -> listener.getCount() > writeCount, 10, 50);
Assert.fail("Listener's count was updated after it was closed.");
} catch (TimeoutException tex) {
// This is expected. We do not want our condition to hold true.
}
// This should have no effect as it's already closed.
log.registerQueueStateChangeListener(listener);
Assert.assertFalse("Not expected the listener to have been notified after closing.", listener.wasNotifiedWhenClosed());
}
}
Aggregations