Search in sources :

Example 1 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class CacheKeyTest method testKeyGenerationFromMetacardAndResourceRequest.

@Test
public void testKeyGenerationFromMetacardAndResourceRequest() {
    CacheKey cacheKey = new CacheKey(getMetacardStub("sampleId"), getResourceRequestStub());
    String key = cacheKey.generateKey();
    assertNotNull("Key must not be null.", key);
    assertThat("Key must not be empty.", key, not(equalToIgnoringWhiteSpace("")));
}
Also used : CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Example 2 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class CacheKeyTest method testKeyUniquenessFromSourcesAndIdsWithoutResourceRequest.

@Test
public void testKeyUniquenessFromSourcesAndIdsWithoutResourceRequest() {
    CacheKey cacheKey1 = new CacheKey(getMetacardStub("sampleId1", "source1"));
    CacheKey cacheKey2 = new CacheKey(getMetacardStub("sampleId2", "source2"));
    CacheKey cacheKey3 = new CacheKey(getMetacardStub("sampleId1", "source2"));
    CacheKey cacheKey4 = new CacheKey(getMetacardStub("sampleId2", "source1"));
    String key1 = cacheKey1.generateKey();
    String key2 = cacheKey2.generateKey();
    String key3 = cacheKey3.generateKey();
    String key4 = cacheKey4.generateKey();
    assertThat("Keys must be different.", key1, not(equalTo(key2)));
    assertThat("Keys must be different.", key1, not(equalTo(key3)));
    assertThat("Keys must be different.", key1, not(equalTo(key4)));
}
Also used : CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Example 3 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class CacheKeyTest method testKeyGenerationFromMetacardOnly.

@Test
public void testKeyGenerationFromMetacardOnly() {
    CacheKey cacheKey = new CacheKey(getMetacardStub("sampleId"));
    String key = cacheKey.generateKey();
    assertNotNull("Key must not be null.", key);
    assertThat("Key must not be empty.", key, not(equalToIgnoringWhiteSpace("")));
}
Also used : CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Example 4 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testCacheFileExceptionDuringProductDownload.

/**
     * Tests that if exception with file being cached to occurs during a product retrieval, then the (partially) cached
     * file is deleted and it is not placed in the cache map, but the product continues to be streamed to the client until
     * the EOF is detected.
     *
     * @throws Exception
     */
@Test
@Ignore
public void testCacheFileExceptionDuringProductDownload() throws Exception {
    // Need the product InputStream (MockInputStream) to read slower so that client has time to
    // start reading from the ReliableResourceInputStream and close the FileOutputStream the
    // download manager is writing to, simulating a cache file exception during
    // the product download
    mis = new MockInputStream(productInputFilename, true);
    mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
    Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
    resourceResponse = getMockResourceResponse();
    ResourceRetriever retriever = mock(ResourceRetriever.class);
    when(retriever.retrieveResource()).thenReturn(resourceResponse);
    int chunkSize = 2;
    startDownload(true, chunkSize, false, metacard, retriever);
    // On second chunk read by client it will close the download manager's cache file output stream
    // to simulate a cache file exception that should be detected by the ReliableResourceCallable
    executor = Executors.newCachedThreadPool();
    ProductDownloadClient productDownloadClient = new ProductDownloadClient(productInputStream, chunkSize);
    productDownloadClient.setSimulateCacheFileException(2, downloadMgr);
    future = executor.submit(productDownloadClient);
    ByteArrayOutputStream clientBytesRead = future.get();
    verifyClientBytesRead(clientBytesRead);
    // Verify product was not cached, i.e., its pending caching entry was removed
    String cacheKey = new CacheKey(metacard, resourceResponse.getRequest()).generateKey();
    verify(resourceCache, timeout(3000)).removePendingCacheEntry(cacheKey);
    cleanup();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testDownloadWithCachingDifferentChunkSizes.

/**
     * Verifies that if client is reading from @ReliableResourceInputStream slower than
     * {@link ReliableResourceCallable} is reading from product InputStream and writing to FileBackedOutputStream,
     * that complete product is still successfully downloaded by the client.
     * (This will be the case with CXF and @ReliableResourceCallable)
     *
     * @throws Exception
     */
@Test
public //@Ignore
void testDownloadWithCachingDifferentChunkSizes() throws Exception {
    mis = new MockInputStream(productInputFilename);
    Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
    resourceResponse = getMockResourceResponse();
    ResourceRetriever retriever = mock(ResourceRetriever.class);
    when(retriever.retrieveResource()).thenReturn(resourceResponse);
    CacheKey cacheKey = new CacheKey(metacard, resourceResponse.getRequest());
    String key = cacheKey.generateKey();
    when(resourceCache.isPending(key)).thenReturn(false);
    int chunkSize = 50;
    startDownload(true, chunkSize, false, metacard, retriever);
    int clientChunkSize = 2;
    ByteArrayOutputStream clientBytesRead = clientRead(clientChunkSize, productInputStream);
    // Captures the ReliableResource object that should have been put in the ResourceCacheImpl's map
    ArgumentCaptor<ReliableResource> argument = ArgumentCaptor.forClass(ReliableResource.class);
    verify(resourceCache).put(argument.capture());
    verifyCaching(argument.getValue(), EXPECTED_CACHE_KEY);
    verifyClientBytesRead(clientBytesRead);
    cleanup();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Aggregations

CacheKey (ddf.catalog.cache.impl.CacheKey)20 Test (org.junit.Test)17 Metacard (ddf.catalog.data.Metacard)7 MockInputStream (ddf.catalog.cache.MockInputStream)6 ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReliableResource (ddf.catalog.resource.data.ReliableResource)4 Ignore (org.junit.Ignore)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 Resource (ddf.catalog.resource.Resource)2 IOException (java.io.IOException)2 CountingOutputStream (com.google.common.io.CountingOutputStream)1 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)1 Attribute (ddf.catalog.data.Attribute)1 Result (ddf.catalog.data.Result)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 ResourceRequest (ddf.catalog.operation.ResourceRequest)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1