alberttwong commented on code in PR #11919: URL: https://github.com/apache/nifi/pull/11919#discussion_r4617159126
########## nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisClusterDistributedMapCacheClientService.java: ########## @@ -0,0 +1,29 @@ +/* + * 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.nifi.redis.service; + +/** + * Runs the atomic cache contract against the Redis Cluster API, with all hash slots assigned to one node. + * This exercises cluster command routing without requiring fixed Docker host ports or a multi-node network. + */ +public class ITRedisClusterDistributedMapCacheClientService extends ITRedisDistributedMapCacheClientService { Review Comment: You're right. The one-node fixture exercises the Cluster connection path and single-key atomic cache contract, but it does not exercise `MOVED` or `ASK`. The simpler topology avoids the advertised-address/network setup needed for multiple containers, but leaves that coverage gap. I'll document it explicitly in the test Javadoc and PR verification notes. Multi-node routing and slot migration need dedicated coverage before claiming redirect, resharding, or failover validation. ########## nifi-extension-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java: ########## @@ -173,9 +211,133 @@ public void testReplaceCreatesWhenKeyAbsent() throws IOException { assertEquals("v1", cacheClient.get(key, STRING_SERIALIZER, STRING_DESERIALIZER)); } - private static void flushDatabase() { + @Test + public void testPutIfAbsentSetsExpirationWithoutChangingExistingExpiration() throws IOException { + assertTrue(cacheClient.putIfAbsent("ttl", "v1", STRING_SERIALIZER, STRING_SERIALIZER)); + try (RedisConnection connection = redisConnectionPool.getConnection()) { + final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8); + final long ttl = connection.keyCommands().ttl(key); + assertTrue(ttl > 0 && ttl <= 60); + connection.keyCommands().expire(key, 20); + assertFalse(cacheClient.putIfAbsent("ttl", "v2", STRING_SERIALIZER, STRING_SERIALIZER)); + assertTrue(connection.keyCommands().ttl(key) <= 20); + } + assertEquals("v1", cacheClient.get("ttl", STRING_SERIALIZER, STRING_DESERIALIZER)); + } + + @Test + public void testGetAndPutIfAbsentDoesNotRefreshExistingExpiration() throws IOException { + assertNull(cacheClient.getAndPutIfAbsent("ttl", "v1", STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER)); + try (RedisConnection connection = redisConnectionPool.getConnection()) { + final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8); + assertTrue(connection.keyCommands().ttl(key) > 0); + connection.keyCommands().expire(key, 20); + assertEquals("v1", cacheClient.getAndPutIfAbsent("ttl", "v2", STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER)); + assertTrue(connection.keyCommands().ttl(key) <= 20); + } + } + + @Test + public void testReplacePreservesExpirationOnConflictAndRefreshesOnSuccess() throws IOException { + cacheClient.put("ttl", "v1", STRING_SERIALIZER, STRING_SERIALIZER); + final AtomicCacheEntry<String, String, byte[]> entry = cacheClient.fetch("ttl", STRING_SERIALIZER, STRING_DESERIALIZER); + try (RedisConnection connection = redisConnectionPool.getConnection()) { + final byte[] key = "ttl".getBytes(StandardCharsets.UTF_8); + connection.keyCommands().expire(key, 20); + assertFalse(cacheClient.replace(new AtomicCacheEntry<>("ttl", "v2", new byte[0]), STRING_SERIALIZER, STRING_SERIALIZER)); + assertTrue(connection.keyCommands().ttl(key) <= 20); + entry.setValue("v2"); + assertTrue(cacheClient.replace(entry, STRING_SERIALIZER, STRING_SERIALIZER)); + assertTrue(connection.keyCommands().ttl(key) > 20); + } + } + + @Test + public void testBinaryAndEmptyValuesAreDistinctFromMissingKeys() throws IOException { + final Serializer<byte[]> serializer = (value, output) -> output.write(value); + final Deserializer<byte[]> deserializer = value -> value; + final byte[] key = {0, (byte) 255, 1}; + final byte[] empty = new byte[0]; + final byte[] binary = {0, (byte) 255, 2}; + assertNull(cacheClient.getAndPutIfAbsent(key, empty, serializer, serializer, deserializer)); + assertArrayEquals(empty, cacheClient.getAndPutIfAbsent(key, binary, serializer, serializer, deserializer)); + assertFalse(cacheClient.replace(new AtomicCacheEntry<>(key, binary, null), serializer, serializer)); + assertTrue(cacheClient.replace(new AtomicCacheEntry<>(key, binary, empty), serializer, serializer)); + assertArrayEquals(binary, cacheClient.fetch(key, serializer, deserializer).getRevision().orElseThrow()); + assertFalse(cacheClient.replace(new AtomicCacheEntry<>(new byte[]{3}, binary, empty), serializer, serializer)); + } + + @Test + public void testZeroTtlCreatesPersistentEntries() throws IOException { + testRunner.disableControllerService(cacheClient); + testRunner.setProperty(cacheClient, RedisUtils.TTL, "0 secs"); + testRunner.enableControllerService(cacheClient); + assertTrue(cacheClient.putIfAbsent("put", "v1", STRING_SERIALIZER, STRING_SERIALIZER)); + assertNull(cacheClient.getAndPutIfAbsent("get", "v1", STRING_SERIALIZER, STRING_SERIALIZER, STRING_DESERIALIZER)); + assertTrue(cacheClient.replace(new AtomicCacheEntry<>("replace", "v1", null), STRING_SERIALIZER, STRING_SERIALIZER)); + try (RedisConnection connection = redisConnectionPool.getConnection()) { + for (String key : List.of("put", "get", "replace")) { + assertEquals(-1L, connection.keyCommands().ttl(key.getBytes(StandardCharsets.UTF_8))); + } + // A replacement without TTL must remove an existing expiration, as SET did before this change. Review Comment: Agreed. I'll reword it to describe the contract: `// A successful replacement with TTL disabled removes any existing expiration.` -- 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]
