Fix `testPurgeCacheEntriesForDatabase` on Windows (#129648)

Our hypothesis is that the path is not round-tripping on Windows. This
fixes the test by always using the canonical string for the path, when
inserting and when purging.

Fixes #129635
This commit is contained in:
Pete Gillin 2025-06-19 09:06:09 +01:00 committed by GitHub
parent 1bcd9b4170
commit 8555c16290
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 11 deletions

View File

@ -17,6 +17,7 @@ import org.elasticsearch.core.TimeValue;
import org.elasticsearch.ingest.geoip.stats.CacheStats;
import org.elasticsearch.test.ESTestCase;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
@ -140,23 +141,25 @@ public class GeoIpCacheTests extends ESTestCase {
GeoIpCache cache = new GeoIpCache(100);
ProjectId projectId1 = randomUniqueProjectId();
ProjectId projectId2 = randomUniqueProjectId();
String databasePath1 = "path/to/db1";
String databasePath2 = "path/to/db2";
// Turn the path strings into Paths to ensure that we always use the canonical string representation (this string literal does not
// round-trip when converting to a Path and back again on Windows):
Path databasePath1 = PathUtils.get("path/to/db1");
Path databasePath2 = PathUtils.get("path/to/db2");
String ip1 = "127.0.0.1";
String ip2 = "127.0.0.2";
AbstractResponse response = mock(AbstractResponse.class);
cache.putIfAbsent(projectId1, ip1, databasePath1, ip -> response); // cache miss
cache.putIfAbsent(projectId1, ip2, databasePath1, ip -> response); // cache miss
cache.putIfAbsent(projectId2, ip1, databasePath1, ip -> response); // cache miss
cache.putIfAbsent(projectId1, ip1, databasePath2, ip -> response); // cache miss
cache.purgeCacheEntriesForDatabase(projectId1, PathUtils.get(databasePath1));
cache.putIfAbsent(projectId1, ip1, databasePath1.toString(), ip -> response); // cache miss
cache.putIfAbsent(projectId1, ip2, databasePath1.toString(), ip -> response); // cache miss
cache.putIfAbsent(projectId2, ip1, databasePath1.toString(), ip -> response); // cache miss
cache.putIfAbsent(projectId1, ip1, databasePath2.toString(), ip -> response); // cache miss
cache.purgeCacheEntriesForDatabase(projectId1, databasePath1);
// should have purged entries for projectId1 and databasePath1...
assertNull(cache.get(projectId1, ip1, databasePath1));
assertNull(cache.get(projectId1, ip2, databasePath1));
assertNull(cache.get(projectId1, ip1, databasePath1.toString()));
assertNull(cache.get(projectId1, ip2, databasePath1.toString()));
// ...but left the one for projectId2...
assertSame(response, cache.get(projectId2, ip1, databasePath1));
assertSame(response, cache.get(projectId2, ip1, databasePath1.toString()));
// ...and for databasePath2:
assertSame(response, cache.get(projectId1, ip1, databasePath2));
assertSame(response, cache.get(projectId1, ip1, databasePath2.toString()));
}
}