com.google.testing.threadtester
Class AbstractBreakpoint

java.lang.Object
  extended by com.google.testing.threadtester.AbstractBreakpoint
All Implemented Interfaces:
Breakpoint
Direct Known Subclasses:
SinglePositionBreakpoint

public abstract class AbstractBreakpoint
extends Object
implements Breakpoint

Base class for implementations of Breakpoint. Provides the basic functionality for pausing and resuming, but does not provide any mechanism for recognising when a break point has been reached. Concrete subclasses must provide a mechanism for recognising when a breakpoint has been reached. Upon reaching a breakpoint, they should call hitBreakpoint().

Author:
alasdair.mackintosh@gmail.com (Alasdair Mackintosh)

Field Summary
protected  boolean blocked
          Set to true if the thread breakpoint is curently blocked.
protected  boolean enabled
          Set to true if this breakpoint is curently enabled.
protected  int limit
          The number of times this breakpoint can be hit before activating.
protected  int numHits
          The number of times this breakpoint has been hit.
protected  Thread thread
          The thread associated with this breakpoint.
 
Constructor Summary
protected AbstractBreakpoint()
          Creates a new breakpoint that is not initially associated with a thread.
protected AbstractBreakpoint(Thread thread)
          Creates a new breakpoint associated with the given thread.
 
Method Summary
 void await()
          Waits for this breakpoint to be reached.
 void disable()
          Disables the Breakpoint.
 void enable()
          Enables the Breakpoint.
protected  void finishWaiting()
           
 int getHits()
          Gets the number of times that this Breakpoint has been hit.
protected  int getId()
          Returns a unique integer specifying this breakpoint.
 int getLimit()
          Gets the limit.
 Thread getThread()
          Gets the thread associated with this Breakpoint, if any.
protected  void hitBreakpoint()
          This method should be invoked by subclasses when the Breakpoint has been hit.
 boolean isBlocked()
          Returns true if the associated thread is currently blocked at this breakpoint.
 boolean isEnabled()
          Returns true if this breakpoint is enabled.
protected  void reset()
          Resets the breakpoint.
 void resume()
          Tells the blocked thread to continue.
 void resume(Breakpoint nextBreak)
          Tells the blocked thread to continue, and then wait for the given breakpoint.
 void setHandler(BreakpointHandler handler)
          Sets a handler for this Breakpoint.
 void setLimit(int count)
          Sets the limit.
protected  void setThreadImpl(Thread newThread)
          Sets the thread associated with this breakpoint, and calls reset().
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

enabled

protected volatile boolean enabled
Set to true if this breakpoint is curently enabled. Defaults to true.


limit

protected volatile int limit
The number of times this breakpoint can be hit before activating. Default value is 1.


numHits

protected volatile int numHits
The number of times this breakpoint has been hit.


blocked

protected volatile boolean blocked
Set to true if the thread breakpoint is curently blocked.


thread

protected volatile Thread thread
The thread associated with this breakpoint. See getThread().

Constructor Detail

AbstractBreakpoint

protected AbstractBreakpoint(Thread thread)
Creates a new breakpoint associated with the given thread.


AbstractBreakpoint

protected AbstractBreakpoint()
Creates a new breakpoint that is not initially associated with a thread. The thread should be set with setThreadImpl(Thread) before using this breakpoint.

Method Detail

reset

protected void reset()
Resets the breakpoint. After resetting, the breakpoint will be enabled, with a limit of 1, and with the number of hits set to 0.

Throws:
IllegalStateException - if this breakpoint has already been hit.

getThread

public Thread getThread()
Description copied from interface: Breakpoint
Gets the thread associated with this Breakpoint, if any. Some Breakpoints may only be valid for a given thread, and will not block when reached by another thread. Consult the documentation for the specific Breakpoint type for details. If this Breakpoint is not associated with a thread, returns null.

Specified by:
getThread in interface Breakpoint

setThreadImpl

protected void setThreadImpl(Thread newThread)
Sets the thread associated with this breakpoint, and calls reset(). May be used by implementations of ReusableBreakpoint. Note that the thread cannot be reset if the original thread is blocked at this breakpoint. (I.e. if the breakpoint has been hit, but resume(com.google.testing.threadtester.Breakpoint) has not been called.)


setHandler

public void setHandler(BreakpointHandler handler)
Description copied from interface: Breakpoint
Sets a handler for this Breakpoint. If a handler is set, then when the breakpoint is reached it will invoke the handler's BreakpointHandler.handleBreakpoint(com.google.testing.threadtester.Breakpoint) method.

Specified by:
setHandler in interface Breakpoint

getHits

public int getHits()
Gets the number of times that this Breakpoint has been hit. A Breakpoint with a limit > 1 will be hit more than once before finally blocking. Note that a Breakpoint that is not enabled will not be considered to have been hit.

See Also:
Breakpoint.getLimit(), Breakpoint.isEnabled()

getLimit

public int getLimit()
Description copied from interface: Breakpoint
Gets the limit. This is the initial limit value that was set in the call to Breakpoint.setLimit(int).

Specified by:
getLimit in interface Breakpoint

setLimit

public void setLimit(int count)
Description copied from interface: Breakpoint
Sets the limit. This determines how many times the breakpoint must be hit before it will stop. The default limit is 1, meaning that the breakpoint will stop as soon as it is reached. Setting the limit to N will mean that the breakpoint will not stop the first N-1 times it is reached.

Note that once a breakpoint has stopped, it is no longer active, and will not stop again.

Specified by:
setLimit in interface Breakpoint

isEnabled

public final boolean isEnabled()
Description copied from interface: Breakpoint
Returns true if this breakpoint is enabled. By default, a Breakpoint is active when it is created, but can be deactivated by Breakpoint.disable(). A Breakpoint's thread will only stop if the Breakpoint is enabled.

Specified by:
isEnabled in interface Breakpoint

disable

public void disable()
Description copied from interface: Breakpoint
Disables the Breakpoint. If a breakpoint is not enabled, then its thread will not stop when the breakpoint is reached.

Specified by:
disable in interface Breakpoint

enable

public void enable()
Description copied from interface: Breakpoint
Enables the Breakpoint. If a breakpoint is enabled, then its thread will stop when the breakpoint is reached. Breakpoints are enabled when created, but may be disabled by a call to Breakpoint.disable().

Specified by:
enable in interface Breakpoint

isBlocked

public boolean isBlocked()
Description copied from interface: Breakpoint
Returns true if the associated thread is currently blocked at this breakpoint.

Specified by:
isBlocked in interface Breakpoint

await

public void await()
           throws TestTimeoutException
Description copied from interface: Breakpoint
Waits for this breakpoint to be reached. The caller thread will block until the breakpoint is reached. If the executing thread is already stopped at this breakpoint, then this method will return immediately.

It is the caller's responsibility to ensure that this breakpoint is reachable in the current environment. Waiting for a breakpoint that is not reached will eventually throw a TestTimeoutException.

Specified by:
await in interface Breakpoint
Throws:
TestTimeoutException - if this breakpoint is not reached within the time limit defined by Options.timeout. The thread of the timeout exception will be set to the thread of this breakpoint, not to the thread that called await.

resume

public void resume(Breakpoint nextBreak)
            throws TestTimeoutException
Description copied from interface: Breakpoint
Tells the blocked thread to continue, and then wait for the given breakpoint. This is a shorthand for:
   breakPoint.resume();
   nextBreak.await();
 

Specified by:
resume in interface Breakpoint
Throws:
TestTimeoutException

resume

public void resume()
Description copied from interface: Breakpoint
Tells the blocked thread to continue. This method can only be called after Breakpoint.await() has been successfully called.

Specified by:
resume in interface Breakpoint

finishWaiting

protected final void finishWaiting()

hitBreakpoint

protected void hitBreakpoint()
This method should be invoked by subclasses when the Breakpoint has been hit. It will block the current thread until or unless resume(com.google.testing.threadtester.Breakpoint) is or has been called. Note that calling this method will have no effect if the breakpoint is disabled.

See Also:
isEnabled()

getId

protected int getId()
Returns a unique integer specifying this breakpoint. Used for debugging, and for implementing toString()


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.