net.sf.farrago.test
Class FarragoObjectCacheTest

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by net.sf.farrago.test.FarragoObjectCacheTest
All Implemented Interfaces:
Test

public class FarragoObjectCacheTest
extends TestCase

FarragoObjectCacheTest is a unit test for FarragoObjectCache.

Premise: inner class RentalCarAgency maintains a fleet of rental cars and assigns them to customers as requested. The agency is lucky enough to be able to manufacture new cars on demand! (It uses a private factory to do this.) However, it has a fixed number of tires it can outfit them with (due to a terrible rubber tree blight). So, it sometimes has to destroy old cars to salvage their tires. (For real usage patterns, read "object memory usage" for "tires".)

TODO:

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/test/FarragoObjectCacheTest.java#7 $
Author:
John Sichi

Nested Class Summary
private  class FarragoObjectCacheTest.CustomerThread
          CustomerThread exemplifies a thread which uses objects from the cache.
private  class FarragoObjectCacheTest.LemonException
           
private  class FarragoObjectCacheTest.RentalCar
          RentalCar exemplifies a reusable object.
private  class FarragoObjectCacheTest.RentalCarAgency
          RentalCarAgency exemplifies a cache of reusable objects.
private  class FarragoObjectCacheTest.RentalCarAgreement
          RentalCarAgreement encapsulates the assignment of a car to a customer for a duration.
 
Field Summary
(package private)  FarragoObjectCacheTest.RentalCarAgency agency
           
(package private) static int MAX_MILEAGE
          Mileage limit after which a car should be discarded.
(package private) static int MAX_TIRES
          Default cache size limit.
(package private)  AtomicInteger nCarsCreated
           
(package private)  AtomicInteger nCarsDestroyed
           
 
Constructor Summary
FarragoObjectCacheTest(String testName)
          Creates a new FarragoObjectCacheTest object.
 
Method Summary
private  void runMultipleThreads(boolean exclusive)
           
private  void runOneThreadSequential(String description, int milesToDrive, boolean expectNew)
           
 void tearDown()
           
 void testMultipleThreadsExclusive()
          Tests a multi-threaded scenario with objects pinned exclusively.
 void testMultipleThreadsShared()
          Tests a multi-threaded scenario with objects pinned as shared.
 void testOneThreadFailedInitialization()
          Tests a scenario where an exception is thrown during initialization.
 void testOneThreadOverlappingExclusive()
          Tests a scenario where two cars are rented at the same time.
 void testOneThreadSequential()
          Tests a scenario where a single car is rented, returned, and then rented and returned again.
 void testOneThreadSequentialNonReusable()
          Tests a scenario where a car is rented, smoked in, and then returned, so that a subsequent rental request forces creation of a new car (discarding the old one).
 void testOneThreadSequentialStale()
          Tests a scenario where a car is rented, driven a long way, and then returned, so that a subsequent rental request forces creation of a new car (discarding the old one).
 void testOneThreadShared()
          Tests a scenario where the same car is rented by two customers at the same time.
 void testOneThreadVictimization()
          Tests a scenario where new cars are constantly requested and then returned, causing old cars to be recycled for their tires.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_TIRES

static final int MAX_TIRES
Default cache size limit.

See Also:
Constant Field Values

MAX_MILEAGE

static final int MAX_MILEAGE
Mileage limit after which a car should be discarded.

See Also:
Constant Field Values

agency

FarragoObjectCacheTest.RentalCarAgency agency

nCarsCreated

AtomicInteger nCarsCreated

nCarsDestroyed

AtomicInteger nCarsDestroyed
Constructor Detail

FarragoObjectCacheTest

public FarragoObjectCacheTest(String testName)
                       throws Exception
Creates a new FarragoObjectCacheTest object.

Throws:
Exception
Method Detail

tearDown

public void tearDown()
Overrides:
tearDown in class TestCase

testOneThreadOverlappingExclusive

public void testOneThreadOverlappingExclusive()
Tests a scenario where two cars are rented at the same time. A single thread acts as customer for both cars.


testOneThreadSequential

public void testOneThreadSequential()
Tests a scenario where a single car is rented, returned, and then rented and returned again.


testOneThreadSequentialStale

public void testOneThreadSequentialStale()
Tests a scenario where a car is rented, driven a long way, and then returned, so that a subsequent rental request forces creation of a new car (discarding the old one).


testOneThreadSequentialNonReusable

public void testOneThreadSequentialNonReusable()
Tests a scenario where a car is rented, smoked in, and then returned, so that a subsequent rental request forces creation of a new car (discarding the old one).


runOneThreadSequential

private void runOneThreadSequential(String description,
                                    int milesToDrive,
                                    boolean expectNew)

testOneThreadShared

public void testOneThreadShared()
Tests a scenario where the same car is rented by two customers at the same time. A single thread acts as both customers.


testOneThreadVictimization

public void testOneThreadVictimization()
Tests a scenario where new cars are constantly requested and then returned, causing old cars to be recycled for their tires.


testOneThreadFailedInitialization

public void testOneThreadFailedInitialization()
Tests a scenario where an exception is thrown during initialization.


testMultipleThreadsExclusive

public void testMultipleThreadsExclusive()
Tests a multi-threaded scenario with objects pinned exclusively.


testMultipleThreadsShared

public void testMultipleThreadsShared()
Tests a multi-threaded scenario with objects pinned as shared.


runMultipleThreads

private void runMultipleThreads(boolean exclusive)