KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > datastruct > v1 > SynchQueueUTest


1 /*
2  * @(#)SynchQueueUTest.java
3  *
4  * Copyright (C) 2001-2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.datastruct.v1;
28
29 import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
30 import net.sourceforge.groboutils.junit.v1.TestMonitorRunnable;
31 import net.sourceforge.groboutils.junit.v1.TestRunnable;
32
33 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38
39
40 /**
41  *
42  * <H3>Changes made for 0.9.1:</H3>
43  * <UL>
44  * <LI>Added a new test to check the size of the queue during the
45  * threaded part. The size should never go below 0.
46  * </UL>
47  *
48  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
49  * @since March 14, 2001 (Alpha 0.9.0)
50  * @version $Date: 2004/06/08 20:55:40 $
51  */

52 public class SynchQueueUTest extends TestCase
53 {
54     private static final Class JavaDoc THIS_CLASS = SynchQueueUTest.class;
55     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
56     
57     public SynchQueueUTest( String JavaDoc name )
58     {
59         super( name );
60     }
61     
62     public static Test suite()
63     {
64         TestSuite suite = new TestSuite( THIS_CLASS );
65         
66         return suite;
67     }
68     
69     public static void main( String JavaDoc[] args )
70     {
71         String JavaDoc[] name = { THIS_CLASS.getName() };
72         
73         // junit.textui.TestRunner.main( name );
74
// junit.swingui.TestRunner.main( name );
75

76         junit.textui.TestRunner.main( name );
77     }
78     
79     protected void setUp() throws Exception JavaDoc
80     {
81         super.setUp();
82         
83         // set ourself up
84
}
85     
86     
87     protected void tearDown() throws Exception JavaDoc
88     {
89         // tear ourself down
90

91         
92         super.tearDown();
93     }
94     
95     
96     public void testInstantiate1()
97     {
98         Object JavaDoc o = new SynchQueue();
99         assertNotNull( "New should never return null.", o );
100         assertTrue( "Instantiation must be of the correct class.",
101             o instanceof SynchQueue );
102         assertTrue( "Queue needs to be empty immediately after instantiation.",
103             ((SynchQueue)o).isEmpty() );
104     }
105     
106     
107     public void testSimpleAdd1() throws InterruptedException JavaDoc
108     {
109         SynchQueue sq = new SynchQueue();
110         Object JavaDoc o1 = new Object JavaDoc();
111         sq.enqueue( o1 );
112         assertTrue( "Queue should not be empty.", !sq.isEmpty() );
113         Object JavaDoc o2 = sq.dequeue();
114         assertEquals( "What gets put in, needs to equal what gets put out.",
115             o1, o2 );
116         assertTrue( "Queue should be empty after removing.", sq.isEmpty() );
117     }
118     
119     public void testAddSize1()
120     {
121         SynchQueue sq = new SynchQueue();
122         assertEquals( "Size must be 0 immediately after instantiation.",
123             0, sq.size() );
124         for (int i = 1; i <= 20; i++)
125         {
126             Object JavaDoc o = new Object JavaDoc();
127             sq.enqueue( o );
128             assertEquals( "Size must be "+i+" after enqueue call.",
129                 i, sq.size() );
130         }
131     }
132     
133     public void testRemoveSize1() throws InterruptedException JavaDoc
134     {
135         SynchQueue sq = new SynchQueue();
136         int count = 0;
137         for (int i = 1; i <= 20; i++)
138         {
139             Object JavaDoc o = new Object JavaDoc();
140             sq.enqueue( o );
141             count++;
142 //System.out.println("Added element: size now "+count);
143
}
144         while( count > 0 )
145         {
146             assertTrue( "Queue should not be empty.", !sq.isEmpty() );
147             assertEquals( "Queue size must be "+count+".", count, sq.size() );
148 //System.out.println("Removing element: size is now "+sq.size()+" (should be "+count+")" );
149
sq.dequeue();
150             count--;
151         }
152         assertTrue( "Queue should be empty after removing.", sq.isEmpty() );
153         assertEquals( "Size must be 0 after dequeue all.", 0, sq.size() );
154     }
155     
156     public void testUniqueRetrieval1() throws InterruptedException JavaDoc
157     {
158         SynchQueue sq = new SynchQueue();
159         int maxSize = 20;
160         Object JavaDoc objs[] = new Object JavaDoc[ maxSize ];
161         for (int i = 0; i < maxSize; i++)
162         {
163             objs[i] = "element "+i;
164             sq.enqueue( objs[i] );
165         }
166         // parse forwards through the list for checking the return values
167
// (its a queue, not a stack)
168
//System.err.println("Starting dequeue loop:");
169
for (int i = 0; i < maxSize; i++)
170         {
171 //System.err.println("dequeueing element index "+i);
172
assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+
173                 " elements left.", !sq.isEmpty() );
174             Object JavaDoc o = sq.dequeue();
175             assertNotNull( "Queue must never return null from dequeue.", o );
176             assertEquals( "Queue did not return the right object in the right "+
177                 "order (element "+i+", size = "+sq.size()+")", objs[i], o );
178         }
179         // yeah, we already have a test for this - but just to be careful.
180
assertTrue( "Queue must be empty.", sq.isEmpty() );
181     }
182     
183     public void testIdenticalRetrieval1() throws InterruptedException JavaDoc
184     {
185         SynchQueue sq = new SynchQueue();
186         int maxSize = 20;
187         Object JavaDoc obj = new Object JavaDoc();
188         for (int i = 0; i < maxSize; i++)
189         {
190             sq.enqueue( obj );
191         }
192         // parse forwards through the list for checking the return values
193
// (its a queue, not a stack)
194
//System.err.println("Starting dequeue loop:");
195
for (int i = 0; i < maxSize; i++)
196         {
197 //System.err.println("dequeueing element index "+i);
198
assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+
199                 " elements left.", !sq.isEmpty() );
200             Object JavaDoc o = sq.dequeue();
201             assertNotNull( "Queue must never return null from dequeue.", o );
202             assertEquals( "Queue did not return the right object in the right "+
203                 "order (element "+i+", size = "+sq.size()+")", obj, o );
204         }
205         // yeah, we already have a test for this - but just to be careful.
206
assertTrue( "Queue must be empty.", sq.isEmpty() );
207     }
208     
209
210     public void testPeek1() throws InterruptedException JavaDoc
211     {
212         SynchQueue sq = new SynchQueue();
213         assertNull( "Peek must return null on an empty queue", sq.peek() );
214         
215         int maxSize = 20;
216         Object JavaDoc objs[] = new Object JavaDoc[ maxSize ];
217         for (int i = 0; i < maxSize; i++)
218         {
219             objs[i] = "element "+i;
220             sq.enqueue( objs[i] );
221         }
222         // parse forwards through the list for checking the return values
223
// (its a queue, not a stack)
224
//System.err.println("Starting dequeue loop:");
225
for (int i = 0; i < maxSize; i++)
226         {
227 //System.err.println("dequeueing element index "+i);
228
assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+
229                 " elements left.", !sq.isEmpty() );
230             Object JavaDoc o1 = sq.peek();
231             assertNotNull( "Peek must not return null on a non-null queue",o1 );
232             Object JavaDoc o2 = sq.dequeue();
233             assertNotNull( "Queue must never return null from dequeue.", o2 );
234             assertEquals( "Peek did not return the right element in the right "+
235                 "order (element "+i+", size = "+sq.size()+")", o2, o1 );
236         }
237         // yeah, we already have a test for this - but just to be careful.
238
assertNull( "Peek must return null on an empty queue", sq.peek() );
239     }
240     
241     
242     public void testRemoveAll1() throws InterruptedException JavaDoc
243     {
244         SynchQueue sq = new SynchQueue();
245         assertNull( "Peek must return null on an empty queue", sq.peek() );
246         
247         int maxSize = 20;
248         Object JavaDoc objs[] = new Object JavaDoc[ maxSize ];
249         for (int i = 0; i < maxSize; i++)
250         {
251             objs[i] = "element "+i;
252             sq.enqueue( objs[i] );
253         }
254         assertEquals("Queue must have "+maxSize+" elements.", maxSize,
255             sq.size() );
256         sq.removeAll();
257         assertEquals("Queue must be empty.", 0, sq.size() );
258         assertTrue( "Queue must be empty.", sq.isEmpty() );
259         
260         // now verify that the queue still works correctly
261
testUniqueRetrieval1();
262     }
263     
264
265     
266     private class PutObjects extends TestRunnable
267     {
268         private SynchQueue m_sq;
269         private Object JavaDoc[] m_objs;
270         private long m_delay;
271         public PutObjects( SynchQueue sq, Object JavaDoc[] list, long delayMillis )
272         {
273             this.m_sq = sq;
274             this.m_objs = list;
275             this.m_delay = delayMillis;
276         }
277         
278         public void runTest() throws Throwable JavaDoc
279         {
280             try
281             {
282                 DOC.getLog().debug("Entering PutObjects.runTest()");
283                 for (int i = 0; i < this.m_objs.length; i++)
284                 {
285                     // for putting into the list, we delay before inserting
286
DOC.getLog().debug( "Enqueue delay "+this.m_delay+
287                         " millis." );
288                     delay( this.m_delay );
289                     DOC.getLog().debug( new Object JavaDoc[] {
290                         "Enqueue object '", this.m_objs[i], "'" } );
291                     this.m_sq.enqueue( this.m_objs[i] );
292                 }
293             }
294             finally
295             {
296                 DOC.getLog().debug("Leaving PutObjects.runTest()");
297             }
298         }
299     }
300     
301     
302
303     private class CheckSize extends TestMonitorRunnable
304     {
305         private SynchQueue m_sq;
306         private int m_maxSize;
307         public CheckSize( SynchQueue sq, int maxSize )
308         {
309             this.m_sq = sq;
310             this.m_maxSize = maxSize;
311         }
312         
313         public void runMonitor() throws Throwable JavaDoc
314         {
315             int size = this.m_sq.size();
316             assertTrue(
317                 "Invalid queue size "+size+
318                 ": must be within the set [0, "+this.m_maxSize+"].",
319                 (size >= 0) && (size <= this.m_maxSize) );
320         }
321     }
322     
323     
324     private class GetObjects extends TestRunnable
325     {
326         private SynchQueue m_sq;
327         private Object JavaDoc[] m_objs;
328         private long m_delay;
329         private long m_waitMillis;
330         private int m_waitNanos;
331         public GetObjects( SynchQueue sq, Object JavaDoc[] expectedlist,
332                 long delayMillis )
333         {
334             this( sq, expectedlist, delayMillis, -1, -1 );
335         }
336         public GetObjects( SynchQueue sq, Object JavaDoc[] expectedlist,
337                 long delayMillis, long waitTime, int nanos )
338         {
339             this.m_sq = sq;
340             this.m_objs = expectedlist;
341             this.m_delay = delayMillis;
342             this.m_waitMillis = waitTime;
343             this.m_waitNanos = nanos;
344         }
345         
346         public void runTest() throws Throwable JavaDoc
347         {
348             DOC.getLog().debug("Entering GetObjects.runTest()");
349             for (int i = 0; i < this.m_objs.length; i++)
350             {
351                 DOC.getLog().debug("Dequeue delay "+this.m_delay+
352                     " millis.");
353                 delay( this.m_delay );
354                 Object JavaDoc o = dequeue();
355                 assertEquals( "Retrieved element "+i+" doesn't match",
356                     this.m_objs[i], o );
357             }
358             DOC.getLog().debug("Leaving GetObjects.runTest()");
359         }
360         
361         protected Object JavaDoc dequeue() throws Throwable JavaDoc
362         {
363             long startTime = System.currentTimeMillis();
364             Object JavaDoc ret;
365             if (this.m_waitMillis > 0)
366             {
367                 if (this.m_waitNanos > 0)
368                 {
369                     DOC.getLog().debug("dequeue w/ nano delay");
370                     ret = this.m_sq.dequeue(
371                         this.m_waitMillis, this.m_waitNanos );
372                 }
373                 else
374                 {
375                     DOC.getLog().debug("dequeue w/ millis dely ["+
376                         this.m_waitMillis+"]");
377                     ret = this.m_sq.dequeue( this.m_waitMillis );
378                 }
379             }
380             else
381             {
382                 DOC.getLog().debug("dequeue w/ no time-out");
383                 ret = this.m_sq.dequeue();
384             }
385             long endTime = System.currentTimeMillis();
386             DOC.getLog().debug( new Object JavaDoc[] {
387                 "returning dequeued object '", ret, "' after "+
388                 (endTime - startTime)+" milliseconds." } );
389             return ret;
390         }
391     }
392     
393     
394     
395     
396     public void testSimpleThreaded()
397             throws Throwable JavaDoc
398     {
399         DOC.getLog().info( "Entering testSimpleThreaded" );
400         int numElements = 20;
401         SynchQueue sq = new SynchQueue();
402         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
403         for (int i = 0; i < numElements; i++)
404         {
405             list[i] = "element "+i;
406         }
407         
408         // MCA: 26-Oct-2003: changed GetObjects delay to 20 ms due to
409
// coverage number delays and file I/O on slower machines.
410
TestRunnable tcs[] = { new PutObjects( sq, list, 10 ),
411             new GetObjects( sq, list, 20 ) };
412         // allow for a maximum of 60 seconds for this test to run
413
DOC.getLog().debug("Starting SimpleThreaded()");
414         runTestRunnables( tcs, new CheckSize( sq, numElements ), 1000 * 60 );
415         DOC.getLog().debug("Finished SimpleThreaded()");
416     }
417     
418     public void testSyncapatedThreaded()
419             throws Throwable JavaDoc
420     {
421         DOC.getLog().info( "Entering testSyncapatedThreaded" );
422         int numElements = 20;
423         SynchQueue sq = new SynchQueue();
424         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
425         for (int i = 0; i < numElements; i++)
426         {
427             list[i] = "element "+i;
428         }
429         TestRunnable tcs[] = { new PutObjects( sq, list, 1000 ),
430             new GetObjects( sq, list, 10 ) };
431         DOC.getLog().debug("Starting SyncapatedThreaded()");
432         runTestRunnables( tcs, new CheckSize( sq, numElements ), 1000 * 60 );
433         DOC.getLog().debug("Finished SyncapatedThreaded()");
434     }
435     
436     public void testDelayMillisThreaded()
437             throws Throwable JavaDoc
438     {
439         DOC.getLog().info( "Entering testDelayMillisThreaded" );
440         int numElements = 1;
441         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
442         SynchQueue sq = new SynchQueue();
443         // don't wait - detect the null immediately
444
TestRunnable tcs[] = { new GetObjects( sq, list, 0, 10, -1 ) };
445         DOC.getLog().debug("Starting DelayMillisThreaded()");
446         runTestRunnables( tcs, new CheckSize( sq, numElements ), 2000 );
447         DOC.getLog().debug("Finished DelayMillisThreaded()");
448     }
449     
450     public void testDelayNanosThreaded()
451             throws Throwable JavaDoc
452     {
453         DOC.getLog().info( "Entering testDelayNanosThreaded" );
454         int numElements = 1;
455         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
456         SynchQueue sq = new SynchQueue();
457         // wait for 10 millis, 10 nanos, for null.
458
CheckSize cs = new CheckSize( sq, numElements );
459         TestRunnable tcs[] = { new GetObjects( sq, list, 0, 10, 10 ) };
460         DOC.getLog().debug("Starting DelayNanosThreaded()");
461         runTestRunnables( tcs, new CheckSize( sq, numElements ), 2000 );
462         DOC.getLog().debug("Finished DelayNanosThreaded()");
463     }
464     
465     public void testPutDelayMillisThreaded()
466             throws Throwable JavaDoc
467     {
468         DOC.getLog().info( "Entering testPutDelayMillisThreaded" );
469         int numElements = 20;
470         SynchQueue sq = new SynchQueue();
471         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
472         for (int i = 0; i < numElements; i++)
473         {
474             list[i] = "element "+i;
475         }
476         TestRunnable tcs[] = { new PutObjects( sq, list, 10 ),
477             new GetObjects( sq, list, 0, 500, -1 ) };
478         DOC.getLog().debug("Starting PutDelayMillisThreaded()");
479         runTestRunnables( tcs, new CheckSize( sq, numElements ), 1000 * 60 );
480         DOC.getLog().debug("Finished PutDelayMillisThreaded()");
481     }
482     
483     public void testPutDelayMillisThreaded2()
484             throws Throwable JavaDoc
485     {
486         DOC.getLog().info( "Entering testPutDelayMillisThreaded2" );
487         int numElements = 5;
488         SynchQueue sq = new SynchQueue();
489         Object JavaDoc list[] = new Object JavaDoc[ numElements ];
490         Object JavaDoc nulllist[] = new Object JavaDoc[ numElements ];
491         for (int i = 0; i < numElements; i++)
492         {
493             list[i] = "element "+i;
494         }
495         // timing issues cause these numbers not to match up perfectly.
496
TestRunnable tcs[] = { new PutObjects( sq, list, numElements * 500 ),
497             new GetObjects( sq, nulllist, 0, 100, -1 ) };
498         DOC.getLog().debug("Starting PutDelayMillisThreaded2()");
499         runTestRunnables( tcs, new CheckSize( sq, numElements ), 1000 * 60 );
500         DOC.getLog().debug("Finished PutDelayMillisThreaded2()");
501     }
502     
503     
504     protected void runTestRunnables( TestRunnable tr[], TestRunnable tm,
505             long maxtime )
506             throws Throwable JavaDoc
507     {
508         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( tr,
509             new TestRunnable[] { tm } );
510         mttr.runTestRunnables( maxtime );
511     }
512 }
513
514
Popular Tags