Search in sources :

Example 21 with SegmentOutputStream

use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.

the class EventStreamReaderTest method testRestore.

@Test(timeout = 10000)
public void testRestore() throws SegmentSealedException, ReinitializationRequiredException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
    Segment segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
    ByteBuffer buffer = writeInt(stream, 1);
    Mockito.when(groupState.getCheckpoint()).thenThrow(new ReinitializationRequiredException());
    try {
        reader.readNextEvent(0);
        fail();
    } catch (ReinitializationRequiredException e) {
    // expected
    }
    assertTrue(reader.getReaders().isEmpty());
    reader.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 22 with SegmentOutputStream

use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.

the class EventStreamReaderTest method testCheckpoint.

@Test(timeout = 10000)
public void testCheckpoint() throws SegmentSealedException, ReinitializationRequiredException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
    Segment segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
    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());
    assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    reader.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 23 with SegmentOutputStream

use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.

the class EventStreamWriterTest method testFailOnClose.

@Test
public void testFailOnClose() throws SegmentSealedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
    EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, new JavaSerializer<>(), config, new InlineExecutor());
    Mockito.doThrow(new RuntimeException("Intentional exception")).when(outputStream).close();
    writer.writeEvent("Foo");
    writer.writeEvent("Bar");
    try {
        writer.close();
        fail();
    } catch (RuntimeException e) {
    // expected.
    }
    try {
        writer.writeEvent("fail");
        fail();
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 24 with SegmentOutputStream

use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.

the class SegmentTransactionTest method testFlush.

@Test(timeout = 5000)
public void testFlush() throws TxnFailedException, SegmentSealedException {
    UUID uuid = UUID.randomUUID();
    SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
    SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            PendingEvent event = (PendingEvent) invocation.getArgument(0);
            event.getAckFuture().complete(true);
            return null;
        }
    }).when(outputStream).write(Mockito.any(PendingEvent.class));
    txn.writeEvent("hi");
    verify(outputStream).write(Mockito.any(PendingEvent.class));
    txn.flush();
    verify(outputStream).flush();
    Mockito.verifyNoMoreInteractions(outputStream);
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) Test(org.junit.Test)

Example 25 with SegmentOutputStream

use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.

the class SegmentTransactionTest method testSegmentDoesNotExist.

@Test(timeout = 5000)
public void testSegmentDoesNotExist() {
    UUID uuid = UUID.randomUUID();
    SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
    SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            PendingEvent event = (PendingEvent) invocation.getArgument(0);
            event.getAckFuture().completeExceptionally(new NoSuchSegmentException("segment"));
            return null;
        }
    }).when(outputStream).write(Mockito.any(PendingEvent.class));
    AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.writeEvent("hi"));
    verify(outputStream).write(Mockito.any(PendingEvent.class));
    AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.flush());
    Mockito.verifyNoMoreInteractions(outputStream);
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException) Test(org.junit.Test)

Aggregations

SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)25 Segment (io.pravega.client.segment.impl.Segment)19 Test (org.junit.Test)16 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)11 ByteBuffer (java.nio.ByteBuffer)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)5 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)5 Cleanup (lombok.Cleanup)5 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)4 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)3 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)3 SegmentOutputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl)3 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SegmentInputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl)2 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)2 Controller (io.pravega.client.stream.impl.Controller)2 PendingEvent (io.pravega.client.stream.impl.PendingEvent)2 MockController (io.pravega.client.stream.mock.MockController)2