use of java.util.concurrent.ExecutionException in project crate by crate.
the class ListenableBatchConsumerTest method testErrorCaseHandling.
@Test
public void testErrorCaseHandling() throws Exception {
TestingBatchConsumer consumer = new TestingBatchConsumer();
ListenableBatchConsumer listenableBatchConsumer = new ListenableBatchConsumer(consumer);
listenableBatchConsumer.accept(null, new IllegalStateException("dummy"));
// both the delegate consumer must receive the error and also the listenableBatchConsumers future must trigger
try {
consumer.getResult();
fail("should have raised an exception");
} catch (IllegalStateException e) {
// expected
}
try {
listenableBatchConsumer.completionFuture().get(10, TimeUnit.SECONDS);
fail("should have raised an exception");
} catch (ExecutionException e) {
// expected
}
}
use of java.util.concurrent.ExecutionException in project weixin-java-tools by chanjarster.
the class WxCpMessageRouter method route.
/**
* 处理微信消息
* @param wxMessage
*/
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
if (isDuplicateMessage(wxMessage)) {
// 如果是重复消息,那么就不做处理
return null;
}
final List<WxCpMessageRouterRule> matchRules = new ArrayList<WxCpMessageRouterRule>();
// 收集匹配的规则
for (final WxCpMessageRouterRule rule : rules) {
if (rule.test(wxMessage)) {
matchRules.add(rule);
if (!rule.isReEnter()) {
break;
}
}
}
if (matchRules.size() == 0) {
return null;
}
WxCpXmlOutMessage res = null;
final List<Future> futures = new ArrayList<Future>();
for (final WxCpMessageRouterRule rule : matchRules) {
// 返回最后一个非异步的rule的执行结果
if (rule.isAsync()) {
futures.add(executorService.submit(new Runnable() {
public void run() {
rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
}
}));
} else {
res = rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
// 在同步操作结束,session访问结束
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
sessionEndAccess(wxMessage);
}
}
if (futures.size() > 0) {
executorService.submit(new Runnable() {
@Override
public void run() {
for (Future future : futures) {
try {
future.get();
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
// 异步操作结束,session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException e) {
log.error("Error happened when wait task finish", e);
} catch (ExecutionException e) {
log.error("Error happened when wait task finish", e);
}
}
}
});
}
return res;
}
use of java.util.concurrent.ExecutionException in project cucumber-jvm by cucumber.
the class URLOutputStreamTest method can_http_put.
@Test
public void can_http_put() throws IOException, ExecutionException, InterruptedException {
final BlockingQueue<String> data = new LinkedBlockingDeque<String>();
Rest r = new Rest(webbit);
r.PUT("/.cucumber/stepdefs.json", new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest req, HttpResponse res, HttpControl ctl) throws Exception {
data.offer(req.body());
res.end();
}
});
Writer w = new UTF8OutputStreamWriter(new URLOutputStream(new URL(Utils.toURL("http://localhost:9873/.cucumber"), "stepdefs.json")));
w.write("Hellesøy");
w.flush();
w.close();
assertEquals("Hellesøy", data.poll(1000, TimeUnit.MILLISECONDS));
}
use of java.util.concurrent.ExecutionException in project weave by continuuity.
the class ZKServiceDecoratorTest method testStateTransition.
@Test
public void testStateTransition() throws InterruptedException, ExecutionException, TimeoutException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
try {
final String namespace = Joiner.on('/').join("/weave", RunIds.generate(), "runnables", "Runner1");
final ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClient.startAndWait();
zkClient.create(namespace, null, CreateMode.PERSISTENT).get();
try {
JsonObject content = new JsonObject();
content.addProperty("containerId", "container-123");
content.addProperty("host", "localhost");
RunId runId = RunIds.generate();
final Semaphore semaphore = new Semaphore(0);
ZKServiceDecorator service = new ZKServiceDecorator(ZKClients.namespace(zkClient, namespace), runId, Suppliers.ofInstance(content), new AbstractIdleService() {
@Override
protected void startUp() throws Exception {
Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to start");
}
@Override
protected void shutDown() throws Exception {
Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to stop");
}
});
final String runnablePath = namespace + "/" + runId.getId();
final AtomicReference<String> stateMatch = new AtomicReference<String>("STARTING");
watchDataChange(zkClient, runnablePath + "/state", semaphore, stateMatch);
Assert.assertEquals(Service.State.RUNNING, service.start().get(5, TimeUnit.SECONDS));
stateMatch.set("STOPPING");
Assert.assertEquals(Service.State.TERMINATED, service.stop().get(5, TimeUnit.SECONDS));
} finally {
zkClient.stopAndWait();
}
} finally {
zkServer.stopAndWait();
}
}
use of java.util.concurrent.ExecutionException in project weave by continuuity.
the class ServicesTest method testCompletion.
@Test
public void testCompletion() throws ExecutionException, InterruptedException {
Service service = new DummyService("s1", new AtomicBoolean());
ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
service.start();
service.stop();
completion.get();
AtomicBoolean transiting = new AtomicBoolean();
service = new DummyService("s2", transiting);
completion = Services.getCompletionFuture(service);
service.startAndWait();
transiting.set(true);
service.stop();
try {
completion.get();
Assert.assertTrue(false);
} catch (ExecutionException e) {
// Expected
}
}
Aggregations