Search in sources :

Example 31 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEntryResultTooShort.

/**
 * Tests the ability to handle a case where the key could not be read before the read result was done.
 */
@Test
public void testReadEntryResultTooShort() {
    val testItems = generateTestItems();
    for (val e : testItems) {
        // Start a new reader & processor for this key-serialization pair.
        val entryReader = AsyncTableEntryReader.readEntry(new ByteArraySegment(e.key), 0L, SERIALIZER, new TimeoutTimer(TIMEOUT));
        @Cleanup val rr = new ReadResultMock(e.serialization, e.serialization.length - 1, 1);
        AsyncReadResultProcessor.process(rr, entryReader, executorService());
        AssertExtensions.assertThrows("Unexpected behavior for shorter read result..", () -> entryReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) SerializationException(io.pravega.common.io.SerializationException) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 32 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class CheckpointOperationTests method configurePreSerialization.

@Override
protected void configurePreSerialization(CheckpointOperationBase operation, Random random) {
    if (operation.getContents() == null) {
        byte[] data = new byte[10245];
        random.nextBytes(data);
        operation.setContents(new ByteArraySegment(data));
    } else if (isPreSerializationConfigRequired(operation)) {
        Assert.fail("isPreSerializationConfigRequired returned true but there is nothing to be done.");
    }
}
Also used : ByteArraySegment(io.pravega.common.util.ByteArraySegment)

Example 33 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class CachedStreamSegmentAppendOperationTests method testConstructor.

/**
 * Tests the constructor of the operation, based on an existing StreamSegmentAppendOperation and CacheKey.
 */
@Test
public void testConstructor() {
    ByteArraySegment data = new ByteArraySegment("foo".getBytes());
    val attributes = StreamSegmentAppendOperationTests.createAttributes();
    StreamSegmentAppendOperation baseOp = new StreamSegmentAppendOperation(SEGMENT_ID, data, attributes);
    baseOp.setSequenceNumber(1);
    baseOp.setStreamSegmentOffset(OFFSET);
    // Valid scenarios.
    CachedStreamSegmentAppendOperation newOp = new CachedStreamSegmentAppendOperation(baseOp);
    OperationComparer.DEFAULT.assertEquals("Unexpected result from constructor.", baseOp, newOp);
    // Invalid scenarios.
    AssertExtensions.assertThrows("Unexpected exception when invalid offset.", () -> new CachedStreamSegmentAppendOperation(new StreamSegmentAppendOperation(SEGMENT_ID, data, attributes)), ex -> ex instanceof IllegalArgumentException || ex instanceof IllegalStateException);
    AssertExtensions.assertThrows("Unexpected exception when invalid sequence number.", () -> {
        StreamSegmentAppendOperation badOp = new StreamSegmentAppendOperation(SEGMENT_ID, data, attributes);
        baseOp.setStreamSegmentOffset(OFFSET);
        new CachedStreamSegmentAppendOperation(badOp);
    }, ex -> ex instanceof IllegalArgumentException || ex instanceof IllegalStateException);
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Test(org.junit.Test)

Example 34 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class TableBucketReaderTests method generateEntries.

private List<TableEntry> generateEntries(EntrySerializer s) {
    val rnd = new Random(0);
    val result = new ArrayList<TableEntry>();
    long version = 0;
    for (int i = 0; i < COUNT; i++) {
        byte[] keyData = new byte[KEY_LENGTH];
        rnd.nextBytes(keyData);
        byte[] valueData = new byte[VALUE_LENGTH];
        rnd.nextBytes(valueData);
        result.add(TableEntry.versioned(new ByteArraySegment(keyData), new ByteArraySegment(valueData), version));
        version += s.getUpdateLength(result.get(result.size() - 1));
    }
    return result;
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Random(java.util.Random) ArrayList(java.util.ArrayList)

Example 35 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class TableBucketReaderTests method testFindKey.

/**
 * Tests the ability to locate Table Keys in a Table Bucket using {@link TableBucketReader#key}.
 */
@Test
public void testFindKey() throws Exception {
    val segment = new SegmentMock(executorService());
    // Generate our test data and append it to the segment.
    val data = generateData();
    segment.append(new ByteArraySegment(data.serialization), null, TIMEOUT).join();
    val reader = TableBucketReader.key(segment, (s, offset, timeout) -> CompletableFuture.completedFuture(data.getBackpointer(offset)), executorService());
    // Check a valid result.
    val validKey = data.entries.get(1).getKey();
    val validResult = reader.find(validKey.getKey(), data.getBucketOffset(), new TimeoutTimer(TIMEOUT)).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    Assert.assertEquals("Unexpected version from valid key.", data.getEntryOffset(1), validResult.getVersion());
    Assert.assertEquals("Unexpected 'valid' key returned.", validKey.getKey(), validResult.getKey());
    // Check a key that does not exist.
    val invalidKey = data.unlinkedEntry.getKey();
    val invalidResult = reader.find(invalidKey.getKey(), data.getBucketOffset(), new TimeoutTimer(TIMEOUT)).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    Assert.assertNull("Not expecting any result for key that does not exist.", invalidResult);
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) SegmentMock(io.pravega.segmentstore.server.SegmentMock) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Aggregations

ByteArraySegment (io.pravega.common.util.ByteArraySegment)222 lombok.val (lombok.val)158 Test (org.junit.Test)145 Cleanup (lombok.Cleanup)114 ArrayList (java.util.ArrayList)88 CompletableFuture (java.util.concurrent.CompletableFuture)58 BufferView (io.pravega.common.util.BufferView)54 HashMap (java.util.HashMap)54 List (java.util.List)52 AssertExtensions (io.pravega.test.common.AssertExtensions)50 Assert (org.junit.Assert)49 Duration (java.time.Duration)48 AtomicReference (java.util.concurrent.atomic.AtomicReference)44 Collectors (java.util.stream.Collectors)42 IOException (java.io.IOException)41 AtomicLong (java.util.concurrent.atomic.AtomicLong)41 IntentionalException (io.pravega.test.common.IntentionalException)40 Random (java.util.Random)40 Map (java.util.Map)39 Rule (org.junit.Rule)39