use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testCacheFullAdd.
/**
* Stage file when cache full.
* @throws Exception
*/
@Test
public void testCacheFullAdd() throws Exception {
// initialize cache to have restricted size
stagingCache = UploadStagingCache.build(root, null, 1, /*threads*/
4 * 1024, /* bytes */
uploader, null, /*cache*/
statsProvider, executor, null, 3000, 6000);
closer.register(stagingCache);
// add load
List<ListenableFuture<Integer>> futures = put(folder);
// Add another load
File f2 = copyToFile(randomStream(1, 4 * 1024), folder.newFile());
Optional<SettableFuture<Integer>> future2 = stagingCache.put(ID_PREFIX + 1, f2);
assertFalse(future2.isPresent());
// start
taskLatch.countDown();
callbackLatch.countDown();
assertFuture(futures, 0);
// Try 2nd upload again
Optional<SettableFuture<Integer>> future = stagingCache.put(ID_PREFIX + 1, f2);
futures = Lists.newArrayList();
if (future.isPresent()) {
futures.add(future.get());
}
assertFuture(futures, 1);
assertCacheStats(stagingCache, 0, 0, 2, 3);
}
use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testConcurrentSameAdd.
/**
* Stage same file concurrently.
* @throws Exception
*/
@Test
public void testConcurrentSameAdd() throws Exception {
// Add load
List<ListenableFuture<Integer>> futures = put(folder);
File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
Optional<SettableFuture<Integer>> future2 = stagingCache.put(ID_PREFIX + 0, f);
assertTrue(future2.isPresent());
assertEquals(future2.get().get().intValue(), 0);
// start
taskLatch.countDown();
callbackLatch.countDown();
assertFuture(futures, 0);
assertCacheStats(stagingCache, 0, 0, 1, 2);
}
use of com.google.common.util.concurrent.SettableFuture in project netvirt by opendaylight.
the class L2GatewayConnectionUtils method readAndCopyLocalUcastMacsToCache.
private void readAndCopyLocalUcastMacsToCache(final String elanName, final L2GatewayDevice l2GatewayDevice) {
final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(l2GatewayDevice.getHwvtepNodeId()));
jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
final SettableFuture settableFuture = SettableFuture.create();
Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
@Override
public void onSuccess(@Nonnull Optional<Node> resultNode) {
LocalUcastMacListener localUcastMacListener = new LocalUcastMacListener(broker, haOpClusteredListener, elanL2GatewayUtils, jobCoordinator, elanInstanceCache);
settableFuture.set(resultNode);
Optional<Node> nodeOptional = resultNode;
if (nodeOptional.isPresent()) {
Node node = nodeOptional.get();
if (node.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
List<LocalUcastMacs> localUcastMacs = node.getAugmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
if (localUcastMacs == null) {
return;
}
localUcastMacs.stream().filter((mac) -> {
return macBelongsToLogicalSwitch(mac, elanName);
}).forEach((mac) -> {
InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
localUcastMacListener.added(macIid, mac);
});
}
}
}
}, MoreExecutors.directExecutor());
return Lists.newArrayList(settableFuture);
}, 5);
}
use of com.google.common.util.concurrent.SettableFuture in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceClientTest method testInputStreamClosed.
@Test
public void testInputStreamClosed() throws Exception {
CountDownLatch startQuery = new CountDownLatch(1);
SettableFuture<Boolean> queryFinished = SettableFuture.create();
// Create a query that blocks on the countdown latch
when(queryServiceDelegate.prepareQuery(TEST_SQL_QUERY, MAX_ROWS, ImmutableMap.of())).thenReturn((MockQuery) outputStream -> {
Random random = new Random();
try {
outputStream.write(random.nextInt());
startQuery.await();
outputStream.write(random.nextInt());
queryFinished.set(true);
} catch (Exception e) {
queryFinished.setException(e);
}
});
// Start query and close immediately
client.query(TEST_SQL_QUERY, MAX_ROWS).close();
// Unleash query stream
startQuery.countDown();
// Assert no errors thrown or logged
assertThat(Futures.get(queryFinished, IOException.class), is(true));
verify(log, never()).logError(anyString(), any(Throwable.class));
}
use of com.google.common.util.concurrent.SettableFuture in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceClientTest method testWindowInputStreamClosed.
@Test
public void testWindowInputStreamClosed() throws Exception {
CountDownLatch startQuery = new CountDownLatch(1);
SettableFuture<Boolean> queryFinished = SettableFuture.create();
// Create a query that blocks on the countdown latch
when(queryServiceDelegate.prepareQuery(TEST_SQL_QUERY, WINDOW_MODE, WINDOW_SIZE, WINDOW_EVERY, WINDOW_MAX_SIZE, ImmutableMap.of())).thenReturn((MockQuery) outputStream -> {
Random random = new Random();
try {
outputStream.write(random.nextInt());
startQuery.await();
outputStream.write(random.nextInt());
queryFinished.set(true);
} catch (Exception e) {
queryFinished.setException(e);
}
});
// Start query and close immediately
client.query(TEST_SQL_QUERY, WINDOW_MODE, WINDOW_SIZE, WINDOW_EVERY, WINDOW_MAX_SIZE).close();
// Unleash query stream
startQuery.countDown();
// Assert no errors thrown or logged
assertThat(Futures.get(queryFinished, IOException.class), is(true));
verify(log, never()).logError(anyString(), any(Throwable.class));
}
Aggregations