tzulitai commented on a change in pull request #28: [FLINK-16159] [tests, build] Add verification integration test + integrate with Maven build URL: https://github.com/apache/flink-statefun/pull/28#discussion_r382886088
########## File path: statefun-integration-tests/statefun-sanity-itcase/src/test/java/org/apache/flink/statefun/itcases/sanity/SanityVerificationITCase.java ########## @@ -0,0 +1,287 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.statefun.itcases.sanity; + +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.nio.file.Paths; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.Command; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.FnAddress; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.Modify; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.Noop; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.Send; +import org.apache.flink.statefun.itcases.sanity.generated.VerificationMessages.StateSnapshot; +import org.apache.kafka.clients.consumer.Consumer; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.Producer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.serialization.Deserializer; +import org.apache.kafka.common.serialization.Serializer; +import org.junit.Rule; +import org.junit.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.images.builder.ImageFromDockerfile; + +/** + * Sanity verification integration test based on the {@link SanityVerificationModule} application. + * + * <p>The integration test setups Kafka brokers and the verification application using Docker, sends + * a few commands to Kafka to be consumed by the application, and finally verifies that outputs sent + * to Kafka from the application are correct. + */ +public class SanityVerificationITCase { + + private static final String CONFLUENT_PLATFORM_VERSION = "5.0.3"; + + private static final ImageFromDockerfile verificationAppImage = + new ImageFromDockerfile("statefun-sanity-itcase") + .withFileFromClasspath("Dockerfile", "Dockerfile") + .withFileFromPath(".", Paths.get(System.getProperty("user.dir") + "/target/")); + + @Rule public Network network = Network.newNetwork(); + + @Rule + public KafkaContainer kafka = + new KafkaContainer(CONFLUENT_PLATFORM_VERSION) + .withNetwork(network) + .withNetworkAliases("kafka-broker"); + + @Rule + public GenericContainer verificationAppMaster = + new GenericContainer(verificationAppImage) + .dependsOn(kafka) + .withNetwork(network) + .withNetworkAliases("master") + .withEnv("ROLE", "master") + .withEnv("MASTER_HOST", "master"); + + @Rule + public GenericContainer verificationAppWorker = + new GenericContainer(verificationAppImage) + .dependsOn(kafka, verificationAppMaster) + .withNetwork(network) + .withNetworkAliases("worker") + .withEnv("ROLE", "worker") + .withEnv("MASTER_HOST", "master"); + + @Test + public void run() throws Exception { + final String kafkaAddress = kafka.getBootstrapServers(); + final ExecutorService kafkaIoExecutor = Executors.newCachedThreadPool(); + + kafkaIoExecutor.submit(new ProduceCommands(kafkaAddress)); + Future<List<StateSnapshot>> stateSnapshotOutputs = + kafkaIoExecutor.submit(new ConsumeStateSnapshots(kafkaAddress)); + + assertThat( + stateSnapshotOutputs.get(1, TimeUnit.MINUTES), + hasItems( + stateSnapshot(fnAddress(0, "id-1"), 100), + stateSnapshot(fnAddress(0, "id-2"), 300), + stateSnapshot(fnAddress(1, "id-3"), 200), + stateSnapshot(fnAddress(0, "id-2"), 350))); + } + + // ================================================================================= + // Kafka IO utility classes and methods + // ================================================================================= + + private static class ProduceCommands implements Runnable { + private final String kafkaAddress; + + ProduceCommands(String kafkaAddress) { + this.kafkaAddress = kafkaAddress; + } + + @Override + public void run() { + Producer<FnAddress, Command> commandProducer = kafkaCommandProducer(kafkaAddress); + produceCommandToKafka(commandProducer, modifyAction(fnAddress(0, "id-1"), 100)); + produceCommandToKafka(commandProducer, modifyAction(fnAddress(0, "id-2"), 300)); + produceCommandToKafka(commandProducer, modifyAction(fnAddress(1, "id-3"), 200)); + produceCommandToKafka( + commandProducer, + sendAction(fnAddress(1, "id-2"), modifyAction(fnAddress(0, "id-2"), 50))); + produceCommandToKafka( + commandProducer, sendAction(fnAddress(0, "id-1"), noOpAction(fnAddress(1, "id-1")))); + commandProducer.flush(); + } + } + + private static class ConsumeStateSnapshots implements Callable<List<StateSnapshot>> { + + private final String kafkaAddress; + + ConsumeStateSnapshots(String kafkaAddress) { + this.kafkaAddress = kafkaAddress; + } + + @Override + public List<StateSnapshot> call() throws Exception { + Consumer<FnAddress, StateSnapshot> stateSnapshotConsumer = + kafkaStateSnapshotConsumer(kafkaAddress); + + final int expectedOutputs = 4; + List<StateSnapshot> responses = new ArrayList<>(expectedOutputs); + while (responses.size() < expectedOutputs) { + ConsumerRecords<FnAddress, StateSnapshot> stateSnapshots = + stateSnapshotConsumer.poll(Duration.ofMillis(100)); + for (ConsumerRecord<FnAddress, StateSnapshot> stateSnapshot : stateSnapshots) { + responses.add(stateSnapshot.value()); + } + } + + return responses; + } + } + + private static Producer<FnAddress, Command> kafkaCommandProducer(String bootstrapServers) { + Properties props = new Properties(); + props.put("bootstrap.servers", bootstrapServers); + + return new KafkaProducer<>( + props, new FnAddressSerializerDeserializer(), new CommandSerializer()); + } + + private static Consumer<FnAddress, StateSnapshot> kafkaStateSnapshotConsumer( + String bootstrapServers) { + Properties consumerProps = new Properties(); + consumerProps.setProperty("bootstrap.servers", bootstrapServers); + consumerProps.setProperty("group.id", "sanity-itcase"); + consumerProps.setProperty("auto.offset.reset", "earliest"); + + KafkaConsumer<FnAddress, StateSnapshot> consumer = + new KafkaConsumer<>( + consumerProps, new FnAddressSerializerDeserializer(), new StateSnapshotDeserializer()); + consumer.subscribe(Collections.singletonList(KafkaIO.STATE_SNAPSHOTS_TOPIC_NAME)); + + return consumer; + } + + private static void produceCommandToKafka( + Producer<FnAddress, Command> producer, Command command) { + producer.send(new ProducerRecord<>(KafkaIO.COMMAND_TOPIC_NAME, command.getTarget(), command)); + } + + // ================================================================================= + // Protobuf message building utilities + // ================================================================================= + + private static StateSnapshot stateSnapshot(FnAddress fromFnAddress, int stateSnapshotValue) { + return StateSnapshot.newBuilder().setFrom(fromFnAddress).setState(stateSnapshotValue).build(); + } + + private static Command sendAction(FnAddress targetAddress, Command commandToSend) { + final Send sendAction = Send.newBuilder().addCommandToSend(commandToSend).build(); + + return Command.newBuilder().setTarget(targetAddress).setSend(sendAction).build(); + } + + private static Command modifyAction(FnAddress targetAddress, int stateValueDelta) { + final Modify modifyAction = Modify.newBuilder().setDelta(stateValueDelta).build(); + + return Command.newBuilder().setTarget(targetAddress).setModify(modifyAction).build(); + } + + private static Command noOpAction(FnAddress targetAddress) { + return Command.newBuilder().setTarget(targetAddress).setNoop(Noop.getDefaultInstance()).build(); + } + + private static FnAddress fnAddress(int typeIndex, String fnId) { + if (typeIndex > Constants.FUNCTION_TYPES.length - 1) { + throw new IndexOutOfBoundsException( + "Type index is out of bounds. Max index: " + (Constants.FUNCTION_TYPES.length - 1)); + } + return FnAddress.newBuilder().setType(typeIndex).setId(fnId).build(); + } + + // ================================================================================= + // Kafka ingress / egress serde + // ================================================================================= + + public static final class FnAddressSerializerDeserializer Review comment: Turns out there is a nice way to do that! See fdcdef1c1a4697bbecb0d9949d002dbe76551f44 for the fix. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services