use of io.pravega.client.stream.mock.MockClientFactory 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);
}
}
use of io.pravega.client.stream.mock.MockClientFactory 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());
}
use of io.pravega.client.stream.mock.MockClientFactory 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());
}
use of io.pravega.client.stream.mock.MockClientFactory in project pravega by pravega.
the class TransactionTest method testDrop.
@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testDrop() throws TxnFailedException, ReinitializationRequiredException {
String endpoint = "localhost";
String groupName = "testDrop-group";
String streamName = "testDrop";
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 TransactionalEventStreamWriter<String> txnProducer = clientFactory.createTransactionalEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
Transaction<String> transaction = txnProducer.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());
@Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
producer.writeEvent(routingKey, nonTxEvent);
producer.flush();
assertEquals(nonTxEvent, consumer.readNextEvent(1500).getEvent());
}
use of io.pravega.client.stream.mock.MockClientFactory 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, mock(TableStore.class), serviceBuilder.getLowPriorityExecutor());
server.startListening();
@Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
@Cleanup 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);
}
Aggregations