Search in sources :

Example 41 with MockSegmentStreamFactory

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

the class SynchronizerTest method testConsistency.

@Test(timeout = 20000)
public void testConsistency() {
    String streamName = "streamName";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactory.getController());
    @Cleanup StateSynchronizer<RevisionedImpl> syncA = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    @Cleanup StateSynchronizer<RevisionedImpl> syncB = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    syncA.initialize(new RegularUpdate("Foo"));
    AtomicInteger callCount = new AtomicInteger(0);
    syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bar"));
    });
    assertEquals(1, callCount.get());
    assertEquals("Foo", syncA.getState().value);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Baz"));
    });
    assertEquals(3, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncB.fetchUpdates();
    assertEquals("Baz", syncA.getState().value);
    syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bat"));
    });
    assertEquals(4, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncA.fetchUpdates();
    assertEquals("Bat", syncA.getState().value);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 42 with MockSegmentStreamFactory

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

the class SynchronizerTest method testCompactionShrinksSet.

@Test(timeout = 20000)
public void testCompactionShrinksSet() {
    String streamName = "testCompactionShrinksSet";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactory.getController());
    SetSynchronizer<String> set = SetSynchronizer.createNewSet(streamName, clientFactory);
    RevisionedStreamClient<byte[]> rsc = clientFactory.createRevisionedStreamClient(streamName, new ByteArraySerializer(), SynchronizerConfig.builder().build());
    set.add("Foo");
    assertNull(rsc.getMark());
    set.add("Bar");
    assertNull(rsc.getMark());
    set.clear();
    assertNotNull(rsc.getMark());
    Iterator<?> iter = rsc.readFrom(rsc.getMark());
    assertTrue(iter.hasNext());
    iter.next();
    assertFalse(iter.hasNext());
    set.add("Foo2");
    assertNotNull(rsc.getMark());
    assertEquals(1, set.getCurrentSize());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) ByteArraySerializer(io.pravega.client.stream.impl.ByteArraySerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 43 with MockSegmentStreamFactory

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

the class EventStreamReaderTest method testCheckpoint.

@Test(timeout = 10000)
public void testCheckpoint() throws SegmentSealedException, ReaderNotInReaderGroupException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    @Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
    Segment segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
    Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
    @Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    ByteBuffer buffer = writeInt(stream, 1);
    Mockito.when(groupState.getCheckpoint()).thenReturn("Foo").thenReturn(null);
    EventRead<byte[]> eventRead = reader.readNextEvent(0);
    assertTrue(eventRead.isCheckpoint());
    assertNull(eventRead.getEvent());
    assertEquals("Foo", eventRead.getCheckpointName());
    InOrder order = Mockito.inOrder(groupState);
    order.verify(groupState).getCheckpoint();
    order.verify(groupState, Mockito.never()).checkpoint(Mockito.anyString(), Mockito.any());
    assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    order.verify(groupState).checkpoint(Mockito.eq("Foo"), Mockito.any());
    order.verify(groupState).getCheckpoint();
    reader.close();
}
Also used : InOrder(org.mockito.InOrder) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 44 with MockSegmentStreamFactory

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

the class EventStreamReaderTest method testDataTruncated.

@Test(timeout = 10000)
public void testDataTruncated() throws SegmentSealedException, ReaderNotInReaderGroupException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    @Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
    Segment segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
    Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
    @Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    @Cleanup SegmentMetadataClient metadataClient = segmentStreamFactory.createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
    ByteBuffer buffer1 = writeInt(stream, 1);
    ByteBuffer buffer2 = writeInt(stream, 2);
    writeInt(stream, 3);
    long length = metadataClient.fetchCurrentSegmentLength().join();
    assertEquals(0, length % 3);
    EventRead<byte[]> event1 = reader.readNextEvent(0);
    assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
    metadataClient.truncateSegment(length / 3).join();
    assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    metadataClient.truncateSegment(length).join();
    ByteBuffer buffer4 = writeInt(stream, 4);
    assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(0));
    assertEquals(buffer4, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    assertThrows(NoSuchEventException.class, () -> reader.fetchEvent(event1.getEventPointer()));
    reader.close();
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 45 with MockSegmentStreamFactory

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

the class EventStreamReaderTest method testCheckpointFollowingSilentCheckpointFollowingCheckpoint.

@Test(timeout = 10000)
public void testCheckpointFollowingSilentCheckpointFollowingCheckpoint() throws SegmentSealedException, ReaderNotInReaderGroupException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    @Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
    Segment segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
    @Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    ByteBuffer buffer = writeInt(stream, 1);
    Mockito.doReturn(true).when(groupState).isCheckpointSilent(Mockito.startsWith(ReaderGroupImpl.SILENT));
    Mockito.when(groupState.getCheckpoint()).thenReturn("Foo").thenReturn(ReaderGroupImpl.SILENT + "Bar").thenReturn("Baz").thenReturn(null);
    Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
    EventRead<byte[]> eventRead = reader.readNextEvent(10000);
    assertTrue(eventRead.isCheckpoint());
    assertNull(eventRead.getEvent());
    assertEquals("Foo", eventRead.getCheckpointName());
    eventRead = reader.readNextEvent(0);
    assertTrue(eventRead.isCheckpoint());
    assertNull(eventRead.getEvent());
    assertEquals("Baz", eventRead.getCheckpointName());
    InOrder order = Mockito.inOrder(groupState);
    order.verify(groupState).getCheckpoint();
    order.verify(groupState).checkpoint(Mockito.eq("Foo"), Mockito.any());
    assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    order.verify(groupState).getCheckpoint();
    order.verify(groupState).checkpoint(Mockito.eq(ReaderGroupImpl.SILENT + "Bar"), Mockito.any());
    order.verify(groupState).getCheckpoint();
    order.verify(groupState).checkpoint(Mockito.eq("Baz"), Mockito.any());
    order.verify(groupState).findSegmentToReleaseIfRequired();
    order.verify(groupState).getCheckpoint();
    reader.close();
}
Also used : InOrder(org.mockito.InOrder) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) 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