Search in sources :

Example 1 with MockClientFactory

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

the class SynchronizerTest method testReturnValue.

@Test(timeout = 20000)
public void testReturnValue() {
    String streamName = "streamName";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactory.getController());
    StateSynchronizer<RevisionedImpl> sync = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    StateSynchronizer<RevisionedImpl> syncA = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    StateSynchronizer<RevisionedImpl> syncB = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    syncA.initialize(new RegularUpdate("Foo"));
    AtomicInteger callCount = new AtomicInteger(0);
    String previous = syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bar"));
        return state.getValue();
    });
    assertEquals(previous, "Foo");
    assertEquals(1, callCount.get());
    assertEquals("Foo", syncA.getState().value);
    previous = syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Baz"));
        return state.getValue();
    });
    assertEquals(previous, "Bar");
    assertEquals(3, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncB.fetchUpdates();
    assertEquals("Baz", syncA.getState().value);
    previous = syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bat"));
        return state.getValue();
    });
    assertEquals(previous, "Baz");
    assertEquals(4, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncA.fetchUpdates();
    assertEquals("Bat", syncA.getState().value);
}
Also used : AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) StreamSegments(io.pravega.client.stream.impl.StreamSegments) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URI(java.net.URI) InitialUpdate(io.pravega.client.state.InitialUpdate) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Serializable(java.io.Serializable) CountDownLatch(java.util.concurrent.CountDownLatch) SetSynchronizer(io.pravega.client.state.examples.SetSynchronizer) Assert.assertFalse(org.junit.Assert.assertFalse) Entry(java.util.Map.Entry) SegmentAttribute(io.pravega.client.segment.impl.SegmentAttribute) Controller(io.pravega.client.control.impl.Controller) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Mockito.mock(org.mockito.Mockito.mock) StateSynchronizer(io.pravega.client.state.StateSynchronizer) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Segment(io.pravega.client.segment.impl.Segment) TruncatedDataException(io.pravega.client.stream.TruncatedDataException) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ByteArraySerializer(io.pravega.client.stream.impl.ByteArraySerializer) Update(io.pravega.client.state.Update) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ReusableLatch(io.pravega.common.util.ReusableLatch) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Revisioned(io.pravega.client.state.Revisioned) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) Assert.assertNull(org.junit.Assert.assertNull) TreeMap(java.util.TreeMap) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) Data(lombok.Data) Revision(io.pravega.client.state.Revision) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 2 with MockClientFactory

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

the class SynchronizerTest method testCompactWithTruncation.

@Test(timeout = 20000)
public void testCompactWithTruncation() {
    String streamName = "streamName";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactoryA = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactoryA.getController());
    @Cleanup MockClientFactory clientFactoryB = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactoryB.getController());
    StateSynchronizer<RevisionedImpl> syncA = clientFactoryA.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    StateSynchronizer<RevisionedImpl> syncB = clientFactoryB.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    assertEquals(0, syncA.bytesWrittenSinceCompaction());
    assertEquals(0, syncB.bytesWrittenSinceCompaction());
    AtomicInteger callCount = new AtomicInteger(0);
    syncA.initialize(new RegularUpdate("a"));
    syncB.initialize(new RegularUpdate("b"));
    assertEquals("a", syncA.getState().getValue());
    assertEquals("a", syncB.getState().getValue());
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("b"));
    });
    assertEquals(1, callCount.get());
    assertEquals("b", syncA.getState().getValue());
    syncB.fetchUpdates();
    assertEquals("b", syncB.getState().getValue());
    long size = syncA.bytesWrittenSinceCompaction();
    assertTrue(size > 0);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("c"));
    });
    assertEquals(2, callCount.get());
    assertEquals("c", syncA.getState().getValue());
    syncB.fetchUpdates();
    assertEquals("c", syncB.getState().getValue());
    assertTrue(syncA.bytesWrittenSinceCompaction() > size);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("d"));
    });
    assertEquals(3, callCount.get());
    assertEquals("d", syncA.getState().getValue());
    assertEquals("c", syncB.getState().getValue());
    syncB.fetchUpdates();
    assertEquals("d", syncB.getState().getValue());
    assertTrue(syncA.bytesWrittenSinceCompaction() > size);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("e"));
    });
    assertEquals(4, callCount.get());
    assertEquals("e", syncA.getState().getValue());
    syncB.fetchUpdates();
    assertEquals("e", syncB.getState().getValue());
    assertTrue(syncA.bytesWrittenSinceCompaction() > size);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("f"));
    });
    assertEquals(5, callCount.get());
    assertEquals("f", syncA.getState().getValue());
    assertTrue(syncA.bytesWrittenSinceCompaction() > size);
    assertEquals("e", syncB.getState().getValue());
    syncB.compact(state -> {
        callCount.incrementAndGet();
        return new RegularUpdate(state.getValue());
    });
    assertEquals(7, callCount.get());
    assertEquals(0, syncB.bytesWrittenSinceCompaction());
    assertEquals("f", syncA.getState().getValue());
    assertEquals("f", syncB.getState().getValue());
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("g"));
    });
    assertEquals(9, callCount.get());
    assertEquals("g", syncA.getState().getValue());
    assertEquals(syncA.bytesWrittenSinceCompaction(), size);
    syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("h"));
    });
    assertEquals(10, callCount.get());
    assertEquals("h", syncA.getState().getValue());
    syncA.compact(state -> {
        callCount.incrementAndGet();
        return new RegularUpdate("h");
    });
    assertEquals(11, callCount.get());
    assertEquals("h", syncA.getState().getValue());
    syncB.fetchUpdates();
    assertEquals("h", syncB.getState().getValue());
    assertEquals(0, syncA.bytesWrittenSinceCompaction());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 3 with MockClientFactory

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

the class SynchronizerTest method testSetOperations.

@Test(timeout = 20000)
public void testSetOperations() {
    String streamName = "testCompactionShrinksSet";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactory.getController());
    SetSynchronizer<String> set = SetSynchronizer.createNewSet(streamName, clientFactory);
    SetSynchronizer<String> set2 = SetSynchronizer.createNewSet(streamName, clientFactory);
    assertEquals(0, set.getCurrentSize());
    set.add("Foo");
    set2.add("Bar");
    set.update();
    assertEquals(2, set.getCurrentSize());
    set.clear();
    assertEquals(0, set.getCurrentSize());
    set.add("Baz");
    assertEquals(2, set2.getCurrentSize());
    set2.remove("Bar");
    assertEquals(1, set2.getCurrentSize());
    set2.remove("Baz");
    assertEquals(0, set2.getCurrentSize());
    set.update();
    assertEquals(0, set.getCurrentSize());
    SetSynchronizer<String> set3 = SetSynchronizer.createNewSet(streamName, clientFactory);
    set3.update();
    assertEquals(0, set3.getCurrentSize());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 4 with MockClientFactory

use of io.pravega.client.stream.mock.MockClientFactory 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 5 with MockClientFactory

use of io.pravega.client.stream.mock.MockClientFactory 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)

Aggregations

MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)33 Cleanup (lombok.Cleanup)32 Test (org.junit.Test)28 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)24 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)24 TableStore (io.pravega.segmentstore.contracts.tables.TableStore)23 MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)21 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)15 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)12 ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)10 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 InlineExecutor (io.pravega.test.common.InlineExecutor)6 Controller (io.pravega.client.control.impl.Controller)5 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ClientConfig (io.pravega.client.ClientConfig)4 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)4 Checkpoint (io.pravega.client.stream.Checkpoint)4