1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Added unit tests for standard service functionality (159680).

This commit is contained in:
Pawel Piech 2006-10-16 14:49:55 +00:00
parent cfeb1e5d31
commit bc54ad145e
3 changed files with 123 additions and 58 deletions

View file

@ -71,27 +71,30 @@ public class DefaultDsfExecutor extends ScheduledThreadPoolExecutor
} catch (CancellationException e) { // Ignore also
} catch (ExecutionException e) {
if (e.getCause() != null) {
ILog log = DsfPlugin.getDefault().getLog();
if (log != null) {
log.log(new Status(
IStatus.ERROR, DsfPlugin.PLUGIN_ID, -1, "Uncaught exception in DSF executor thread", e.getCause()));
// Print out the stack trace to console if assertions are enabled.
if(ASSERTIONS_ENABLED) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(512);
PrintStream printStream = new PrintStream(outStream);
try {
printStream.write("Uncaught exception in session executor thread: ".getBytes());
} catch (IOException e2) {}
e.getCause().printStackTrace(new PrintStream(outStream));
System.err.println(outStream.toString());
}
}
logException(t);
}
}
}
}
static void logException(Throwable t) {
ILog log = DsfPlugin.getDefault().getLog();
if (log != null) {
log.log(new Status(
IStatus.ERROR, DsfPlugin.PLUGIN_ID, -1, "Uncaught exception in DSF executor thread", t));
}
// Print out the stack trace to console if assertions are enabled.
if(ASSERTIONS_ENABLED) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(512);
PrintStream printStream = new PrintStream(outStream);
try {
printStream.write("Uncaught exception in session executor thread: ".getBytes());
} catch (IOException e2) {}
t.printStackTrace(new PrintStream(outStream));
System.err.println(outStream.toString());
}
}
//
// Utilities used for tracing.
//

View file

@ -17,6 +17,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import org.eclipse.core.runtime.CoreException;
/**
* A convenience class that allows a client to retrieve data from services
@ -53,19 +55,36 @@ abstract public class DsfQuery<V> extends DsfRunnable
public boolean isDone() { return fSync.doIsDone(); }
protected void done(V result) {
fSync.doSet(result);
}
protected void doneException(Throwable t) {
fSync.doSetException(t);
}
abstract protected void execute();
abstract protected void execute(GetDataDone<V> done);
public void run() {
if (fSync.doRun()) {
execute();
try {
execute(new GetDataDone<V>() {
public void run() {
if (getStatus().isOK()) fSync.doSet(getData());
else fSync.doSetException(new CoreException(getStatus()));
}
});
} catch(Throwable t) {
/*
* Catching the exception here will only work if the exception
* happens within the execute. It will not work in cases when
* the execute submits other runnables, and the other runnables
* encounter the exception.
*/
fSync.doSetException(t);
/*
* Since we caught the exception, it will not be logged by
* DefaultDsfExecutable.afterExecution(). So log it here.
*/
DefaultDsfExecutor.logException(t);
}
}
}

View file

@ -160,6 +160,11 @@ abstract public class DsfSequence extends DsfRunnable implements Future<Object>
* Returns the steps to be executed. It is up to the deriving class to
* supply the steps and to ensure that the list of steps will not be
* modified after the sequence is constructed.
* <p>
* Steps are purposely not accepted as part of the DsfConstructor, in
* order to allow deriving classes to create the steps as a field. And a
* setSteps() method is not provided, to guarantee that the steps will not
* be modified once set (perhaps this is a bit paranoid, but oh well).
*/
abstract public Step[] getSteps();
@ -264,24 +269,43 @@ abstract public class DsfSequence extends DsfRunnable implements Future<Object>
// Proceed with executing next step.
fCurrentStepIdx = nextStepIndex;
getSteps()[fCurrentStepIdx].execute(new Done() {
final private int fStepIdx = fCurrentStepIdx;
public void run() {
// Check if we're still the correct step.
assert fStepIdx == fCurrentStepIdx;
// Proceed to the next step.
if (getStatus().isOK()) {
fProgressMonitor.worked(getSteps()[fStepIdx].getTicks());
executeStep(fStepIdx + 1);
} else {
abortExecution(getStatus());
try {
getSteps()[fCurrentStepIdx].execute(new Done() {
final private int fStepIdx = fCurrentStepIdx;
public void run() {
// Check if we're still the correct step.
assert fStepIdx == fCurrentStepIdx;
// Proceed to the next step.
if (getStatus().isOK()) {
fProgressMonitor.worked(getSteps()[fStepIdx].getTicks());
executeStep(fStepIdx + 1);
} else {
abortExecution(getStatus());
}
}
}
public String toString() {
return "DsfSequence \"" + fTaskName + "\", result for executing step #" + fStepIdx + " = " + getStatus();
}
});
public String toString() {
return "DsfSequence \"" + fTaskName + "\", result for executing step #" + fStepIdx + " = " + getStatus();
}
});
} catch(Throwable t) {
/*
* Catching the exception here will only work if the exception
* happens within the execute method. It will not work in cases
* when the execute submits other runnables, and the other runnables
* encounter the exception.
*/
abortExecution(new Status(
IStatus.ERROR, DsfPlugin.PLUGIN_ID, 0,
"Unhandled exception when executing DsfSequence " + this + ", step #" + fCurrentStepIdx,
t));
/*
* Since we caught the exception, it will not be logged by
* DefaultDsfExecutable.afterExecution(). So log it here.
*/
DefaultDsfExecutor.logException(t);
}
}
/**
@ -297,25 +321,44 @@ abstract public class DsfSequence extends DsfRunnable implements Future<Object>
// Proceed with rolling back given step.
fCurrentStepIdx = stepIdx;
getSteps()[fCurrentStepIdx].rollBack(new Done() {
final private int fStepIdx = fCurrentStepIdx;
public void run() {
// Check if we're still the correct step.
assert fStepIdx == fCurrentStepIdx;
// Proceed to the next step.
if (getStatus().isOK()) {
fProgressMonitor.worked(getSteps()[fStepIdx].getTicks());
rollBackStep(fStepIdx - 1);
} else {
abortRollBack(getStatus());
}
};
@Override
public String toString() {
return "DsfSequence \"" + fTaskName + "\", result for rolling back step #" + fStepIdx + " = " + getStatus();
}
});
try {
getSteps()[fCurrentStepIdx].rollBack(new Done() {
final private int fStepIdx = fCurrentStepIdx;
public void run() {
// Check if we're still the correct step.
assert fStepIdx == fCurrentStepIdx;
// Proceed to the next step.
if (getStatus().isOK()) {
fProgressMonitor.worked(getSteps()[fStepIdx].getTicks());
rollBackStep(fStepIdx - 1);
} else {
abortRollBack(getStatus());
}
};
@Override
public String toString() {
return "DsfSequence \"" + fTaskName + "\", result for rolling back step #" + fStepIdx + " = " + getStatus();
}
});
} catch(Throwable t) {
/*
* Catching the exception here will only work if the exception
* happens within the execute method. It will not work in cases
* when the execute submits other runnables, and the other runnables
* encounter the exception.
*/
abortRollBack(new Status(
IStatus.ERROR, DsfPlugin.PLUGIN_ID, 0,
"Unhandled exception when rolling back DsfSequence " + this + ", step #" + fCurrentStepIdx,
t));
/*
* Since we caught the exception, it will not be logged by
* DefaultDsfExecutable.afterExecution(). So log it here.
*/
DefaultDsfExecutor.logException(t);
}
}
/**