use of com.google.common.util.concurrent.SettableFuture in project bazel by bazelbuild.
the class GlobCache method getGlobUnsorted.
@VisibleForTesting
protected List<String> getGlobUnsorted(String pattern, boolean excludeDirs) throws IOException, BadGlobException, InterruptedException {
Future<List<Path>> futureResult = getGlobUnsortedAsync(pattern, excludeDirs);
List<Path> globPaths = fromFuture(futureResult);
// garbage collection of the GlobFuture and GlobVisitor objects.
if (!(futureResult instanceof SettableFuture<?>)) {
SettableFuture<List<Path>> completedFuture = SettableFuture.create();
completedFuture.set(globPaths);
globCache.put(Pair.of(pattern, excludeDirs), completedFuture);
}
List<String> result = Lists.newArrayListWithCapacity(globPaths.size());
for (Path path : globPaths) {
String relative = path.relativeTo(packageDirectory).getPathString();
// really want to name the package directory.
if (!relative.isEmpty()) {
result.add(relative);
}
}
return result;
}
use of com.google.common.util.concurrent.SettableFuture in project apollo by ctripcorp.
the class ReleaseMessageScannerTest method testScanMessageAndNotifyMessageListener.
@Test
public void testScanMessageAndNotifyMessageListener() throws Exception {
SettableFuture<ReleaseMessage> someListenerFuture = SettableFuture.create();
ReleaseMessageListener someListener = (message, channel) -> someListenerFuture.set(message);
releaseMessageScanner.addMessageListener(someListener);
String someMessage = "someMessage";
long someId = 100;
ReleaseMessage someReleaseMessage = assembleReleaseMessage(someId, someMessage);
when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(0L)).thenReturn(Lists.newArrayList(someReleaseMessage));
ReleaseMessage someListenerMessage = someListenerFuture.get(5000, TimeUnit.MILLISECONDS);
assertEquals(someMessage, someListenerMessage.getMessage());
assertEquals(someId, someListenerMessage.getId());
SettableFuture<ReleaseMessage> anotherListenerFuture = SettableFuture.create();
ReleaseMessageListener anotherListener = (message, channel) -> anotherListenerFuture.set(message);
releaseMessageScanner.addMessageListener(anotherListener);
String anotherMessage = "anotherMessage";
long anotherId = someId + 1;
ReleaseMessage anotherReleaseMessage = assembleReleaseMessage(anotherId, anotherMessage);
when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(someId)).thenReturn(Lists.newArrayList(anotherReleaseMessage));
ReleaseMessage anotherListenerMessage = anotherListenerFuture.get(5000, TimeUnit.MILLISECONDS);
assertEquals(anotherMessage, anotherListenerMessage.getMessage());
assertEquals(anotherId, anotherListenerMessage.getId());
}
use of com.google.common.util.concurrent.SettableFuture in project bitsquare by bitsquare.
the class RequestDataHandler method requestData.
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void requestData(NodeAddress nodeAddress, boolean isPreliminaryDataRequest) {
Log.traceCall("nodeAddress=" + nodeAddress);
peersNodeAddress = nodeAddress;
if (!stopped) {
GetDataRequest getDataRequest;
// We collect the keys of the PersistedStoragePayload items so we exclude them in our request.
// PersistedStoragePayload items don't get removed, so we don't have an issue with the case that
// an object gets removed in between PreliminaryGetDataRequest and the GetUpdatedDataRequest and we would
// miss that event if we do not load the full set or use some delta handling.
Set<byte[]> excludedKeys = dataStorage.getMap().entrySet().stream().filter(e -> e.getValue().getStoragePayload() instanceof PersistedStoragePayload).map(e -> e.getKey().bytes).collect(Collectors.toSet());
if (isPreliminaryDataRequest)
getDataRequest = new PreliminaryGetDataRequest(nonce, excludedKeys);
else
getDataRequest = new GetUpdatedDataRequest(networkNode.getNodeAddress(), nonce, excludedKeys);
if (timeoutTimer == null) {
timeoutTimer = UserThread.runAfter(() -> {
// setup before sending to avoid race conditions
if (!stopped) {
String errorMessage = "A timeout occurred at sending getDataRequest:" + getDataRequest + " on nodeAddress:" + nodeAddress;
log.debug(errorMessage + " / RequestDataHandler=" + RequestDataHandler.this);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT);
} else {
log.trace("We have stopped already. We ignore that timeoutTimer.run call. " + "Might be caused by an previous networkNode.sendMessage.onFailure.");
}
}, TIME_OUT_SEC);
}
log.debug("We send a {} to peer {}. ", getDataRequest.getClass().getSimpleName(), nodeAddress);
networkNode.addMessageListener(this);
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
if (!stopped) {
RequestDataHandler.this.connection = connection;
log.trace("Send " + getDataRequest + " to " + nodeAddress + " succeeded.");
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." + "Might be caused by an previous timeout.");
}
}
@Override
public void onFailure(@NotNull Throwable throwable) {
if (!stopped) {
String errorMessage = "Sending getDataRequest to " + nodeAddress + " failed. That is expected if the peer is offline.\n\t" + "getDataRequest=" + getDataRequest + "." + "\n\tException=" + throwable.getMessage();
log.debug(errorMessage);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " + "Might be caused by an previous timeout.");
}
}
});
} else {
log.warn("We have stopped already. We ignore that requestData call.");
}
}
use of com.google.common.util.concurrent.SettableFuture 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);
}
use of com.google.common.util.concurrent.SettableFuture in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testPutMoveFileError.
/**
* Error in putting file to stage.
* @throws Exception
*/
@Test
public void testPutMoveFileError() throws Exception {
File empty = new File(folder.getRoot(), String.valueOf(System.currentTimeMillis()));
assertFalse(empty.exists());
Optional<SettableFuture<Integer>> future = stagingCache.put(ID_PREFIX + 0, empty);
// assert no file
assertFalse(future.isPresent());
assertEquals(1, stagingCache.getStats().getMissCount());
assertCacheStats(stagingCache, 0, 0, 0, 1);
}
Aggregations