Search in sources :

Example 1 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class SegmentIteratorTest method testTruncate.

@Test(timeout = 5000)
public void testTruncate() {
    MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
    Segment segment = new Segment("Scope", "Stream", 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
    }, config, DelegationTokenProviderFactory.createWithEmptyToken());
    sendData("1", outputStream);
    sendData("2", outputStream);
    sendData("3", outputStream);
    @Cleanup SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
    long length = metadataClient.getSegmentInfo().join().getWriteOffset();
    @Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
    assertEquals("1", iter.next());
    long segmentLength = metadataClient.fetchCurrentSegmentLength().join();
    assertEquals(0, segmentLength % 3);
    metadataClient.truncateSegment(segmentLength * 2 / 3).join();
    AssertExtensions.assertThrows(TruncatedDataException.class, () -> iter.next());
    @Cleanup SegmentIteratorImpl<String> iter2 = new SegmentIteratorImpl<>(factory, segment, stringSerializer, segmentLength * 2 / 3, length);
    assertTrue(iter2.hasNext());
    assertEquals("3", iter2.next());
    assertFalse(iter.hasNext());
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) Test(org.junit.Test)

Example 2 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class SegmentIteratorTest method testOffset.

@Test(timeout = 5000)
public void testOffset() {
    MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
    Segment segment = new Segment("Scope", "Stream", 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
    }, config, DelegationTokenProviderFactory.createWithEmptyToken());
    sendData("1", outputStream);
    sendData("2", outputStream);
    sendData("3", outputStream);
    @Cleanup SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
    long length = metadataClient.getSegmentInfo().join().getWriteOffset();
    @Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
    assertEquals(0, iter.getOffset());
    assertEquals("1", iter.next());
    assertEquals(length / 3, iter.getOffset());
    assertEquals("2", iter.next());
    assertEquals(length / 3 * 2, iter.getOffset());
    assertTrue(iter.hasNext());
    assertEquals(length / 3 * 2, iter.getOffset());
    assertEquals("3", iter.next());
    assertEquals(length, iter.getOffset());
    assertThrows(NoSuchElementException.class, () -> iter.next());
    assertFalse(iter.hasNext());
    assertEquals(length, iter.getOffset());
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) Test(org.junit.Test)

Example 3 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class SegmentIteratorTest method testHasNext.

@Test(timeout = 5000)
public void testHasNext() {
    MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
    Segment segment = new Segment("Scope", "Stream", 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
    }, config, DelegationTokenProviderFactory.createWithEmptyToken());
    sendData("1", outputStream);
    sendData("2", outputStream);
    sendData("3", outputStream);
    @Cleanup SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
    long length = metadataClient.getSegmentInfo().join().getWriteOffset();
    @Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
    assertTrue(iter.hasNext());
    assertTrue(iter.hasNext());
    assertEquals("1", iter.next());
    assertEquals("2", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("3", iter.next());
    assertFalse(iter.hasNext());
    assertThrows(NoSuchElementException.class, () -> iter.next());
    assertFalse(iter.hasNext());
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) Test(org.junit.Test)

Example 4 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class RevisionedStreamClientTest method testCreateRevisionedStreamClientError.

@Test
public void testCreateRevisionedStreamClientError() {
    String scope = "scope";
    String stream = "stream";
    JavaSerializer<Serializable> serializer = new JavaSerializer<>();
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    Controller controller = Mockito.mock(Controller.class);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    // Simulate sealed stream.
    CompletableFuture<StreamSegments> result = new CompletableFuture<>();
    result.complete(new StreamSegments(new TreeMap<>()));
    when(controller.getCurrentSegments(scope, stream)).thenReturn(result);
    assertThrows(InvalidStreamException.class, () -> clientFactory.createRevisionedStreamClient(stream, serializer, config));
    // Simulate invalid stream.
    result = new CompletableFuture<>();
    result.completeExceptionally(new RuntimeException());
    when(controller.getCurrentSegments(scope, stream)).thenReturn(result);
    assertThrows(InvalidStreamException.class, () -> clientFactory.createRevisionedStreamClient(stream, serializer, config));
    // Simulate null result from Controller.
    result = new CompletableFuture<>();
    result.complete(null);
    when(controller.getCurrentSegments(scope, stream)).thenReturn(result);
    assertThrows(InvalidStreamException.class, () -> clientFactory.createRevisionedStreamClient(stream, serializer, config));
}
Also used : Serializable(java.io.Serializable) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Cleanup(lombok.Cleanup) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) CompletableFuture(java.util.concurrent.CompletableFuture) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) StreamSegments(io.pravega.client.stream.impl.StreamSegments) Test(org.junit.Test)

Example 5 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class RevisionedStreamClientTest method testMark.

@Test
public void testMark() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    client.writeUnconditionally("a");
    Revision ra = client.fetchLatestRevision();
    client.writeUnconditionally("b");
    Revision rb = client.fetchLatestRevision();
    client.writeUnconditionally("c");
    Revision rc = client.fetchLatestRevision();
    assertTrue(client.compareAndSetMark(null, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, rb));
    assertEquals(rb, client.getMark());
    assertFalse(client.compareAndSetMark(ra, rc));
    assertEquals(rb, client.getMark());
    assertTrue(client.compareAndSetMark(rb, rc));
    assertEquals(rc, client.getMark());
    assertTrue(client.compareAndSetMark(rc, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, null));
    assertEquals(null, client.getMark());
}
Also used : SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Cleanup(lombok.Cleanup) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Aggregations

MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)58 Cleanup (lombok.Cleanup)55 Test (org.junit.Test)55 MockController (io.pravega.client.stream.mock.MockController)48 Segment (io.pravega.client.segment.impl.Segment)46 AtomicLong (java.util.concurrent.atomic.AtomicLong)39 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)31 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)30 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)27 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)24 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)23 Controller (io.pravega.client.control.impl.Controller)19 HashMap (java.util.HashMap)16 ByteBuffer (java.nio.ByteBuffer)11 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)8 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)8 lombok.val (lombok.val)8 EventSegmentReader (io.pravega.client.segment.impl.EventSegmentReader)7 InOrder (org.mockito.InOrder)7 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)6