KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > event > command > test > TPCThreadManagerTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.event.command.test;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
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 /**
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  */

37 public class TPCThreadManagerTestCase extends TestCase
38 {
39     /**
40      * Constructor for JUnit
41      *
42      * @param name The name of the test
43      */

44     public TPCThreadManagerTestCase( String JavaDoc name )
45     {
46         super( name );
47     }
48
49     // number of milliseconds it reasonably takes the JVM to switch threads
50
private final static int SCHEDULING_TIMEOUT = 1000; // ms
51

52     // number of times the handler should be called
53
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     /**
66      * Checks TPCThreadManager ability to survive the situation when
67      * it tries to schedule more tasks than it has threads. Originally
68      * it was dying due to hitting Pool limit and not catching the
69      * resulting runtime exception.
70      * <p>
71      * The test is not foolproof, it probably depends on preemtive
72      * threads management.
73      *
74      * @throws Exception on error
75      */

76     public void testThreadContention() throws Exception JavaDoc
77     {
78         // enforces only 1 thread and no timeout which makes it
79
// fail quickly
80
final TPCThreadManager threadManager = new TPCThreadManager();
81
82         threadManager.parameterize( createParameters( 1, 0 ) );
83         threadManager.initialize();
84
85         // an obviously syncronized component
86
final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
87         final StringWriter JavaDoc exceptionBuffer = new StringWriter JavaDoc();
88         final PrintWriter JavaDoc errorOut = new PrintWriter JavaDoc( exceptionBuffer );
89
90         threadManager.register( new Pipeline( result, errorOut ) );
91
92         // sleeps for 1 more scheduling timeout to surely go over limit
93
Thread.sleep( SCHEDULING_TIMEOUT * ( MINIMAL_NUMBER_INVOCATIONS + 1 ) );
94
95         int numberCalls = result.length();
96
97         String JavaDoc 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(); // why not?
105

106         String JavaDoc 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 JavaDoc m_result;
118         private final PrintWriter JavaDoc m_errorOut;
119
120         Pipeline( StringBuffer JavaDoc resultAccumulator, PrintWriter JavaDoc errorOut )
121             throws SinkException
122         {
123             m_result = resultAccumulator;
124             m_errorOut = errorOut;
125             // even though TPCThreadManager currently calls event handlers
126
// when there is nothing to do, that may change
127
m_queue.enqueue( new Object JavaDoc()
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 JavaDoc element )
148         {
149             handleEvents( new Object JavaDoc[]{element} );
150         }
151
152         public void handleEvents( Object JavaDoc[] elements )
153         {
154             // records the fact that the handler was called
155
m_result.append( 'a' );
156             try
157             {
158                 // sleeps to occupy the thread and let thread manager try to reschedule
159
Thread.sleep( SCHEDULING_TIMEOUT );
160                 // enqueues another element to be called again
161
m_queue.enqueue( new Object JavaDoc()
162                 {
163                 } );
164             }
165             catch( Exception JavaDoc e )
166             {
167                 // fails the test, no exceptions are expected
168
e.printStackTrace( m_errorOut );
169
170             }
171         }
172     }
173 }
174
Popular Tags