1 17 package org.apache.excalibur.event.command.test; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 import junit.framework.TestCase; 23 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.excalibur.event.EventHandler; 26 import org.apache.excalibur.event.Queue; 27 import org.apache.excalibur.event.Sink; 28 import org.apache.excalibur.event.SinkException; 29 import org.apache.excalibur.event.Source; 30 import org.apache.excalibur.event.command.EventPipeline; 31 import org.apache.excalibur.event.command.TPCThreadManager; 32 import org.apache.excalibur.event.impl.DefaultQueue; 33 34 37 public class TPCThreadManagerTestCase extends TestCase 38 { 39 44 public TPCThreadManagerTestCase( String name ) 45 { 46 super( name ); 47 } 48 49 private final static int SCHEDULING_TIMEOUT = 1000; 52 private final static int MINIMAL_NUMBER_INVOCATIONS = 2; 54 55 private Parameters createParameters( int threadsPerProcessor, long sleep ) 56 { 57 final Parameters parameters = new Parameters(); 58 59 parameters.setParameter( "threads-per-processor", String.valueOf( threadsPerProcessor ) ); 60 parameters.setParameter( "sleep-time", String.valueOf( sleep ) ); 61 62 return parameters; 63 } 64 65 76 public void testThreadContention() throws Exception 77 { 78 final TPCThreadManager threadManager = new TPCThreadManager(); 81 82 threadManager.parameterize( createParameters( 1, 0 ) ); 83 threadManager.initialize(); 84 85 final StringBuffer result = new StringBuffer (); 87 final StringWriter exceptionBuffer = new StringWriter (); 88 final PrintWriter errorOut = new PrintWriter ( exceptionBuffer ); 89 90 threadManager.register( new Pipeline( result, errorOut ) ); 91 92 Thread.sleep( SCHEDULING_TIMEOUT * ( MINIMAL_NUMBER_INVOCATIONS + 1 ) ); 94 95 int numberCalls = result.length(); 96 97 String msg = 98 "Number of calls to handler (" + numberCalls + 99 ") is less than the expected number of calls (" + 100 MINIMAL_NUMBER_INVOCATIONS + ")"; 101 102 assertTrue( msg, numberCalls >= MINIMAL_NUMBER_INVOCATIONS ); 103 104 errorOut.flush(); 106 String stackTrace = exceptionBuffer.toString(); 107 108 assertEquals( "Exceptions while running the test", 109 "", 110 stackTrace ); 111 } 112 113 private static class Pipeline implements EventPipeline, EventHandler 114 { 115 private final Queue m_queue = new DefaultQueue(); 116 private final Source[] m_sources = new Source[]{m_queue}; 117 private final StringBuffer m_result; 118 private final PrintWriter m_errorOut; 119 120 Pipeline( StringBuffer resultAccumulator, PrintWriter errorOut ) 121 throws SinkException 122 { 123 m_result = resultAccumulator; 124 m_errorOut = errorOut; 125 m_queue.enqueue( new Object () 128 { 129 } ); 130 } 131 132 public EventHandler getEventHandler() 133 { 134 return this; 135 } 136 137 public final Source[] getSources() 138 { 139 return m_sources; 140 } 141 142 public final Sink getSink() 143 { 144 return m_queue; 145 } 146 147 public void handleEvent( Object element ) 148 { 149 handleEvents( new Object []{element} ); 150 } 151 152 public void handleEvents( Object [] elements ) 153 { 154 m_result.append( 'a' ); 156 try 157 { 158 Thread.sleep( SCHEDULING_TIMEOUT ); 160 m_queue.enqueue( new Object () 162 { 163 } ); 164 } 165 catch( Exception e ) 166 { 167 e.printStackTrace( m_errorOut ); 169 170 } 171 } 172 } 173 } 174 | Popular Tags |