Search in sources :

Example 6 with ExecutionError

use of com.google.common.util.concurrent.ExecutionError in project caffeine by ben-manes.

the class CacheLoadingTest method testBulkLoadError.

public void testBulkLoadError() throws ExecutionException {
    Error e = new Error();
    CacheLoader<Object, Object> loader = errorLoader(e);
    LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder().recordStats(), bulkLoader(loader));
    CacheStats stats = cache.stats();
    assertEquals(0, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(0, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.getAll(asList(new Object()));
        fail();
    } catch (ExecutionError expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(1, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(1, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionError(com.google.common.util.concurrent.ExecutionError)

Example 7 with ExecutionError

use of com.google.common.util.concurrent.ExecutionError in project guava by google.

the class LocalCache method load.

private V load(Object key) throws ExecutionException {
    long startTime = ticker.read();
    V calculatedValue;
    try {
        /*
       * This cast isn't safe, but we can rely on the fact that K is almost always passed to
       * Map.get(), and tools like IDEs and Findbugs can catch situations where this isn't the
       * case.
       *
       * The alternative is to add an overloaded method, but the chances of a user calling get()
       * instead of the new API and the risks inherent in adding a new API outweigh this little
       * hole.
       */
        K castKey = (K) key;
        calculatedValue = loader.load(castKey);
        put(castKey, calculatedValue);
    } catch (RuntimeException e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new UncheckedExecutionException(e);
    } catch (Exception e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new ExecutionException(e);
    } catch (Error e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new ExecutionError(e);
    }
    if (calculatedValue == null) {
        String message = loader + " returned null for key " + key + ".";
        throw new CacheLoader.InvalidCacheLoadException(message);
    }
    statsCounter.recordLoadSuccess(ticker.read() - startTime);
    return calculatedValue;
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with ExecutionError

use of com.google.common.util.concurrent.ExecutionError in project guava by google.

the class CacheLoadingTest method testLoadError.

public void testLoadError() throws ExecutionException {
    Error e = new Error();
    CacheLoader<Object, Object> loader = errorLoader(e);
    LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(loader);
    CacheStats stats = cache.stats();
    assertEquals(0, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(0, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.get(new Object());
        fail();
    } catch (ExecutionError expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(1, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(1, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.getUnchecked(new Object());
        fail();
    } catch (ExecutionError expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(2, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(2, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    cache.refresh(new Object());
    checkLoggedCause(e);
    stats = cache.stats();
    assertEquals(2, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(3, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    final Error callableError = new Error();
    try {
        cache.get(new Object(), new Callable<Object>() {

            @Override
            public Object call() {
                throw callableError;
            }
        });
        fail();
    } catch (ExecutionError expected) {
        assertSame(callableError, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(3, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(4, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.getAll(asList(new Object()));
        fail();
    } catch (ExecutionError expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(4, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(5, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionError(com.google.common.util.concurrent.ExecutionError)

Example 9 with ExecutionError

use of com.google.common.util.concurrent.ExecutionError in project guava by hceylan.

the class CacheLoadingTest method testBulkLoadError.

public void testBulkLoadError() throws ExecutionException {
    Error e = new Error();
    CacheLoader<Object, Object> loader = errorLoader(e);
    LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().build(bulkLoader(loader));
    CacheStats stats = cache.stats();
    assertEquals(0, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(0, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.getAll(asList(new Object()));
        fail();
    } catch (ExecutionError expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(1, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(1, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionError(com.google.common.util.concurrent.ExecutionError)

Example 10 with ExecutionError

use of com.google.common.util.concurrent.ExecutionError in project guava by hceylan.

the class AbstractLoadingCacheTest method testGetUnchecked_error.

public void testGetUnchecked_error() {
    final Error cause = new Error();
    final AtomicReference<Object> valueRef = new AtomicReference<Object>();
    LoadingCache<Object, Object> cache = new AbstractLoadingCache<Object, Object>() {

        @Override
        public Object get(Object key) throws ExecutionException {
            Object v = valueRef.get();
            if (v == null) {
                throw new ExecutionError(cause);
            }
            return v;
        }

        @Override
        public Object getIfPresent(Object key) {
            return valueRef.get();
        }
    };
    try {
        cache.getUnchecked(new Object());
        fail();
    } catch (ExecutionError expected) {
        assertEquals(cause, expected.getCause());
    }
    Object newValue = new Object();
    valueRef.set(newValue);
    assertSame(newValue, cache.getUnchecked(new Object()));
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionError(com.google.common.util.concurrent.ExecutionError) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

ExecutionError (com.google.common.util.concurrent.ExecutionError)11 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)3 NoSuchElementException (java.util.NoSuchElementException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Stopwatch (com.google.common.base.Stopwatch)2 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)2 UnsupportedLoadingOperationException (com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 AbstractMap (java.util.AbstractMap)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nullable (javax.annotation.Nullable)2