/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.SimpleCollector;
import org.jicarilla.plumbing.SimpleSource;
import org.jicarilla.plumbing.SimpleCollector;
import org.jicarilla.plumbing.SimpleSource;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleCollector.
*
* @todo this test just deadlocked on me. Figure out what's going on.
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleCollectorTestCase.java,v 1.1 2003/11/18 16:09:16 lsimons
* Exp $
*/
public class SimpleCollectorTestCase extends TestCase
{
public void testAdd()
{
Collector c = new Collector();
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
assertEquals( c.getSources().get( 0 ), s );
assertEquals( c.getSources().size(), 1 );
assertEquals( c.getSources().size(), c.getSize() );
LinkedQueue l2 = new LinkedQueue();
SimpleSource s2 = new SimpleSource( l2 );
c.addSource( s2 );
Throwable t = null;
try
{
c.addSource( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Collector c = new Collector();
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testTake() throws InterruptedException
{
Collector c = new Collector();
LinkedQueue l = new Q();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
l.put( o1 );
l.put( o2 );
l.put( o3 );
l.put( o4 );
assertEquals( o1, c.take() );
assertEquals( o2, c.take() );
assertEquals( o3, c.take() );
assertEquals( o4, c.take() );
LinkedQueue l2 = new LinkedQueue();
SimpleSource s2 = new SimpleSource( l2 );
c.addSource( s2 );
l2.put( o3 );
assertEquals( o3, c.take() );
l.put( null );
l2.put( o1 );
assertEquals( o1, c.take() );
}
boolean interrupted = false;
public void testBlockOnTake() throws InterruptedException
{
final Collector c = new Collector();
final LinkedQueue l = new LinkedQueue();
final SimpleSource s = new SimpleSource( l );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
Thread thread = new Thread( new Runnable()
{
public void run()
{
try
{
c.take(); // should block, then be interrupted
}
catch( InterruptedException ie )
{
interrupted = true;
}
}
} );
thread.start();
Thread.sleep( 1000 );
thread.interrupt();
Thread.sleep( 1000 );
assertTrue( interrupted );
}
public static class Q extends LinkedQueue
{
public void put( Object o ) throws InterruptedException
{
if( o == null )
{
super.insert( o );
}
else
{
super.put( o );
}
}
}
public void testPoll() throws InterruptedException
{
Collector c = new Collector();
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
l.put( o1 );
l.put( o2 );
l.put( o3 );
l.put( o4 );
assertEquals( o1, c.poll( 10 ) );
assertEquals( o2, c.poll( 20 ) );
assertEquals( o3, c.poll( 100 ) );
assertEquals( o4, c.poll( 50 ) );
assertNull( c.poll( 100 ) );
}
public static class Collector extends SimpleCollector
{
public List getSources()
{
return m_sources;
}
public int getSize()
{
return m_sources.size();
}
}
}
|