use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleIPCEventStreamAgent method sendPreComponentUpdateEvent.
/**
* Signal components about pending component updates.
*
* @param preComponentUpdateEvent event sent to subscribed components
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public List<Future<DeferComponentUpdateRequest>> sendPreComponentUpdateEvent(PreComponentUpdateEvent preComponentUpdateEvent) {
List<Future<DeferComponentUpdateRequest>> deferUpdateFutures = new ArrayList<>();
discardDeferComponentUpdateFutures();
for (Map.Entry<String, Set<StreamEventPublisher<ComponentUpdatePolicyEvents>>> entry : componentUpdateListeners.entrySet()) {
String serviceName = entry.getKey();
entry.getValue().forEach(subscribeHandler -> {
log.atTrace().kv(COMPONENT_NAME, serviceName).log("Sending preComponentUpdate event");
ComponentUpdatePolicyEvents componentUpdatePolicyEvents = new ComponentUpdatePolicyEvents();
componentUpdatePolicyEvents.setPreUpdateEvent(preComponentUpdateEvent);
CompletableFuture<DeferComponentUpdateRequest> deferUpdateFuture = new CompletableFuture<>();
// If there are multiple pre component events sent to same service, we will store the latest future
// As the update should be waiting for the latest one to complete.
Pair<String, String> serviceAndDeployment = new Pair<>(serviceName, preComponentUpdateEvent.getDeploymentId());
// Save to the map before sending the event so that it will be there if
// they respond very quickly
deferUpdateFuturesMap.put(serviceAndDeployment, deferUpdateFuture);
try {
subscribeHandler.sendStreamEvent(componentUpdatePolicyEvents).get(DEFAULT_STREAM_MESSAGE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (Exception e) {
log.atError().setCause(e).kv(COMPONENT_NAME, serviceName).log("Failed to send the pre component update on stream");
deferUpdateFuturesMap.remove(serviceAndDeployment);
return;
}
deferUpdateFutures.add(deferUpdateFuture);
});
}
return deferUpdateFutures;
}
use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.
the class TestUtils method asyncAssertOnBiConsumer.
/**
* Wraps a given biconsumer function so that once it is called, the completable future can
* complete with the exception, or with a success.
*
* @param bi
* @param numCalls number of expected calls
*/
public static <A, B> Pair<CompletableFuture<Void>, BiConsumer<A, B>> asyncAssertOnBiConsumer(BiConsumer<A, B> bi, int numCalls) {
CompletableFuture<Void> f = new CompletableFuture<>();
AtomicInteger calls = new AtomicInteger(0);
return new Pair<>(f, (a, b) -> {
try {
int callsSoFar = calls.incrementAndGet();
bi.accept(a, b);
if (callsSoFar == numCalls) {
f.complete(null);
}
} catch (Throwable ex) {
f.completeExceptionally(ex);
}
});
}
use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.
the class TestUtils method asyncAssertOnConsumer.
/**
* Creates a test utility wrapping a given Consumer and returning a new Consumer and Future. Use the Future to
* validate that the Consumer is called numCalls times without any exceptions.
*
* @param c Consumer to wrap
* @param numCalls number of expected calls. -1 will ignore the number of expected calls
*/
public static <A> Pair<CompletableFuture<Void>, Consumer<A>> asyncAssertOnConsumer(Consumer<A> c, int numCalls) {
CompletableFuture<Void> f = new CompletableFuture<>();
AtomicInteger calls = new AtomicInteger();
return new Pair<>(f, (a) -> {
try {
int callsSoFar = calls.incrementAndGet();
c.accept(a);
if (callsSoFar == numCalls || numCalls < 0) {
f.complete(null);
} else if (callsSoFar > numCalls) {
f.obtrudeException(new Exception("Too many invocations! (" + callsSoFar + "), expected " + numCalls));
}
} catch (Throwable ex) {
f.obtrudeException(ex);
}
});
}
Aggregations