Samrat002 commented on code in PR #6: URL: https://github.com/apache/flink-connector-redis-streams/pull/6#discussion_r3178664078
########## flink-connector-redis-streams/src/test/java/org/apache/flink/connector/redis/streams/source/reader/split/RedisStreamsSplitReaderClusterTest.java: ########## @@ -0,0 +1,275 @@ +/* + * 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.connector.redis.streams.source.reader.split; + +import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; +import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition; +import org.apache.flink.connector.redis.streams.source.config.RedisStreamsSourceConfig; +import org.apache.flink.connector.redis.streams.source.config.StartupMode; +import org.apache.flink.connector.redis.streams.source.split.RedisStreamsSourceSplit; + +import io.lettuce.core.RedisURI; +import io.lettuce.core.StreamMessage; +import io.lettuce.core.cluster.RedisClusterClient; +import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; +import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands; +import io.lettuce.core.models.stream.PendingMessages; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link RedisStreamsSplitReader} against a Redis Cluster. + * + * <p>The local Docker daemon API is sometimes too old for the testcontainers version bundled + * here, so the suite supports two backends: + * + * <ul> + * <li>{@code FLINK_REDIS_CLUSTER_NODES=host1:port1,host2:port2,...} env var pointing at an + * externally-managed cluster (e.g. {@code docker run -p 7000-7005:7000-7005 + * grokzen/redis-cluster:7.0.10}). + * <li>Otherwise, {@code grokzen/redis-cluster:7.0.10} via testcontainers — skipped cleanly if + * Docker is unavailable or rejected by the docker-java client. + * </ul> + */ +class RedisStreamsSplitReaderClusterTest { + + private static final String CLUSTER_IMAGE = "grokzen/redis-cluster:7.0.10"; + private static final String CONSUMER_GROUP = "cg"; + private static final String CONSUMER_NAME = "consumer"; + + /** Stream key wrapped in a hash tag so all keys hash to the same slot for deterministic tests. */ + private static final String STREAM = "{tag}-stream-1"; + + private static final String EXTERNAL_CLUSTER = System.getenv("FLINK_REDIS_CLUSTER_NODES"); + + private static GenericContainer<?> CLUSTER; + private static List<String> seedNodes; + + private RedisClusterClient client; + private StatefulRedisClusterConnection<String, String> connection; + private RedisAdvancedClusterCommands<String, String> commands; + + private RedisStreamsSplitReader currentReader; + + @BeforeAll + static void startBackend() { + if (EXTERNAL_CLUSTER != null && !EXTERNAL_CLUSTER.isEmpty()) { + seedNodes = Arrays.asList(EXTERNAL_CLUSTER.split(",")); + return; + } + try { + CLUSTER = + new GenericContainer<>(CLUSTER_IMAGE) + .withEnv("IP", "0.0.0.0") + .withEnv("INITIAL_PORT", "7000") + .withEnv("MASTERS", "3") + .withEnv("SLAVES_PER_MASTER", "0") + .withExposedPorts(7000, 7001, 7002, 7003, 7004, 7005); + CLUSTER.start(); + seedNodes = new ArrayList<>(); + for (int port : new int[] {7000, 7001, 7002}) { + seedNodes.add(CLUSTER.getHost() + ":" + CLUSTER.getMappedPort(port)); + } + } catch (Throwable t) { + CLUSTER = null; + Assumptions.abort( + "No Redis Cluster backend available — set FLINK_REDIS_CLUSTER_NODES or run a " + + "modern Docker daemon. Cause: " + + t.getMessage()); + } + } + + @AfterAll + static void stopBackend() { + if (CLUSTER != null) { + CLUSTER.stop(); + } + } + + @BeforeEach + void openConnection() { + List<RedisURI> uris = + seedNodes.stream() + .map( + hp -> { + int colon = hp.lastIndexOf(':'); + return RedisURI.builder() + .withHost(hp.substring(0, colon)) + .withPort(Integer.parseInt(hp.substring(colon + 1))) + .withTimeout(Duration.ofSeconds(5)) + .build(); + }) + .collect(Collectors.toList()); + client = RedisClusterClient.create(uris); + connection = client.connect(); + commands = connection.sync(); + // Best-effort cleanup: delete the test stream and its consumer group on each test start. + try { + commands.del(STREAM); + } catch (Exception ignored) { + // ok if the key does not exist + } + } + + @AfterEach + void cleanupReader() throws Exception { + if (currentReader != null) { + try { + currentReader.close(); + } catch (Exception ignored) { + // best-effort + } + currentReader = null; + } + if (connection != null) { + connection.close(); + } + if (client != null) { + client.shutdown(); + } + } + + private RedisStreamsSourceConfig.Builder cfg() { + return RedisStreamsSourceConfig.builder() + .setClusterNodes(seedNodes) + .setConsumerGroup(CONSUMER_GROUP) + .setConsumerName(CONSUMER_NAME) + .setStreamKeys(List.of(STREAM)) + .setStartupMode(StartupMode.EARLIEST) Review Comment: Added -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
