Search in sources :

Example 1 with FakeSegmentOutputStream

use of io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testGetTxnAborted.

@Test(expected = TxnFailedException.class)
public void testGetTxnAborted() throws TxnFailedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    UUID txnId = UUID.randomUUID();
    EventWriterConfig config = EventWriterConfig.builder().build();
    JavaSerializer<String> serializer = new JavaSerializer<>();
    // Setup mocks
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.getEpochSegments(scope, streamName, NameUtils.getEpoch(txnId))).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.checkTransactionStatus(Stream.of(scope, streamName), txnId)).thenReturn(CompletableFuture.completedFuture(Transaction.Status.ABORTED));
    Mockito.when(controller.pingTransaction(eq(Stream.of(scope, streamName)), eq(txnId), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txnId)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txnId), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    // Create a transactional event Writer
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    writer.beginTxn();
    // fetch the txn when it is already aborted
    Transaction<String> txn = writer.getTxn(txnId);
    txn.writeEvent("Foo");
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with FakeSegmentOutputStream

use of io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testTxnCommit.

@Test
public void testTxnCommit() throws TxnFailedException, SegmentSealedException {
    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().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    FakeSegmentOutputStream outputStream = spy(new FakeSegmentOutputStream(segment));
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(controller.commitTransaction(eq(stream), anyString(), isNull(), eq(txid))).thenReturn(CompletableFuture.completedFuture(null));
    Mockito.when(controller.pingTransaction(eq(stream), eq(txid), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    Mockito.when(controller.checkTransactionStatus(eq(stream), eq(txid))).thenReturn(CompletableFuture.completedFuture(Transaction.Status.OPEN));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    Transaction<String> txn = writer.beginTxn();
    txn.writeEvent("Foo");
    assertTrue(bad.unacked.isEmpty());
    assertEquals(1, outputStream.unacked.size());
    outputStream.unacked.get(0).getAckFuture().complete(null);
    txn.checkStatus();
    Mockito.verify(controller, Mockito.times(1)).checkTransactionStatus(eq(stream), eq(txid));
    // invoke commit of transaction.
    txn.commit();
    // verify if segments are flushed and closed.
    Mockito.verify(outputStream, Mockito.times(1)).close();
    Mockito.verify(controller, Mockito.times(1)).commitTransaction(eq(stream), anyString(), isNull(), eq(txid));
    assertTrue(bad.unacked.isEmpty());
    assertTrue(outputStream.unacked.isEmpty());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with FakeSegmentOutputStream

use of io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testGetTxn.

@Test
public void testGetTxn() throws TxnFailedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    UUID txnId = UUID.randomUUID();
    EventWriterConfig config = EventWriterConfig.builder().build();
    JavaSerializer<String> serializer = new JavaSerializer<>();
    // Setup mocks
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.getEpochSegments(scope, streamName, NameUtils.getEpoch(txnId))).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.checkTransactionStatus(Stream.of(scope, streamName), txnId)).thenReturn(CompletableFuture.completedFuture(Transaction.Status.OPEN));
    Mockito.when(controller.pingTransaction(eq(Stream.of(scope, streamName)), eq(txnId), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txnId)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txnId), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    // Create a transactional event Writer
    TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    writer.beginTxn();
    writer.close();
    // Create a new transactional eventWriter
    @Cleanup TransactionalEventStreamWriter<String> writer1 = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, EventWriterConfig.builder().transactionTimeoutTime(10000).build(), executorService());
    Transaction<String> txn = writer1.getTxn(txnId);
    txn.writeEvent("Foo");
    assertTrue(bad.unacked.isEmpty());
    assertEquals(1, outputStream.unacked.size());
    outputStream.unacked.get(0).getAckFuture().complete(null);
    txn.flush();
    assertTrue(bad.unacked.isEmpty());
    assertTrue(outputStream.unacked.isEmpty());
    // verify pings was invoked for the transaction with the specified transactionTimeout.
    verify(controller, timeout(10_000).atLeastOnce()).pingTransaction(eq(stream), eq(txnId), eq(10000L));
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with FakeSegmentOutputStream

use of io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testTxnFailed.

@Test
public void testTxnFailed() {
    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().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(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    Transaction<String> txn = writer.beginTxn();
    outputStream.invokeSealedCallBack();
    try {
        txn.writeEvent("Foo");
    } catch (TxnFailedException e) {
    // Expected
    }
    assertTrue(bad.unacked.isEmpty());
    assertEquals(1, outputStream.unacked.size());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) TxnFailedException(io.pravega.client.stream.TxnFailedException) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with FakeSegmentOutputStream

use of io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testTxnAbort.

@Test
public void testTxnAbort() throws TxnFailedException, SegmentSealedException {
    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().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    FakeSegmentOutputStream outputStream = spy(new FakeSegmentOutputStream(segment));
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(controller.pingTransaction(eq(stream), eq(txid), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    Mockito.when(controller.abortTransaction(eq(stream), eq(txid))).thenReturn(CompletableFuture.completedFuture(null));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    Transaction<String> txn = writer.beginTxn();
    txn.writeEvent("Foo");
    assertTrue(bad.unacked.isEmpty());
    assertEquals(1, outputStream.unacked.size());
    outputStream.unacked.get(0).getAckFuture().complete(null);
    // invoke commit of transaction.
    txn.abort();
    // verify if segments are flushed and closed.
    Mockito.verify(outputStream, Mockito.times(1)).close();
    Mockito.verify(controller, Mockito.times(1)).abortTransaction(eq(stream), eq(txid));
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Controller (io.pravega.client.control.impl.Controller)7 Segment (io.pravega.client.segment.impl.Segment)7 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)7 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)7 FakeSegmentOutputStream (io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream)7 UUID (java.util.UUID)7 Cleanup (lombok.Cleanup)7 Test (org.junit.Test)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 TxnFailedException (io.pravega.client.stream.TxnFailedException)2 StatusRuntimeException (io.grpc.StatusRuntimeException)1 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)1 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1