Search in sources :

Example 1 with AbstractResponse

use of com.maxmind.geoip2.model.AbstractResponse in project OpenSearch by opensearch-project.

the class IngestGeoIpPluginTests method testCachesAndEvictsResults.

public void testCachesAndEvictsResults() {
    GeoIpCache cache = new GeoIpCache(1);
    AbstractResponse response1 = mock(AbstractResponse.class);
    AbstractResponse response2 = mock(AbstractResponse.class);
    // add a key
    AbstractResponse cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), AbstractResponse.class, ip -> response1);
    assertSame(cachedResponse, response1);
    assertSame(cachedResponse, cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), AbstractResponse.class, ip -> response1));
    assertSame(cachedResponse, cache.get(InetAddresses.forString("127.0.0.1"), AbstractResponse.class));
    // evict old key by adding another value
    cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.2"), AbstractResponse.class, ip -> response2);
    assertSame(cachedResponse, response2);
    assertSame(cachedResponse, cache.putIfAbsent(InetAddresses.forString("127.0.0.2"), AbstractResponse.class, ip -> response2));
    assertSame(cachedResponse, cache.get(InetAddresses.forString("127.0.0.2"), AbstractResponse.class));
    assertNotSame(response1, cache.get(InetAddresses.forString("127.0.0.1"), AbstractResponse.class));
}
Also used : InetAddresses(org.opensearch.common.network.InetAddresses) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) Mockito.mock(org.mockito.Mockito.mock) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Aggregations

AbstractResponse (com.maxmind.geoip2.model.AbstractResponse)1 Mockito.mock (org.mockito.Mockito.mock)1 InetAddresses (org.opensearch.common.network.InetAddresses)1 GeoIpCache (org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)1 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)1