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);
}
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());
}
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());
}
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);
}
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());
}
Aggregations