Search in sources :

Example 6 with MockStreamManager

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

the class TransactionTest method testDrop.

@Test
public void testDrop() throws TxnFailedException, ReinitializationRequiredException {
    String endpoint = "localhost";
    String groupName = "group";
    String streamName = "abc";
    int port = TestUtils.getAvailableListenPort();
    String txnEvent = "TXN Event\n";
    String nonTxEvent = "Non-TX Event\n";
    String routingKey = "RoutingKey";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager("scope", endpoint, port);
    streamManager.createScope("scope");
    streamManager.createStream("scope", streamName, StreamConfiguration.builder().build());
    streamManager.createReaderGroup(groupName, ReaderGroupConfig.builder().stream(Stream.of("scope", streamName)).build());
    MockClientFactory clientFactory = streamManager.getClientFactory();
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().transactionTimeoutTime(60000).transactionTimeoutScaleGracePeriod(60000).build());
    Transaction<String> transaction = producer.beginTxn();
    transaction.writeEvent(routingKey, txnEvent);
    transaction.flush();
    transaction.abort();
    transaction.abort();
    AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.writeEvent(routingKey, txnEvent));
    AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.commit());
    @Cleanup EventStreamReader<Serializable> consumer = streamManager.getClientFactory().createReader("reader", groupName, new JavaSerializer<>(), ReaderConfig.builder().build());
    producer.writeEvent(routingKey, nonTxEvent);
    producer.flush();
    assertEquals(nonTxEvent, consumer.readNextEvent(1500).getEvent());
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Serializable(java.io.Serializable) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 7 with MockStreamManager

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

the class TransactionTest method testTransactionalWritesOrderedCorrectly.

@Test
public void testTransactionalWritesOrderedCorrectly() throws TxnFailedException, ReinitializationRequiredException {
    int readTimeout = 5000;
    String readerName = "reader";
    String groupName = "group";
    String endpoint = "localhost";
    String streamName = "abc";
    int port = TestUtils.getAvailableListenPort();
    String txnEvent = "TXN Event\n";
    String nonTxEvent = "Non-TX Event\n";
    String routingKey = "RoutingKey";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager("scope", endpoint, port);
    streamManager.createScope("scope");
    streamManager.createStream("scope", streamName, StreamConfiguration.builder().build());
    streamManager.createReaderGroup(groupName, ReaderGroupConfig.builder().stream(Stream.of("scope", streamName)).build());
    MockClientFactory clientFactory = streamManager.getClientFactory();
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().transactionTimeoutTime(60000).transactionTimeoutScaleGracePeriod(60000).build());
    producer.writeEvent(routingKey, nonTxEvent);
    Transaction<String> transaction = producer.beginTxn();
    producer.writeEvent(routingKey, nonTxEvent);
    transaction.writeEvent(routingKey, txnEvent);
    producer.writeEvent(routingKey, nonTxEvent);
    transaction.writeEvent(routingKey, txnEvent);
    producer.flush();
    producer.writeEvent(routingKey, nonTxEvent);
    transaction.writeEvent(routingKey, txnEvent);
    producer.writeEvent(routingKey, nonTxEvent);
    transaction.writeEvent(routingKey, txnEvent);
    transaction.flush();
    producer.writeEvent(routingKey, nonTxEvent);
    transaction.writeEvent(routingKey, txnEvent);
    producer.flush();
    transaction.writeEvent(routingKey, txnEvent);
    transaction.commit();
    producer.writeEvent(routingKey, nonTxEvent);
    AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.writeEvent(routingKey, txnEvent));
    EventStreamReader<Serializable> consumer = streamManager.getClientFactory().createReader(readerName, groupName, new JavaSerializer<>(), ReaderConfig.builder().build());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
    assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Serializable(java.io.Serializable) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 8 with MockStreamManager

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

the class StartLocalService method main.

public static void main(String[] args) throws Exception {
    @Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, StartLocalService.SERVICE_PORT, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(SCOPE, "localhost", StartLocalService.SERVICE_PORT);
    streamManager.createScope(SCOPE);
    streamManager.createStream(SCOPE, STREAM_NAME, null);
    Thread.sleep(60000);
    System.exit(0);
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder)

Example 9 with MockStreamManager

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

the class InProcessMockClientAdapter method startUp.

// endregion
// region ClientAdapterBase Implementation
@Override
protected void startUp() throws Exception {
    int segmentStorePort = this.testConfig.getSegmentStorePort(0);
    this.listener = new PravegaConnectionListener(false, segmentStorePort, getStreamSegmentStore());
    this.listener.startListening();
    this.streamManager = new MockStreamManager(SCOPE, LISTENING_ADDRESS, segmentStorePort);
    this.streamManager.createScope(SCOPE);
    super.startUp();
}
Also used : MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)

Example 10 with MockStreamManager

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

the class AutoCheckpointTest method testOnlyOneOutstanding.

@Test(timeout = 30000)
public void testOnlyOneOutstanding() throws ReinitializationRequiredException, DurableDataLogException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerGroup = "group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world: ";
    String scope = "Scope1";
    @Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().automaticCheckpointIntervalMillis(1000).stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, null);
    streamManager.createReaderGroup(readerGroup, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    populateEvents(streamName, testString, clientFactory, serializer);
    AtomicLong fakeClock = new AtomicLong(0);
    @Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("reader1", readerGroup, serializer, ReaderConfig.builder().build(), () -> fakeClock.get(), () -> fakeClock.get() / NANOS_PER_SECOND);
    @Cleanup EventStreamReader<String> reader2 = clientFactory.createReader("reader2", readerGroup, serializer, ReaderConfig.builder().build(), () -> fakeClock.get(), () -> fakeClock.get() / NANOS_PER_SECOND);
    int numRead = 0;
    int checkpointCount = 0;
    while (numRead < 100) {
        fakeClock.addAndGet(NANOS_PER_SECOND);
        EventRead<String> event = reader1.readNextEvent(1000);
        if (event.isCheckpoint()) {
            checkpointCount++;
        } else {
            String message = event.getEvent();
            assertEquals(testString + numRead, message);
            numRead++;
        }
    }
    assertEquals("As there is a second reader that does not pass the checkpoint, only one should occur", 1, checkpointCount);
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Aggregations

MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)22 Cleanup (lombok.Cleanup)21 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)20 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)19 Test (org.junit.Test)17 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)16 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)9 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)7 ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Checkpoint (io.pravega.client.stream.Checkpoint)2 ReaderGroup (io.pravega.client.stream.ReaderGroup)2 InlineExecutor (io.pravega.test.common.InlineExecutor)2 Serializable (java.io.Serializable)2 NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)1 EventPointer (io.pravega.client.stream.EventPointer)1 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)1 IntegerSerializer (io.pravega.test.integration.utils.IntegerSerializer)1 TreeSet (java.util.TreeSet)1 lombok.val (lombok.val)1