Search in sources :

Example 81 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project jackrabbit-oak by apache.

the class UploadStagingCacheTest method testConcurrentGetDelete.

/**
     * Concurrently retrieve after stage but before upload.
     * @throws Exception
     */
@Test
public void testConcurrentGetDelete() throws Exception {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
    closer.register(new ExecutorCloser(executorService));
    // Add load
    List<ListenableFuture<Integer>> futures = put(folder);
    // Get a handle to the file and open stream
    File file = stagingCache.getIfPresent(ID_PREFIX + 0);
    final InputStream fStream = Files.asByteSource(file).openStream();
    // task to copy the steam to a file simulating read from the stream
    File temp = folder.newFile();
    CountDownLatch copyThreadLatch = new CountDownLatch(1);
    SettableFuture<File> future1 = copyStreamThread(executorService, fStream, temp, copyThreadLatch);
    //start
    taskLatch.countDown();
    callbackLatch.countDown();
    waitFinish(futures);
    // trying copying now
    copyThreadLatch.countDown();
    future1.get();
    assertTrue(Files.equal(temp, uploader.read(ID_PREFIX + 0)));
}
Also used : InputStream(java.io.InputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) File(java.io.File) Test(org.junit.Test)

Example 82 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project jackrabbit-oak by apache.

the class FileCacheTest method setup.

@Before
public void setup() throws Exception {
    LOG.info("Started setup");
    root = folder.newFolder();
    closer = Closer.create();
    loader = new TestCacheLoader<String, InputStream>(folder.newFolder());
    CountDownLatch beforeLatch = new CountDownLatch(1);
    CountDownLatch afterLatch = new CountDownLatch(1);
    afterExecuteLatch = new CountDownLatch(1);
    TestExecutor executor = new TestExecutor(1, beforeLatch, afterLatch, afterExecuteLatch);
    beforeLatch.countDown();
    afterLatch.countDown();
    cache = FileCache.build(4 * 1024, /* KB */
    root, loader, executor);
    Futures.successfulAsList((Iterable<? extends ListenableFuture<?>>) executor.futures).get();
    closer.register(cache);
    LOG.info("Finished setup");
}
Also used : InputStream(java.io.InputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 83 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project jackrabbit-oak by apache.

the class FileCacheTest method rebuild.

/**
     * Trigger build cache on start.
     * @throws Exception
     */
@Test
public void rebuild() throws Exception {
    LOG.info("Started rebuild");
    afterExecuteLatch.await();
    LOG.info("Cache built");
    File f = createFile(0, loader, cache, folder);
    assertCache(0, cache, f);
    cache.close();
    CountDownLatch beforeLatch = new CountDownLatch(1);
    CountDownLatch afterLatch = new CountDownLatch(1);
    afterExecuteLatch = new CountDownLatch(1);
    TestExecutor executor = new TestExecutor(1, beforeLatch, afterLatch, afterExecuteLatch);
    beforeLatch.countDown();
    afterLatch.countDown();
    cache = FileCache.build(4 * 1024, /* bytes */
    root, loader, executor);
    closer.register(cache);
    afterExecuteLatch.await();
    Futures.successfulAsList((Iterable<? extends ListenableFuture<?>>) executor.futures).get();
    LOG.info("Cache rebuilt");
    assertCacheIfPresent(0, cache, f);
    assertCacheStats(cache, 1, 4 * 1024, 0, 0);
    LOG.info("Finished rebuild");
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) Test(org.junit.Test)

Example 84 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project jackrabbit-oak by apache.

the class UploadStagingCacheTest method testInvalidate.

/**
     * Invalidate after staging before upload.
     * @throws Exception
     */
@Test
public void testInvalidate() throws Exception {
    // add load
    List<ListenableFuture<Integer>> futures = put(folder);
    // Check invalidate
    stagingCache.invalidate(ID_PREFIX + 0);
    File file = stagingCache.getIfPresent(ID_PREFIX + 0);
    assertNull(file);
    //start
    taskLatch.countDown();
    callbackLatch.countDown();
    waitFinish(futures);
    assertCacheStats(stagingCache, 0, 0, 1, 1);
    // Should not return anything
    file = stagingCache.getIfPresent(ID_PREFIX + 0);
    assertNull(file);
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) File(java.io.File) Test(org.junit.Test)

Example 85 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project jackrabbit-oak by apache.

the class CacheTest method testRefresh.

@Test
public void testRefresh() throws ExecutionException {
    CacheLIRS<Integer, String> cache = new CacheLIRS.Builder<Integer, String>().maximumWeight(100).weigher(new Weigher<Integer, String>() {

        @Override
        public int weigh(Integer key, String value) {
            return key + value.length();
        }
    }).build(new CacheLoader<Integer, String>() {

        @Override
        public String load(Integer key) throws Exception {
            if (key < 0 || key >= 100) {
                throw new Exception("Out of range");
            }
            return "n" + key;
        }

        @Override
        public ListenableFuture<String> reload(Integer key, String oldValue) {
            assertTrue(oldValue != null);
            SettableFuture<String> f = SettableFuture.create();
            f.set(oldValue);
            return f;
        }
    });
    assertEquals("n1", cache.get(1));
    cache.refresh(1);
    cache.refresh(2);
    try {
        cache.get(-1);
        fail();
    } catch (Exception e) {
    // expected
    }
    // expected to log a warning, but not fail
    cache.refresh(-1);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) Weigher(com.google.common.cache.Weigher) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ListenableFuture (com.google.common.util.concurrent.ListenableFuture)186 Test (org.junit.Test)78 ArrayList (java.util.ArrayList)62 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)49 List (java.util.List)45 ExecutionException (java.util.concurrent.ExecutionException)42 IOException (java.io.IOException)35 Map (java.util.Map)33 CountDownLatch (java.util.concurrent.CountDownLatch)26 File (java.io.File)23 ImmutableList (com.google.common.collect.ImmutableList)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Callable (java.util.concurrent.Callable)19 ImmutableMap (com.google.common.collect.ImmutableMap)18 HashMap (java.util.HashMap)17 Futures (com.google.common.util.concurrent.Futures)14 URL (java.net.URL)14 TimeoutException (java.util.concurrent.TimeoutException)13 Request (com.metamx.http.client.Request)12