mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
log exceptions in futures of RequestManager
Signed-off-by: mmews <marcus.mews@numberfour.eu>
This commit is contained in:
parent
09717910c9
commit
d620e0daae
4 changed files with 98 additions and 3 deletions
|
@ -9,16 +9,20 @@ package org.eclipse.xtext.ide.tests.server.concurrent
|
|||
|
||||
import com.google.inject.Guice
|
||||
import com.google.inject.Inject
|
||||
import java.util.concurrent.ExecutionException
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import org.apache.log4j.Level
|
||||
import org.eclipse.xtext.ide.server.ServerModule
|
||||
import org.eclipse.xtext.ide.server.concurrent.RequestManager
|
||||
import org.eclipse.xtext.testing.logging.LoggingTester
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
import org.junit.Assert
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
|
@ -42,6 +46,35 @@ class RequestManagerTest {
|
|||
sharedState = null
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testLogException() {
|
||||
val logResult = LoggingTester.captureLogging(Level.ALL, RequestManager, [
|
||||
val future = requestManager.runWrite([
|
||||
throw new RuntimeException();
|
||||
], [])
|
||||
|
||||
// join future to assert log later
|
||||
try {
|
||||
future.get
|
||||
} catch (Exception e) {}
|
||||
])
|
||||
|
||||
logResult.assertLogEntry("Error during request:")
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException)
|
||||
def void testCatchException() {
|
||||
LoggingTester.captureLogging(Level.ALL, RequestManager, [
|
||||
val future = requestManager.runWrite([
|
||||
throw new RuntimeException()
|
||||
], [])
|
||||
|
||||
assertEquals('Foo', future.get)
|
||||
])
|
||||
|
||||
Assert.fail
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunRead() {
|
||||
val future = requestManager.runRead [
|
||||
|
@ -132,5 +165,4 @@ class RequestManagerTest {
|
|||
Thread.sleep(10)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,10 +10,13 @@ package org.eclipse.xtext.ide.tests.server.concurrent;
|
|||
import com.google.inject.Guice;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.apache.log4j.Level;
|
||||
import org.eclipse.xtext.ide.server.ServerModule;
|
||||
import org.eclipse.xtext.ide.server.concurrent.RequestManager;
|
||||
import org.eclipse.xtext.testing.logging.LoggingTester;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function0;
|
||||
|
@ -49,6 +52,49 @@ public class RequestManagerTest {
|
|||
this.sharedState = null;
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testLogException() {
|
||||
final Runnable _function = () -> {
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
try {
|
||||
future.get();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Exception) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
};
|
||||
final LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, RequestManager.class, _function);
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException.class)
|
||||
public void testCatchException() {
|
||||
final Runnable _function = () -> {
|
||||
try {
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
LoggingTester.captureLogging(Level.ALL, RequestManager.class, _function);
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunRead() {
|
||||
try {
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.util.concurrent.CompletableFuture
|
|||
import java.util.concurrent.CompletionException
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import org.apache.log4j.Logger
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
import org.eclipse.xtext.service.OperationCanceledManager
|
||||
import org.eclipse.xtext.util.CancelIndicator
|
||||
|
@ -23,6 +24,7 @@ import org.eclipse.xtext.util.CancelIndicator
|
|||
* @since 2.11
|
||||
*/
|
||||
class RequestManager {
|
||||
static final Logger LOG = Logger.getLogger(RequestManager);
|
||||
|
||||
@Inject ExecutorService parallel
|
||||
|
||||
|
@ -56,7 +58,12 @@ class RequestManager {
|
|||
protected def <V> CompletableFuture<V> submit(AbstractRequest<V> request) {
|
||||
requests += request
|
||||
queue.submit(request)
|
||||
return request.get
|
||||
val result = request.get.whenComplete[v, throwable|
|
||||
if (throwable !== null && !isCancelException(throwable)) {
|
||||
LOG.error("Error during request: ", throwable);
|
||||
}
|
||||
]
|
||||
return result
|
||||
}
|
||||
|
||||
protected def CompletableFuture<Void> cancel() {
|
||||
|
|
|
@ -14,6 +14,8 @@ import java.util.concurrent.CompletableFuture;
|
|||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.BiConsumer;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.xtext.ide.server.concurrent.AbstractRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.ReadRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.WriteRequest;
|
||||
|
@ -31,6 +33,8 @@ import org.eclipse.xtext.xbase.lib.Functions.Function2;
|
|||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class RequestManager {
|
||||
private static final Logger LOG = Logger.getLogger(RequestManager.class);
|
||||
|
||||
@Inject
|
||||
private ExecutorService parallel;
|
||||
|
||||
|
@ -62,7 +66,13 @@ public class RequestManager {
|
|||
protected <V extends Object> CompletableFuture<V> submit(final AbstractRequest<V> request) {
|
||||
this.requests.add(request);
|
||||
this.queue.submit(request);
|
||||
return request.get();
|
||||
final BiConsumer<V, Throwable> _function = (V v, Throwable throwable) -> {
|
||||
if (((throwable != null) && (!this.isCancelException(throwable)))) {
|
||||
RequestManager.LOG.error("Error during request: ", throwable);
|
||||
}
|
||||
};
|
||||
final CompletableFuture<V> result = request.get().whenComplete(_function);
|
||||
return result;
|
||||
}
|
||||
|
||||
protected CompletableFuture<Void> cancel() {
|
||||
|
|
Loading…
Reference in a new issue