[CVS spice] Unit test blocking event sources
pdonald-yCVjj/[email protected] 17 May 2004 03:52:24 -0000
| Newsgroups | gmane.comp.java.spice.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit in spice/sandbox/event/src on MAIN
java/org/codehaus/spice/event/impl/BlockingEventSource.java +78 -58 1.2 -> 1.3
test/org/codehaus/spice/event/impl/BlockingEventSourceTestCase.java +138 added 1.1
/BlockingTestEventSource.java +45 added 1.1
+261 -58
2 added + 1 modified, total 3 files
Unit test blocking event sources
----------
spice/sandbox/event/src/java/org/codehaus/spice/event/impl
BlockingEventSource.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- BlockingEventSource.java 18 Mar 2004 03:39:50 -0000 1.2
+++ BlockingEventSource.java 17 May 2004 03:52:23 -0000 1.3
@@ -7,69 +7,89 @@
* retrieve events from the underlying EventSource.
*
* @author Peter Donald
- * @version $Revision: 1.2 $ $Date: 2004/03/18 03:39:50 $
+ * @version $Revision: 1.3 $ $Date: 2004/05/17 03:52:23 $
*/
public class BlockingEventSource
- implements EventSource
+ implements EventSource
{
- /**
- * The underlying EventSource to retrieve events from.
- */
- private final EventSource _source;
+ /**
+ * The underlying EventSource to retrieve events from.
+ */
+ private final EventSource m_source;
- /**
- * Create an instance.
- *
- * @param source the source to wrap.
- */
- public BlockingEventSource( final EventSource source )
- {
- if( null == source )
- {
- throw new NullPointerException( "source" );
- }
- _source = source;
- }
+ /**
+ * The duration to wait before unblocking.
+ */
+ private long m_duration;
- /**
- * @see EventSource#getEvent()
- */
- public Object getEvent()
- {
- final Object lock = getSourceLock();
- Object result = _source.getEvent();
- while( null == result )
- {
- synchronized( lock )
- {
- result = _source.getEvent();
- }
- }
- return result;
- }
+ /**
+ * Create an instance.
+ *
+ * @param source the source to wrap.
+ */
+ public BlockingEventSource( final EventSource source, final long duration )
+ {
+ if( null == source )
+ {
+ throw new NullPointerException( "source" );
+ }
+ m_source = source;
+ m_duration = duration;
+ }
- /**
- * @see EventSource#getEvents(int)
- */
- public Object[] getEvents( final int count )
- {
- final Object lock = getSourceLock();
- Object[] results = _source.getEvents( count );
- while( 0 == results.length )
- {
- synchronized( lock )
- {
- results = _source.getEvents( count );
- }
- }
- return results;
- }
+ /**
+ * @see EventSource#getEvent()
+ */
+ public Object getEvent()
+ {
+ final Object lock = getSourceLock();
+ synchronized( lock )
+ {
+ Object result = m_source.getEvent();
+ if( null == result )
+ {
+ try
+ {
+ lock.wait( m_duration );
+ }
+ catch( final InterruptedException e )
+ {
+ }
+ result = m_source.getEvent();
+ }
+ return result;
+ }
+ }
- /**
- * @see EventSource#getSourceLock()
- */
- public Object getSourceLock()
- {
- return _source.getSourceLock();
- }
+ /**
+ * @see EventSource#getEvents(int)
+ */
+ public Object[] getEvents( final int count )
+ {
+ final Object lock = getSourceLock();
+ synchronized( lock )
+ {
+ Object[] results = m_source.getEvents( count );
+ if( 0 == results.length )
+ {
+ try
+ {
+ lock.wait( m_duration );
+ }
+ catch( final InterruptedException ie )
+ {
+ }
+ results = m_source.getEvents( count );
+ }
+ return results;
+ }
+ }
+
+ /**
+ * @see EventSource#getSourceLock()
+ */
+ public Object getSourceLock()
+ {
+ return m_source.getSourceLock();
+ }
}
----------
spice/sandbox/event/src/test/org/codehaus/spice/event/impl
BlockingEventSourceTestCase.java added at 1.1
diff -N BlockingEventSourceTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BlockingEventSourceTestCase.java 17 May 2004 03:52:23 -0000 1.1
@@ -0,0 +1,138 @@
+package org.codehaus.spice.event.impl;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Peter Donald
+ * @version $Revision: 1.1 $ $Date: 2004/05/17 03:52:23 $
+ */
+public class BlockingEventSourceTestCase
+ extends TestCase
+{
+ private Object m_result;
+ private boolean m_started;
+ private boolean m_returned;
+
+ public void testNull_buffer_PassedIntoCtor()
+ throws Exception
+ {
+ try
+ {
+ new BlockingEventSource( null, 0 );
+ }
+ catch( final NullPointerException npe )
+ {
+ assertEquals( "npe.getMessage()", "source", npe.getMessage() );
+ return;
+ }
+ fail( "Expected a NPE when passing source into Ctor" );
+ }
+
+ public void testEternallyBlocked()
+ throws Exception
+ {
+ final BlockingTestEventSource base = new BlockingTestEventSource();
+ final BlockingEventSource source = new BlockingEventSource( base, 0 );
+
+ final Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ m_result = source.getEvent();
+ }
+ };
+
+ final Thread thread = startThread( runnable );
+ Thread.sleep( 30 );
+ assertEquals( "Eternally Blocking - Locked - has returned?",
+ false,
+ m_returned );
+ assertEquals( "Eternally Blocking - Locked",
+ null,
+ m_result );
+ base.unlock();
+ Thread.sleep( 2 );
+ assertEquals( "Eternally Blocking - UnLocked - has returned?",
+ true,
+ m_returned );
+ assertEquals( "Eternally Blocking - UnLocked",
+ BlockingTestEventSource.EVENT,
+ m_result );
+ destroy( thread );
+ }
+
+ private void destroy( final Thread thread )
+ throws InterruptedException
+ {
+ thread.interrupt();
+ thread.join();
+ }
+
+ public void testTimeBlocked()
+ throws Exception
+ {
+ final BlockingTestEventSource base = new BlockingTestEventSource();
+ final BlockingEventSource source = new BlockingEventSource( base, 200 );
+
+ final Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ m_result = source.getEvent();
+ }
+ };
+
+ final Thread thread = startThread( runnable );
+ Thread.sleep( 30 );
+ assertEquals( "Pre-Timeout Blocking - Locked - has returned?",
+ false,
+ m_returned );
+ assertEquals( "Pre-Timeout Blocking - Locked",
+ null,
+ m_result );
+ Thread.sleep( 300 );
+ assertEquals( "Post-Timeout Blocking has returned?",
+ true,
+ m_returned );
+ assertEquals( "Post-Timeout Blocking",
+ null,
+ m_result );
+ destroy( thread );
+
+ final Thread thread2 = startThread( runnable );
+ base.unlock();
+ assertEquals( "UnLocked - has returned?",
+ true,
+ m_returned );
+ assertEquals( "UnLocked result?",
+ BlockingTestEventSource.EVENT,
+ m_result );
+ destroy( thread2 );
+ }
+
+ private Thread startThread( final Runnable runnable )
+ throws InterruptedException
+ {
+ final Runnable work = new Runnable()
+ {
+ public void run()
+ {
+ m_started = true;
+ runnable.run();
+ m_returned = true;
+ }
+ };
+
+ m_result = null;
+ m_returned = false;
+ m_started = false;
+ final Thread thread = new Thread( work );
+ thread.start();
+ while( false == m_started )
+ {
+ Thread.sleep( 2 );
+ }
+
+ return thread;
+ }
+}
----------
spice/sandbox/event/src/test/org/codehaus/spice/event/impl
BlockingTestEventSource.java added at 1.1
diff -N BlockingTestEventSource.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BlockingTestEventSource.java 17 May 2004 03:52:23 -0000 1.1
@@ -0,0 +1,45 @@
+package org.codehaus.spice.event.impl;
+
+import org.codehaus.spice.event.EventSource;
+
+class BlockingTestEventSource
+ implements EventSource
+{
+ static final Object EVENT = new Object();
+ static final Object[] EVENTS = new Object[] {EVENT};
+
+ private final Object m_lock = new Object();
+ private boolean m_unlocked;
+
+ void unlock()
+ {
+ m_unlocked = true;
+ synchronized( m_lock )
+ {
+ m_lock.notifyAll();
+ }
+ }
+
+ public Object getEvent()
+ {
+ if( m_unlocked )
+ {
+ return EVENT;
+ }
+ return null;
+ }
+
+ public Object[] getEvents( int count )
+ {
+ if( m_unlocked )
+ {
+ return EVENTS;
+ }
+ return new Object[0];
+ }
+
+ public Object getSourceLock()
+ {
+ return m_lock;
+ }
+}
CVSspam 0.2.8