Skip to content

Commit 2859c6e

Browse files
committed
Need for a check whether the proxy is usable, i.e. whether it is started and not finished. Introucing #isReady + sanity test.
1 parent a946a0a commit 2859c6e

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

CHANGES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Some progress. Code cleanup, API changes (increasing middle version of
44
trunk).
55
* RubyDebuggerProxy
6-
* use isFinished instead of checkConnection, getting rid of 'connected'
6+
* use #isReady instead of #checkConnection
77
* s/connect/setDebugTarget (this what it actually do now)
88
* relaxing addBreakpoint, to emit warning instead of exception (debuggee
99
could die)

src/org/rubyforge/debugcommons/RubyDebuggerProxy.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static enum DebuggerType { CLASSIC_DEBUGGER, RUBY_DEBUG }
4545
private final DebuggerType debuggerType;
4646
private RubyDebugTarget debugTarged;
4747
private Socket commandSocket;
48+
private boolean started;
4849
private boolean finished;
4950

5051
private PrintWriter commandWriter;
@@ -112,16 +113,17 @@ public void startDebugging(final IRubyBreakpoint[] initialBreakpoints) throws Ru
112113
startSuspensionReaderLoop();
113114
}
114115

116+
115117
/**
116-
* Whether client might send command to the proxy. When the debuggee
117-
* finished (in a standard manner or unexpectedly, e.g. was killed) this
118-
* returns false.
118+
* Whether client might send command to the proxy. When the debuggee has
119+
* finished (in a standard manner or unexpectedly, e.g. was killed) or the
120+
* proxy did not start yet, false is returned.
119121
*/
120-
public synchronized boolean isFinished() {
121-
return finished || !getDebugTarged().isRunning();
122+
public synchronized boolean isReady() {
123+
return !finished && commandWriter != null && getDebugTarged().isRunning();
122124
}
123125

124-
private void startClassicDebugger(final IRubyBreakpoint[] initialBreakpoints) throws RubyDebuggerException {
126+
private synchronized void startClassicDebugger(final IRubyBreakpoint[] initialBreakpoints) throws RubyDebuggerException {
125127
try {
126128
commandFactory = new ClassicDebuggerCommandFactory();
127129
readersSupport.startCommandLoop(getCommandSocket().getInputStream());
@@ -133,7 +135,7 @@ private void startClassicDebugger(final IRubyBreakpoint[] initialBreakpoints) th
133135
}
134136
}
135137

136-
private void startRubyDebug(final IRubyBreakpoint[] initialBreakpoints) throws RubyDebuggerException {
138+
private synchronized void startRubyDebug(final IRubyBreakpoint[] initialBreakpoints) throws RubyDebuggerException {
137139
try {
138140
commandFactory = new RubyDebugCommandFactory();
139141
readersSupport.startCommandLoop(getCommandSocket().getInputStream());
@@ -172,8 +174,8 @@ protected void setBreakpoints(final IRubyBreakpoint[] breakpoints) throws RubyDe
172174

173175
public synchronized void addBreakpoint(final IRubyBreakpoint breakpoint) {
174176
LOGGER.fine("Adding breakpoint: " + breakpoint);
175-
if (isFinished()) {
176-
LOGGER.fine("Session and/or debuggee has already finished, skipping addition of breakpoint: " + breakpoint);
177+
if (!isReady()) {
178+
LOGGER.fine("Session and/or debuggee is not ready, skipping addition of breakpoint: " + breakpoint);
177179
return;
178180
}
179181
assert breakpoint != null : "breakpoint cannot be null";
@@ -226,8 +228,8 @@ public synchronized void removeBreakpoint(final IRubyBreakpoint breakpoint) {
226228
*/
227229
public synchronized void removeBreakpoint(final IRubyBreakpoint breakpoint, boolean silent) {
228230
LOGGER.fine("Removing breakpoint: " + breakpoint);
229-
if (isFinished()) {
230-
LOGGER.fine("Session and/or debuggee has already finished, skipping removing of breakpoint: " + breakpoint);
231+
if (!isReady()) {
232+
LOGGER.fine("Session and/or debuggee is not ready, skipping removing of breakpoint: " + breakpoint);
231233
return;
232234
}
233235
if (breakpoint instanceof IRubyLineBreakpoint) {
@@ -307,9 +309,9 @@ public void resume(final RubyThread thread) {
307309

308310
private void sendCommand(final String s) throws RubyDebuggerException {
309311
LOGGER.fine("Sending command debugger: " + s);
310-
if (isFinished()) {
312+
if (!isReady()) {
311313
throw new RubyDebuggerException("Trying to send a command [" + s +
312-
"] to terminated process (debuggee: " + getDebugTarged() + ", output: \n\n" +
314+
"] to non-started or finished proxy (debuggee: " + getDebugTarged() + ", output: \n\n" +
313315
dumpProcess(debugTarged.getProcess()));
314316
}
315317
getCommandWriter().println(s);
@@ -358,7 +360,7 @@ public RubyFrame[] readFrames(RubyThread thread) throws RubyDebuggerException {
358360
sendCommand(commandFactory.createReadFrames(thread));
359361
infos = getReadersSupport().readFrames();
360362
} catch (RubyDebuggerException e) {
361-
if (isFinished()) {
363+
if (!isReady()) {
362364
throw e;
363365
}
364366
infos = new RubyFrameInfo[0];

src/org/rubyforge/debugcommons/model/RubyDebugTarget.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void suspensionOccurred(SuspensionPoint suspensionPoint) {
7272
try {
7373
updateThreads();
7474
} catch (RubyDebuggerException e) {
75-
if (getProxy().isFinished()) {
76-
throw new RuntimeException("Cannot update threads", e);
75+
if (!getProxy().isReady()) {
76+
throw new RuntimeException("Cannot update threads. Proxy is not ready.", e);
7777
} else {
7878
LOGGER.fine("Session has finished. Ignoring unsuccessful thread update.");
7979
return;

test/org/rubyforge/debugcommons/DebuggerTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected void startDebugging(
194194
public void run() {
195195
try {
196196
proxy.startDebugging(breakpoints);
197-
assertFalse("proxy alive", proxy.isFinished());
197+
assertTrue("proxy alive", proxy.isReady());
198198
} catch (RubyDebuggerException e) {
199199
fail("Cannot start debugger: " + e);
200200
}

test/org/rubyforge/debugcommons/RubyDebuggerProxyTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public final class RubyDebuggerProxyTest extends DebuggerTestBase {
1515
public RubyDebuggerProxyTest(String testName) {
1616
super(testName);
1717
}
18+
19+
public void testIsFinished() throws Exception {
20+
final RubyDebuggerProxy proxy = prepareProxy("sleep 0.01");
21+
assertFalse("proxy not ready yet", proxy.isReady());
22+
startDebugging(proxy, new IRubyLineBreakpoint[]{}, 0);
23+
assertTrue("proxy not ready yet", proxy.isReady());
24+
}
1825

1926
public void testBreakpointsRemoving1() throws Exception {
2027
final RubyDebuggerProxy proxy = prepareProxy(

0 commit comments

Comments
 (0)