Search in sources :

Example 11 with SegmentOutputStreamFactory

use of io.pravega.client.segment.impl.SegmentOutputStreamFactory 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 12 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testSegmentSealedInFlush.

@Test
public void testSegmentSealedInFlush() throws EndOfSegmentException, SegmentTruncatedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    Segment segment2 = new Segment(scope, streamName, 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment1);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream.callBackForSealed = i.getArgument(1);
        return outputStream;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
    MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
    outputStream.invokeSealedCallBack();
    writer.flush();
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertTrue(outputStream2.fetchCurrentSegmentLength() > 0);
    assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
Also used : MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Cleanup(lombok.Cleanup) 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) Test(org.junit.Test)

Example 13 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testTxn.

@Test
public void testTxn() throws TxnFailedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    UUID txid = UUID.randomUUID();
    EventWriterConfig config = EventWriterConfig.builder().transactionTimeoutTime(0).transactionTimeoutScaleGracePeriod(0).build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(stream, 0, 0)).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    Transaction<String> txn = writer.beginTxn();
    txn.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(bad.getUnackedEventsOnSeal().isEmpty());
    assertEquals(1, outputStream.getUnackedEventsOnSeal().size());
    outputStream.getUnackedEventsOnSeal().get(0).getAckFuture().complete(true);
    txn.flush();
    assertTrue(bad.getUnackedEventsOnSeal().isEmpty());
    assertTrue(outputStream.getUnackedEventsOnSeal().isEmpty());
}
Also used : Cleanup(lombok.Cleanup) 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) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testWrite.

@Test
public void testWrite() {
    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));
    MockSegmentIoStreams outputStream = new MockSegmentIoStreams(segment);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
    EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, new JavaSerializer<>(), config, new InlineExecutor());
    writer.writeEvent("Foo");
    writer.writeEvent("Bar");
    writer.close();
    try {
        writer.writeEvent("fail");
        fail();
    } catch (IllegalStateException e) {
    // expected.
    }
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 15 with SegmentOutputStreamFactory

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

the class SegmentSelectorTest method testSameRoutingKey.

@Test
public void testSameRoutingKey() {
    Controller controller = Mockito.mock(Controller.class);
    SegmentOutputStreamFactory factory = Mockito.mock(SegmentOutputStreamFactory.class);
    SegmentSelector selector = new SegmentSelector(new StreamImpl(scope, streamName), controller, factory, config);
    TreeMap<Double, Segment> segments = new TreeMap<>();
    segments.put(0.25, new Segment(scope, streamName, 0));
    segments.put(0.5, new Segment(scope, streamName, 1));
    segments.put(0.75, new Segment(scope, streamName, 2));
    segments.put(1.0, new Segment(scope, streamName, 3));
    StreamSegments streamSegments = new StreamSegments(segments, "");
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(CompletableFuture.completedFuture(streamSegments));
    selector.refreshSegmentEventWriters(segmentSealedCallback);
    int[] counts = new int[4];
    Arrays.fill(counts, 0);
    for (int i = 0; i < 20; i++) {
        Segment segment = selector.getSegmentForEvent("Foo");
        assertNotNull(segment);
        counts[segment.getSegmentNumber()]++;
    }
    assertArrayEquals(new int[] { 20, 0, 0, 0 }, counts);
}
Also used : SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) TreeMap(java.util.TreeMap) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)15 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)15 Test (org.junit.Test)15 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)12 InlineExecutor (io.pravega.test.common.InlineExecutor)12 Cleanup (lombok.Cleanup)10 MockSegmentIoStreams (io.pravega.client.stream.mock.MockSegmentIoStreams)5 TreeMap (java.util.TreeMap)3 UUID (java.util.UUID)2 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)1 TxnFailedException (io.pravega.client.stream.TxnFailedException)1