Ten Puts And One Take

This is part of JavaUnitTestChallengeSolved. -- DonWells


Our next test to add is one where we run ten separate put threads and service them with a single take thread. Our hope is to catch the puts overwhelming the single take. To do this, we first need to change TakeThread so that we can specify that we want to get 50 inputs instead of the 5 we have been using all along. Let's just add an instance variable to tell us how many takes to do and make 5 the default. We can also add a ConstructorMethod? which allows use to specify how many takes.

    int numberOfTakes;

TakeThread(BoundedBuffer aBuffer){ numberOfTakes = 5; buffer = aBuffer;}

TakeThread(BoundedBuffer aBuffer, int aNumberOfTakes){ this(aBuffer); numberOfTakes = aNumberOfTakes;}

Now we can create our test:

 public class TenAndOne? extends Test {
    public BoundedBuffer buffer;
    public PutThread putThreads [];
    public TakeThread takeThread;

public void setUp(){ buffer = new BoundedBuffer(); putThreads = new PutThread [10]; takeThread = new TakeThread(buffer, 50); for (int each = 0; each < 10; each++) { putThreads[each] = new PutThread(buffer);};}

public void tearDown(){ TakeThread.dumpOutput();}

public void runTest (){ TakeThread.resetOutput(); takeThread.start(); for (int each = 0; each < 10; each++) { putThreads[each].start();}; waitForPutToFinish(); sleepHalfSecond(); should(TakeThread.outputLength() == 50, "output was too short"); should(TakeThread.doesOuputHaveTenOf("a"), "should get ten a"); should(TakeThread.doesOuputHaveTenOf("b"), "should get ten b"); should(TakeThread.doesOuputHaveTenOf("c"), "should get ten c"); should(TakeThread.doesOuputHaveTenOf("d"), "should get ten d"); should(TakeThread.doesOuputHaveTenOf("e"), "should get ten e");}

public void waitForPutToFinish(){ try { for (int each = 0; each < 10; each++) { putThreads[each].join(4000);}} catch (InterruptedException exception) {};}

}

This test runs just fine.


EditText of this page (last edited September 22, 2003) or FindPage with title or text search