1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 06:45:43 +02:00

Bug 211620: Junit test should make use of Query

This commit is contained in:
Marc Khouzam 2011-02-05 02:52:40 +00:00
parent 6addb930f2
commit 5bca49ece1

View file

@ -10,7 +10,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.tests.dsf.gdb.framework; package org.eclipse.cdt.tests.dsf.gdb.framework;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -43,7 +43,6 @@ import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch; import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch;
import org.eclipse.cdt.dsf.gdb.service.IGDBProcesses; import org.eclipse.cdt.dsf.gdb.service.IGDBProcesses;
import org.eclipse.cdt.dsf.gdb.service.command.IGDBControl; import org.eclipse.cdt.dsf.gdb.service.command.IGDBControl;
import org.eclipse.cdt.dsf.mi.service.IMICommandControl;
import org.eclipse.cdt.dsf.mi.service.IMIExecutionDMContext; import org.eclipse.cdt.dsf.mi.service.IMIExecutionDMContext;
import org.eclipse.cdt.dsf.mi.service.IMIRunControl; import org.eclipse.cdt.dsf.mi.service.IMIRunControl;
import org.eclipse.cdt.dsf.mi.service.MIStack; import org.eclipse.cdt.dsf.mi.service.MIStack;
@ -93,8 +92,8 @@ public class SyncUtil {
fStack = tracker.getService(MIStack.class); fStack = tracker.getService(MIStack.class);
fExpressions = tracker.getService(IExpressions.class); fExpressions = tracker.getService(IExpressions.class);
fProcessesService = tracker.getService(IGDBProcesses.class); fProcessesService = tracker.getService(IGDBProcesses.class);
fCommandFactory = tracker.getService(IMICommandControl.class).getCommandFactory(); fCommandFactory = fGdbControl.getCommandFactory();
fBreakpointsDmc = (IBreakpointsTargetDMContext)fGdbControl.getContext(); fBreakpointsDmc = (IBreakpointsTargetDMContext)fGdbControl.getContext();
tracker.dispose(); tracker.dispose();
@ -103,7 +102,7 @@ public class SyncUtil {
fSession.getExecutor().submit(runnable).get(); fSession.getExecutor().submit(runnable).get();
} }
public static MIStoppedEvent step(int numSteps, final StepType stepType) throws Throwable { public static MIStoppedEvent step(int numSteps, StepType stepType) throws Throwable {
MIStoppedEvent retVal = null; MIStoppedEvent retVal = null;
for (int i=0; i<numSteps; i++) { for (int i=0; i<numSteps; i++) {
retVal = step(stepType, DefaultTimeouts.get(ETimeout.step)); retVal = step(stepType, DefaultTimeouts.get(ETimeout.step));
@ -119,16 +118,16 @@ public class SyncUtil {
return retVal; return retVal;
} }
public static MIStoppedEvent step(final StepType stepType) throws Throwable { public static MIStoppedEvent step(StepType stepType) throws Throwable {
return step(stepType, DefaultTimeouts.get(ETimeout.step)); return step(stepType, DefaultTimeouts.get(ETimeout.step));
} }
public static MIStoppedEvent step(final StepType stepType, int timeout) throws Throwable { public static MIStoppedEvent step(StepType stepType, int timeout) throws Throwable {
IContainerDMContext containerDmc = SyncUtil.getContainerContext(); IContainerDMContext containerDmc = SyncUtil.getContainerContext();
return step(containerDmc, stepType, timeout); return step(containerDmc, stepType, timeout);
} }
public static MIStoppedEvent step(final IExecutionDMContext dmc, final StepType stepType) throws Throwable { public static MIStoppedEvent step(IExecutionDMContext dmc, StepType stepType) throws Throwable {
return step(dmc, stepType, DefaultTimeouts.get(ETimeout.step)); return step(dmc, stepType, DefaultTimeouts.get(ETimeout.step));
} }
@ -154,7 +153,7 @@ public class SyncUtil {
fGdbControl.queueCommand(fCommandFactory.createMIExecFinish(fStack.createFrameDMContext(dmc, 0)), null); fGdbControl.queueCommand(fCommandFactory.createMIExecFinish(fStack.createFrameDMContext(dmc, 0)), null);
break; break;
default: default:
Assert.assertTrue("Unsupported step type; " + stepType.toString(), false); fail("Unsupported step type; " + stepType.toString());
} }
} }
}); });
@ -163,13 +162,13 @@ public class SyncUtil {
return eventWaitor.waitForEvent(timeout); return eventWaitor.waitForEvent(timeout);
} }
public static MIStoppedEvent runToLine(final IExecutionDMContext dmc, final String fileName, final String lineNo, public static MIStoppedEvent runToLine(IExecutionDMContext dmc, String fileName, String lineNo,
final boolean skipBreakpoints) throws Throwable { boolean skipBreakpoints) throws Throwable {
return runToLine(dmc, fileName, lineNo, skipBreakpoints, DefaultTimeouts.get(ETimeout.runToLine)); return runToLine(dmc, fileName, lineNo, skipBreakpoints, DefaultTimeouts.get(ETimeout.runToLine));
} }
public static MIStoppedEvent runToLine(final IExecutionDMContext dmc, final String fileName, final String lineNo, public static MIStoppedEvent runToLine(final IExecutionDMContext dmc, final String fileName, final String lineNo,
final boolean skipBreakpoints, int timeout) throws Throwable { boolean skipBreakpoints, int timeout) throws Throwable {
final ServiceEventWaitor<MIStoppedEvent> eventWaitor = final ServiceEventWaitor<MIStoppedEvent> eventWaitor =
new ServiceEventWaitor<MIStoppedEvent>( new ServiceEventWaitor<MIStoppedEvent>(
@ -191,87 +190,68 @@ public class SyncUtil {
return eventWaitor.waitForEvent(timeout); return eventWaitor.waitForEvent(timeout);
} }
public static MIStoppedEvent runToLine(final String fileName, final String lineNo, public static MIStoppedEvent runToLine(String fileName, String lineNo,
final boolean skipBreakpoints) throws Throwable { boolean skipBreakpoints) throws Throwable {
return runToLine(fileName, lineNo, skipBreakpoints, DefaultTimeouts.get(ETimeout.runToLine)); return runToLine(fileName, lineNo, skipBreakpoints, DefaultTimeouts.get(ETimeout.runToLine));
} }
public static MIStoppedEvent runToLine(final String fileName, final String lineNo, public static MIStoppedEvent runToLine(String fileName, String lineNo,
final boolean skipBreakpoints, int timeout) throws Throwable { boolean skipBreakpoints, int timeout) throws Throwable {
IContainerDMContext containerDmc = SyncUtil.getContainerContext(); IContainerDMContext containerDmc = SyncUtil.getContainerContext();
return runToLine(containerDmc, fileName, lineNo, skipBreakpoints, timeout); return runToLine(containerDmc, fileName, lineNo, skipBreakpoints, timeout);
} }
public static MIStoppedEvent runToLine(final String fileName, final String lineNo) throws Throwable { public static MIStoppedEvent runToLine(String fileName, String lineNo) throws Throwable {
return runToLine(fileName, lineNo, DefaultTimeouts.get(ETimeout.runToLine)); return runToLine(fileName, lineNo, DefaultTimeouts.get(ETimeout.runToLine));
} }
public static MIStoppedEvent runToLine(final String fileName, final String lineNo, int timeout) throws Throwable { public static MIStoppedEvent runToLine(String fileName, String lineNo, int timeout) throws Throwable {
IContainerDMContext containerDmc = SyncUtil.getContainerContext(); return runToLine(fileName, lineNo, false, timeout);
return runToLine(containerDmc, fileName, lineNo, false, timeout);
} }
public static int addBreakpoint(final String location) throws Throwable { public static int addBreakpoint(String location) throws Throwable {
return addBreakpoint(location, DefaultTimeouts.get(ETimeout.addBreakpoint)); return addBreakpoint(location, DefaultTimeouts.get(ETimeout.addBreakpoint));
} }
public static int addBreakpoint(final String location, int timeout) throws Throwable { public static int addBreakpoint(String location, int timeout) throws Throwable {
return addBreakpoint(location, true, timeout); return addBreakpoint(location, true, timeout);
} }
public static int addBreakpoint(final String location, boolean temporary) throws Throwable { public static int addBreakpoint(String location, boolean temporary) throws Throwable {
return addBreakpoint(location, temporary, DefaultTimeouts.get(ETimeout.addBreakpoint)); return addBreakpoint(location, temporary, DefaultTimeouts.get(ETimeout.addBreakpoint));
} }
public static int addBreakpoint(final String location, boolean temporary, int timeout) public static int addBreakpoint(final String location, final boolean temporary, int timeout)
throws Throwable { throws Throwable {
final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();
DataRequestMonitor<MIBreakInsertInfo> addBreakDone = Query<MIBreakInsertInfo> query = new Query<MIBreakInsertInfo>() {
new DataRequestMonitor<MIBreakInsertInfo>(fRunControl.getExecutor(), null) {
@Override @Override
protected void handleCompleted() { protected void execute(DataRequestMonitor<MIBreakInsertInfo> rm) {
if (isSuccess()) { fGdbControl.queueCommand(
wait.setReturnInfo(getData()); fCommandFactory.createMIBreakInsert(fBreakpointsDmc, temporary, false, null, 0, location, 0),
} rm);
wait.waitFinished(getStatus());
} }
}; };
fGdbControl.queueCommand(
fCommandFactory.createMIBreakInsert(fBreakpointsDmc, temporary, false, null, 0, location, 0),
addBreakDone);
wait.waitUntilDone(timeout); fGdbControl.getExecutor().execute(query);
assertTrue(wait.getMessage(), wait.isOK()); MIBreakInsertInfo info = query.get(timeout, TimeUnit.MILLISECONDS);
MIBreakInsertInfo info = (MIBreakInsertInfo) wait.getReturnInfo();
return info.getMIBreakpoints()[0].getNumber(); return info.getMIBreakpoints()[0].getNumber();
} }
public static int[] getBreakpointList(int timeout) throws Throwable { public static int[] getBreakpointList(int timeout) throws Throwable {
Query<MIBreakListInfo> query = new Query<MIBreakListInfo>() {
final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();
DataRequestMonitor<MIBreakListInfo> listDRM =
new DataRequestMonitor<MIBreakListInfo>(fRunControl.getExecutor(), null) {
@Override @Override
protected void handleCompleted() { protected void execute(DataRequestMonitor<MIBreakListInfo> rm) {
if (isSuccess()) { fGdbControl.queueCommand(fCommandFactory.createMIBreakList(fBreakpointsDmc), rm);
wait.setReturnInfo(getData());
}
wait.waitFinished(getStatus());
} }
}; };
fGdbControl.queueCommand(fCommandFactory.createMIBreakList(fBreakpointsDmc), listDRM);
wait.waitUntilDone(timeout); fGdbControl.getExecutor().execute(query);
assertTrue(wait.getMessage(), wait.isOK()); MIBreakListInfo info = query.get(timeout, TimeUnit.MILLISECONDS);
MIBreakpoint[] breakpoints = info.getMIBreakpoints();
MIBreakpoint[] breakpoints = listDRM.getData().getMIBreakpoints();
int[] result = new int[breakpoints.length]; int[] result = new int[breakpoints.length];
for (int i = 0; i < breakpoints.length; i++) { for (int i = 0; i < breakpoints.length; i++) {
result[i] = breakpoints[i].getNumber(); result[i] = breakpoints[i].getNumber();
@ -283,28 +263,18 @@ public class SyncUtil {
deleteBreakpoint(new int[] {breakpointIndex}, timeout); deleteBreakpoint(new int[] {breakpointIndex}, timeout);
} }
public static void deleteBreakpoint(int[] breakpointIndices, int timeout) throws Throwable { public static void deleteBreakpoint(final int[] breakpointIndices, int timeout) throws Throwable {
Query<MIInfo> query = new Query<MIInfo>() {
final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();
DataRequestMonitor<MIInfo> deleteBreakDone =
new DataRequestMonitor<MIInfo>(fRunControl.getExecutor(), null) {
@Override @Override
protected void handleCompleted() { protected void execute(DataRequestMonitor<MIInfo> rm) {
if (isSuccess()) { fGdbControl.queueCommand(
wait.setReturnInfo(getData()); fCommandFactory.createMIBreakDelete(fBreakpointsDmc, breakpointIndices),
} rm);
wait.waitFinished(getStatus());
} }
}; };
fGdbControl.queueCommand(
fCommandFactory.createMIBreakDelete(fBreakpointsDmc, breakpointIndices), //$NON-NLS-1$
deleteBreakDone);
wait.waitUntilDone(timeout); fGdbControl.getExecutor().execute(query);
assertTrue(wait.getMessage(), wait.isOK()); query.get(timeout, TimeUnit.MILLISECONDS);
} }
@ -353,7 +323,7 @@ public class SyncUtil {
} }
}); });
// Wait for the execution to suspend after the step // Wait for the execution to start after the step
return eventWaitor.waitForEvent(timeout); return eventWaitor.waitForEvent(timeout);
} }
@ -370,6 +340,14 @@ public class SyncUtil {
return waitForStop(DefaultTimeouts.get(ETimeout.waitForStop)); return waitForStop(DefaultTimeouts.get(ETimeout.waitForStop));
} }
// This method is risky. If the command to resume/step execution
// is sent and the stopped event is received before we call this method
// here, then we will miss the stopped event.
// Normally, one shoudl initialize the ServiveEventWaitor before
// triggering the resume to make sure not to miss the stopped event.
// However, in some case this method will still work, for instance
// if there is a sleep in the code between the resume and the time
// it stops; this will give us plenty of time to call this method.
public static MIStoppedEvent waitForStop(int timeout) throws Throwable { public static MIStoppedEvent waitForStop(int timeout) throws Throwable {
final ServiceEventWaitor<MIStoppedEvent> eventWaitor = final ServiceEventWaitor<MIStoppedEvent> eventWaitor =
new ServiceEventWaitor<MIStoppedEvent>( new ServiceEventWaitor<MIStoppedEvent>(
@ -380,11 +358,11 @@ public class SyncUtil {
return eventWaitor.waitForEvent(timeout); return eventWaitor.waitForEvent(timeout);
} }
public static MIStoppedEvent runToLocation(final String location) throws Throwable { public static MIStoppedEvent runToLocation(String location) throws Throwable {
return runToLocation(location, DefaultTimeouts.get(ETimeout.runToLocation)); return runToLocation(location, DefaultTimeouts.get(ETimeout.runToLocation));
} }
public static MIStoppedEvent runToLocation(final String location, int timeout) throws Throwable { public static MIStoppedEvent runToLocation(String location, int timeout) throws Throwable {
// Set a temporary breakpoint and run to it. // Set a temporary breakpoint and run to it.
// Note that if there were other breakpoints set ahead of this one, // Note that if there were other breakpoints set ahead of this one,
// they will stop execution earlier than planned // they will stop execution earlier than planned
@ -393,10 +371,10 @@ public class SyncUtil {
} }
public static IFrameDMContext getStackFrame(final IExecutionDMContext execCtx, final int level) throws Throwable { public static IFrameDMContext getStackFrame(final IExecutionDMContext execCtx, final int level) throws Throwable {
class StackFrameQuery extends Query<IFrameDMContext> { Query<IFrameDMContext> query = new Query<IFrameDMContext>() {
@Override @Override
protected void execute(final DataRequestMonitor<IFrameDMContext> rm) { protected void execute(final DataRequestMonitor<IFrameDMContext> rm) {
fStack.getFrames(execCtx, new DataRequestMonitor<IFrameDMContext[]>(fSession.getExecutor(), rm) { fStack.getFrames(execCtx, new DataRequestMonitor<IFrameDMContext[]>(ImmediateExecutor.getInstance(), rm) {
@Override @Override
protected void handleSuccess() { protected void handleSuccess() {
if (getData().length > level) { if (getData().length > level) {
@ -408,11 +386,10 @@ public class SyncUtil {
} }
}); });
} }
} };
StackFrameQuery sfQuery = new StackFrameQuery(); fSession.getExecutor().execute(query);
fSession.getExecutor().execute(sfQuery); return query.get(500, TimeUnit.MILLISECONDS);
return sfQuery.get();
} }
public static IFrameDMData getFrameData(final IExecutionDMContext execCtx, final int level) throws Throwable { public static IFrameDMData getFrameData(final IExecutionDMContext execCtx, final int level) throws Throwable {
@ -571,35 +548,37 @@ public class SyncUtil {
public static IContainerDMContext getContainerContext() throws InterruptedException { public static IContainerDMContext getContainerContext() throws InterruptedException {
assert !fProcessesService.getExecutor().isInExecutorThread(); assert !fProcessesService.getExecutor().isInExecutorThread();
final AsyncCompletionWaitor waitor = new AsyncCompletionWaitor(); Query<IContainerDMContext> query = new Query<IContainerDMContext>() {
@Override
fProcessesService.getExecutor().submit(new Runnable() { protected void execute(final DataRequestMonitor<IContainerDMContext> rm) {
public void run() { fProcessesService.getProcessesBeingDebugged(
fProcessesService.getProcessesBeingDebugged(
fGdbControl.getContext(), fGdbControl.getContext(),
new DataRequestMonitor<IDMContext[]>(fProcessesService.getExecutor(), null) { new DataRequestMonitor<IDMContext[]>(ImmediateExecutor.getInstance(), null) {
@Override @Override
protected void handleCompleted() { protected void handleCompleted() {
if (isSuccess()) { if (isSuccess()) {
IDMContext[] contexts = getData(); IDMContext[] contexts = getData();
Assert.assertNotNull("invalid return value from service", contexts); Assert.assertNotNull("invalid return value from service", contexts);
Assert.assertEquals("unexpected number of processes", 1, contexts.length); Assert.assertEquals("unexpected number of processes", 1, contexts.length);
IDMContext context = contexts[0]; IDMContext context = contexts[0];
Assert.assertNotNull("unexpected process context type ", context); Assert.assertNotNull("unexpected process context type ", context);
waitor.setReturnInfo(context); rm.setData((IContainerDMContext)context);
waitor.waitFinished(); } else {
} else { rm.setStatus(getStatus());
waitor.waitFinished(getStatus()); }
} rm.done();
} }
}); });
} }
}); };
waitor.waitUntilDone(TestsPlugin.massageTimeout(2000)); fGdbControl.getExecutor().execute(query);
Assert.assertTrue(waitor.getMessage(), waitor.isOK()); try {
return (IContainerDMContext)waitor.getReturnInfo(); return query.get(TestsPlugin.massageTimeout(2000), TimeUnit.MILLISECONDS);
} catch (Exception e) {
fail(e.getMessage());
}
return null;
} }
/** /**