1 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 52 public class SynchQueueUTest extends TestCase 53 { 54 private static final Class THIS_CLASS = SynchQueueUTest.class; 55 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 56 57 public SynchQueueUTest( String 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 [] args ) 70 { 71 String [] name = { THIS_CLASS.getName() }; 72 73 76 junit.textui.TestRunner.main( name ); 77 } 78 79 protected void setUp() throws Exception 80 { 81 super.setUp(); 82 83 } 85 86 87 protected void tearDown() throws Exception 88 { 89 91 92 super.tearDown(); 93 } 94 95 96 public void testInstantiate1() 97 { 98 Object 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 108 { 109 SynchQueue sq = new SynchQueue(); 110 Object o1 = new Object (); 111 sq.enqueue( o1 ); 112 assertTrue( "Queue should not be empty.", !sq.isEmpty() ); 113 Object 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 o = new Object (); 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 134 { 135 SynchQueue sq = new SynchQueue(); 136 int count = 0; 137 for (int i = 1; i <= 20; i++) 138 { 139 Object o = new Object (); 140 sq.enqueue( o ); 141 count++; 142 } 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 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 157 { 158 SynchQueue sq = new SynchQueue(); 159 int maxSize = 20; 160 Object objs[] = new Object [ maxSize ]; 161 for (int i = 0; i < maxSize; i++) 162 { 163 objs[i] = "element "+i; 164 sq.enqueue( objs[i] ); 165 } 166 for (int i = 0; i < maxSize; i++) 170 { 171 assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+ 173 " elements left.", !sq.isEmpty() ); 174 Object 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 assertTrue( "Queue must be empty.", sq.isEmpty() ); 181 } 182 183 public void testIdenticalRetrieval1() throws InterruptedException 184 { 185 SynchQueue sq = new SynchQueue(); 186 int maxSize = 20; 187 Object obj = new Object (); 188 for (int i = 0; i < maxSize; i++) 189 { 190 sq.enqueue( obj ); 191 } 192 for (int i = 0; i < maxSize; i++) 196 { 197 assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+ 199 " elements left.", !sq.isEmpty() ); 200 Object 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 assertTrue( "Queue must be empty.", sq.isEmpty() ); 207 } 208 209 210 public void testPeek1() throws InterruptedException 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 objs[] = new Object [ maxSize ]; 217 for (int i = 0; i < maxSize; i++) 218 { 219 objs[i] = "element "+i; 220 sq.enqueue( objs[i] ); 221 } 222 for (int i = 0; i < maxSize; i++) 226 { 227 assertTrue( "Queue cannot be empty at this point: should have "+(i+1)+ 229 " elements left.", !sq.isEmpty() ); 230 Object o1 = sq.peek(); 231 assertNotNull( "Peek must not return null on a non-null queue",o1 ); 232 Object 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 assertNull( "Peek must return null on an empty queue", sq.peek() ); 239 } 240 241 242 public void testRemoveAll1() throws InterruptedException 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 objs[] = new Object [ 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 testUniqueRetrieval1(); 262 } 263 264 265 266 private class PutObjects extends TestRunnable 267 { 268 private SynchQueue m_sq; 269 private Object [] m_objs; 270 private long m_delay; 271 public PutObjects( SynchQueue sq, Object [] 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 279 { 280 try 281 { 282 DOC.getLog().debug("Entering PutObjects.runTest()"); 283 for (int i = 0; i < this.m_objs.length; i++) 284 { 285 DOC.getLog().debug( "Enqueue delay "+this.m_delay+ 287 " millis." ); 288 delay( this.m_delay ); 289 DOC.getLog().debug( new Object [] { 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 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 [] m_objs; 328 private long m_delay; 329 private long m_waitMillis; 330 private int m_waitNanos; 331 public GetObjects( SynchQueue sq, Object [] expectedlist, 332 long delayMillis ) 333 { 334 this( sq, expectedlist, delayMillis, -1, -1 ); 335 } 336 public GetObjects( SynchQueue sq, Object [] 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 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 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 dequeue() throws Throwable 362 { 363 long startTime = System.currentTimeMillis(); 364 Object 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 [] { 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 398 { 399 DOC.getLog().info( "Entering testSimpleThreaded" ); 400 int numElements = 20; 401 SynchQueue sq = new SynchQueue(); 402 Object list[] = new Object [ numElements ]; 403 for (int i = 0; i < numElements; i++) 404 { 405 list[i] = "element "+i; 406 } 407 408 TestRunnable tcs[] = { new PutObjects( sq, list, 10 ), 411 new GetObjects( sq, list, 20 ) }; 412 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 420 { 421 DOC.getLog().info( "Entering testSyncapatedThreaded" ); 422 int numElements = 20; 423 SynchQueue sq = new SynchQueue(); 424 Object list[] = new Object [ 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 438 { 439 DOC.getLog().info( "Entering testDelayMillisThreaded" ); 440 int numElements = 1; 441 Object list[] = new Object [ numElements ]; 442 SynchQueue sq = new SynchQueue(); 443 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 452 { 453 DOC.getLog().info( "Entering testDelayNanosThreaded" ); 454 int numElements = 1; 455 Object list[] = new Object [ numElements ]; 456 SynchQueue sq = new SynchQueue(); 457 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 467 { 468 DOC.getLog().info( "Entering testPutDelayMillisThreaded" ); 469 int numElements = 20; 470 SynchQueue sq = new SynchQueue(); 471 Object list[] = new Object [ 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 485 { 486 DOC.getLog().info( "Entering testPutDelayMillisThreaded2" ); 487 int numElements = 5; 488 SynchQueue sq = new SynchQueue(); 489 Object list[] = new Object [ numElements ]; 490 Object nulllist[] = new Object [ numElements ]; 491 for (int i = 0; i < numElements; i++) 492 { 493 list[i] = "element "+i; 494 } 495 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 507 { 508 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( tr, 509 new TestRunnable[] { tm } ); 510 mttr.runTestRunnables( maxtime ); 511 } 512 } 513 514 | Popular Tags |