alberttwong commented on code in PR #11919:
URL: https://github.com/apache/nifi/pull/11919#discussion_r4617162375


##########
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. The behavior-focused wording should be:
   
   `// A successful replacement with TTL disabled removes any existing 
expiration.`
   
   The positive/zero-TTL and conflict-expiration assertions passed in the live 
Software and Cloud validation. The source-comment wording change itself is 
still pending and remains listed in the PR's review follow-ups.



-- 
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]

Reply via email to