Search in sources :

Example 1 with MockStreamManager

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

the class ReadTest method readThroughStreamClient.

@Test(timeout = 10000)
public void readThroughStreamClient() throws ReinitializationRequiredException {
    String endpoint = "localhost";
    String streamName = "readThroughStreamClient";
    String readerName = "reader";
    String readerGroup = "readThroughStreamClient-group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    String scope = "Scope1";
    StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
    TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, NoOpScheduledExecutor.get());
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    @Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().stream(Stream.of(scope, streamName)).disableAutomaticCheckpoints().build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, null);
    streamManager.createReaderGroup(readerGroup, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    producer.writeEvent(testString);
    producer.flush();
    @Cleanup EventStreamReader<String> reader = clientFactory.createReader(readerName, readerGroup, serializer, ReaderConfig.builder().build());
    String read = reader.readNextEvent(5000).getEvent();
    assertEquals(testString, read);
}
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) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 2 with MockStreamManager

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

the class CheckpointTest method testCheckpointAndRestore.

@Test(timeout = 20000)
public void testCheckpointAndRestore() throws ReinitializationRequiredException, InterruptedException, ExecutionException, TimeoutException {
    String endpoint = "localhost";
    String streamName = "testCheckpointAndRestore";
    String readerName = "reader";
    String readerGroupName = "testCheckpointAndRestore-group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    String scope = "Scope1";
    StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
    TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    @Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
    streamManager.createReaderGroup(readerGroupName, groupConfig);
    @Cleanup ReaderGroup readerGroup = streamManager.getReaderGroup(readerGroupName);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.flush();
    AtomicLong clock = new AtomicLong();
    @Cleanup EventStreamReader<String> reader = clientFactory.createReader(readerName, readerGroupName, serializer, ReaderConfig.builder().build(), clock::get, clock::get);
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    EventRead<String> read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    @Cleanup("shutdown") final InlineExecutor backgroundExecutor = new InlineExecutor();
    CompletableFuture<Checkpoint> checkpoint = readerGroup.initiateCheckpoint("Checkpoint", backgroundExecutor);
    assertFalse(checkpoint.isDone());
    read = reader.readNextEvent(60000);
    assertTrue(read.isCheckpoint());
    assertEquals("Checkpoint", read.getCheckpointName());
    assertNull(read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    Checkpoint cpResult = checkpoint.get(5, TimeUnit.SECONDS);
    assertTrue(checkpoint.isDone());
    assertEquals("Checkpoint", cpResult.getName());
    read = reader.readNextEvent(100);
    assertNull(read.getEvent());
    assertFalse(read.isCheckpoint());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(cpResult).disableAutomaticCheckpoints().build());
    try {
        reader.readNextEvent(60000);
        fail();
    } catch (ReinitializationRequiredException e) {
    // Expected
    }
    reader.close();
    reader = clientFactory.createReader(readerName, readerGroupName, serializer, ReaderConfig.builder().build());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(100);
    assertNull(read.getEvent());
    assertFalse(read.isCheckpoint());
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroup(io.pravega.client.stream.ReaderGroup) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Checkpoint(io.pravega.client.stream.Checkpoint) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) Checkpoint(io.pravega.client.stream.Checkpoint) InlineExecutor(io.pravega.test.common.InlineExecutor) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 3 with MockStreamManager

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

the class IdleSegmentTest method testByteBufferEventsWithIdleSegments.

@Test(timeout = 5000)
public void testByteBufferEventsWithIdleSegments() throws ReinitializationRequiredException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerName = "reader";
    String readerGroup = "group";
    int port = TestUtils.getAvailableListenPort();
    ByteBuffer testPayload = ByteBuffer.allocate(100);
    String scope = "Scope1";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    TableStore tableStore = serviceBuilder.createTableStoreService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, serviceBuilder.getLowPriorityExecutor());
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    @Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().stream(Stream.of(scope, streamName)).disableAutomaticCheckpoints().build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(20)).build());
    streamManager.createReaderGroup(readerGroup, groupConfig);
    Serializer<ByteBuffer> serializer = new ByteBufferSerializer();
    @Cleanup EventStreamWriter<ByteBuffer> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    List<CompletableFuture<Void>> results = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        results.add(producer.writeEvent("FixedRoutingKey", testPayload));
        System.out.println("Writing event " + i);
    }
    producer.flush();
    @Cleanup EventStreamReader<ByteBuffer> reader = clientFactory.createReader(readerName, readerGroup, serializer, ReaderConfig.builder().build());
    for (int i = 0; i < 10; i++) {
        ByteBuffer read = reader.readNextEvent(10000).getEvent();
        assertEquals(testPayload, read);
    }
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ArrayList(java.util.ArrayList) ByteBufferSerializer(io.pravega.client.stream.impl.ByteBufferSerializer) ByteBuffer(java.nio.ByteBuffer) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) CompletableFuture(java.util.concurrent.CompletableFuture) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 4 with MockStreamManager

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

the class ReadFromDeletedStreamTest method testDeletedAndRecreatedStream.

@Test(timeout = 30000)
public void testDeletedAndRecreatedStream() throws Exception {
    @Cleanup MockStreamManager streamManager = new MockStreamManager("test", "localhost", Config.SERVICE_PORT);
    @Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    TableStore tableStore = serviceBuilder.createTableStoreService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, false, "localhost", 12345, store, tableStore, SegmentStatsRecorder.noOp(), TableSegmentStatsRecorder.noOp(), null, null, null, true, serviceBuilder.getLowPriorityExecutor(), SecurityConfigDefaults.TLS_PROTOCOL_VERSION);
    server.startListening();
    streamManager.createScope("test");
    streamManager.createStream("test", "test", CONFIG);
    @Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
    // Mocking pravega service by putting scale up and scale down requests for the stream
    @Cleanup EventStreamWriter<String> test = clientFactory.createEventWriter("test", new JavaSerializer<>(), EventWriterConfig.builder().build());
    test.writeEvent("0", "foo").get();
    streamManager.deleteStream("test", "test");
    AssertExtensions.assertThrows(NoSuchSegmentException.class, () -> test.writeEvent("0", "foo").get());
}
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) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) Test(org.junit.Test)

Example 5 with MockStreamManager

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

the class TransactionTest method testTransactionalWritesOrderedCorrectly.

@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testTransactionalWritesOrderedCorrectly() throws TxnFailedException, ReinitializationRequiredException {
    int readTimeout = 5000;
    String readerName = "reader";
    String groupName = "testTransactionalWritesOrderedCorrectly-group";
    String endpoint = "localhost";
    String streamName = "testTransactionalWritesOrderedCorrectly";
    int port = TestUtils.getAvailableListenPort();
    String txnEvent = "TXN Event\n";
    String nonTxEvent = "Non-TX Event\n";
    String routingKey = "RoutingKey";
    StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
    TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
    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)).disableAutomaticCheckpoints().build());
    @Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
    EventWriterConfig eventWriterConfig = EventWriterConfig.builder().transactionTimeoutTime(60000).build();
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
    @Cleanup TransactionalEventStreamWriter<String> txnProducer = clientFactory.createTransactionalEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
    producer.writeEvent(routingKey, nonTxEvent);
    Transaction<String> transaction = txnProducer.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));
    @Cleanup 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 : Serializable(java.io.Serializable) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Aggregations

MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)27 Cleanup (lombok.Cleanup)26 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)25 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)24 TableStore (io.pravega.segmentstore.contracts.tables.TableStore)24 Test (org.junit.Test)22 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)21 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)13 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)10 ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 Checkpoint (io.pravega.client.stream.Checkpoint)4 ReaderGroup (io.pravega.client.stream.ReaderGroup)4 InlineExecutor (io.pravega.test.common.InlineExecutor)4 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 ByteBufferSerializer (io.pravega.client.stream.impl.ByteBufferSerializer)2 Serializable (java.io.Serializable)2 ByteBuffer (java.nio.ByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 lombok.val (lombok.val)2