use of io.pravega.segmentstore.storage.mocks.AbstractInMemoryChunkStorage in project pravega by pravega.
the class ChunkedSegmentStorageTests method testSimpleScenarioWithNonAppendProvider.
/**
* Test simple scenario for storage that does not support any appends.
*
* @throws Exception
*/
@Test
public void testSimpleScenarioWithNonAppendProvider() throws Exception {
String testSegmentName = "foo";
// Force rollover after every 2 byte.
SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
@Cleanup TestContext testContext = getTestContext(ChunkedSegmentStorageConfig.DEFAULT_CONFIG.toBuilder().indexBlockSize(3).build());
((AbstractInMemoryChunkStorage) testContext.chunkStorage).setShouldSupportAppend(false);
// Step 1: Create segment.
val h = testContext.chunkedSegmentStorage.create(testSegmentName, policy, null).get();
Assert.assertEquals(h.getSegmentName(), testSegmentName);
Assert.assertFalse(h.isReadOnly());
HashSet<String> chunksBefore = new HashSet<>();
chunksBefore.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
// Check metadata is stored.
val segmentMetadata = TestUtils.getSegmentMetadata(testContext.metadataStore, testSegmentName);
Assert.assertNotNull(segmentMetadata);
Assert.assertEquals(segmentMetadata.getName(), testSegmentName);
Assert.assertEquals(segmentMetadata.getKey(), testSegmentName);
// Check exists
Assert.assertTrue(testContext.chunkedSegmentStorage.exists(testSegmentName, null).get());
// Check getStreamSegmentInfo.
SegmentProperties info = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).get();
Assert.assertFalse(info.isSealed());
Assert.assertFalse(info.isDeleted());
Assert.assertEquals(info.getName(), testSegmentName);
Assert.assertEquals(info.getLength(), 0);
Assert.assertEquals(info.getStartOffset(), 0);
// Write some data.
long writeAt = 0;
for (int i = 1; i < 5; i++) {
testContext.chunkedSegmentStorage.write(h, writeAt, new ByteArrayInputStream(new byte[i]), i, null).join();
writeAt += i;
}
TestUtils.checkSegmentLayout(testContext.metadataStore, testSegmentName, new long[] { // First write
1, // Second write
2, // Third write
2, // Third write
1, // Fourth write
2, // Fourth write
2 });
TestUtils.checkSegmentBounds(testContext.metadataStore, testSegmentName, 0, 10);
TestUtils.checkReadIndexEntries(testContext.chunkedSegmentStorage, testContext.metadataStore, testSegmentName, 0, 10, true);
TestUtils.checkChunksExistInStorage(testContext.chunkStorage, testContext.metadataStore, testSegmentName);
// Check getStreamSegmentInfo.
info = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).get();
Assert.assertFalse(info.isSealed());
Assert.assertFalse(info.isDeleted());
Assert.assertEquals(info.getName(), testSegmentName);
Assert.assertEquals(info.getLength(), 10);
Assert.assertEquals(info.getStartOffset(), 0);
// Open write handle.
val hWrite = testContext.chunkedSegmentStorage.openWrite(testSegmentName).get();
Assert.assertEquals(hWrite.getSegmentName(), testSegmentName);
Assert.assertFalse(hWrite.isReadOnly());
testContext.chunkedSegmentStorage.write(hWrite, 10, new ByteArrayInputStream(new byte[4]), 4, null).join();
TestUtils.checkSegmentLayout(testContext.metadataStore, testSegmentName, new long[] { // First write
1, // Second write
2, // Third write
2, // Third write
1, // Fourth write
2, // Fourth write
2, // Recent write
2, // Recent write
2 });
TestUtils.checkSegmentBounds(testContext.metadataStore, testSegmentName, 0, 14);
TestUtils.checkReadIndexEntries(testContext.chunkedSegmentStorage, testContext.metadataStore, testSegmentName, 0, 14, true);
TestUtils.checkChunksExistInStorage(testContext.chunkStorage, testContext.metadataStore, testSegmentName);
HashSet<String> chunksAfter = new HashSet<>();
chunksAfter.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
TestUtils.checkGarbageCollectionQueue(testContext.chunkedSegmentStorage, chunksBefore, chunksAfter);
info = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).get();
Assert.assertFalse(info.isSealed());
Assert.assertFalse(info.isDeleted());
Assert.assertEquals(info.getName(), testSegmentName);
Assert.assertEquals(info.getLength(), 14);
Assert.assertEquals(info.getStartOffset(), 0);
// Make sure calling create again does not succeed
AssertExtensions.assertFutureThrows("Create succeeded on missing segment.", testContext.chunkedSegmentStorage.create(testSegmentName, policy, null), ex -> ex instanceof StreamSegmentExistsException);
testContext.chunkedSegmentStorage.delete(hWrite, null);
}
Aggregations