gnodet commented on code in PR #22311: URL: https://github.com/apache/camel/pull/22311#discussion_r3005423371
########## core/camel-support/src/test/java/org/apache/camel/support/MemoryIdempotentRepositoryTest.java: ########## @@ -0,0 +1,49 @@ +/* + * 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.camel.support; + +import java.io.IOException; + +import org.apache.camel.spi.IdempotentRepository; +import org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MemoryIdempotentRepositoryTest { + + @Test + void repositoryEvictsOldestEntryWhenRepositoryIsFull() throws IOException { + final int cacheSize = 5; + final int entriesNotFittingInRepository = 4; + try (IdempotentRepository repository = MemoryIdempotentRepository.memoryIdempotentRepositoryInsertionOrder( + cacheSize)) { + + for (int i = 0; i < cacheSize + entriesNotFittingInRepository; i++) { + repository.add(String.valueOf(i)); + } + + for (int i = entriesNotFittingInRepository; i < cacheSize + entriesNotFittingInRepository; i++) { + assertTrue(repository.contains(String.valueOf(i)), "Repository should contain entry " + i); + } + for (int i = 0; i < cacheSize - entriesNotFittingInRepository; i++) { + assertFalse(repository.contains(String.valueOf(i)), "Repository should not contain entry " + i); + } Review Comment: The loop condition `i < cacheSize - entriesNotFittingInRepository` equals `i < 1`, so this only checks that entry "0" was evicted. It should verify all 4 evicted entries (0, 1, 2, 3) are absent. ```suggestion for (int i = 0; i < entriesNotFittingInRepository; i++) { ``` ########## core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java: ########## @@ -77,6 +79,21 @@ public static IdempotentRepository memoryIdempotentRepository(int cacheSize) { return answer; } + /** + * Creates a new memory based repository using a {@link java.util.LinkedHashMap} as its store, with the given + * maximum capacity. When a new entry is added and the store has reached its maximum capacity, the oldest entry is + * removed. + */ + public static IdempotentRepository memoryIdempotentRepositoryInsertionOrder(int cacheSize) { + LinkedHashMap<String, Object> map = new LinkedHashMap<>() { + @Override + protected boolean removeEldestEntry(Entry<String, Object> eldest) { + return size() > cacheSize; + } + }; Review Comment: This factory method passes the map to the `MemoryIdempotentRepository(Map)` constructor, which does not set the `cacheSize` field. As a result, `getMaxCacheSize()` (a JMX-exposed `@ManagedAttribute`) will return 0. Consider setting it: ```java public static IdempotentRepository memoryIdempotentRepositoryInsertionOrder(int cacheSize) { LinkedHashMap<String, Object> map = new LinkedHashMap<>() { @Override protected boolean removeEldestEntry(Entry<String, Object> eldest) { return size() > cacheSize; } }; MemoryIdempotentRepository answer = new MemoryIdempotentRepository(map); answer.setCacheSize(cacheSize); return answer; } ``` -- 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]
