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)));
}
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");
}
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");
}
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);
}
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);
}
Aggregations