@Test public void testPooledIteratorFullIteratorLoop() { // A) for-each loop interrupted // must accommodate even the smallest primitive type // so that the iteration do not break before it should... final int TEST_SIZE = 126; final long TEST_ROUNDS = 5000; final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE); final long checksum = testContainer.forEach( new KTypeProcedure<KType>() { long count; @Override public void apply(final KType value) { this.count += castType(value); } }) .count; long testValue = 0; final int startingPoolSize = getEntryPoolSize(testContainer); for (int round = 0; round < TEST_ROUNDS; round++) { // Classical iterator loop, with manually allocated Iterator final int initialPoolSize = getEntryPoolSize(testContainer); final AbstractIterator<KTypeCursor<KType>> loopIterator = (AbstractIterator<KTypeCursor<KType>>) testContainer.iterator(); Assert.assertEquals(initialPoolSize - 1, getEntryPoolSize(testContainer)); testValue = 0; while (loopIterator.hasNext()) { testValue += castType(loopIterator.next().value); } // end IteratorLoop // iterator is returned automatically to its pool, by normal iteration termination Assert.assertEquals(initialPoolSize, getEntryPoolSize(testContainer)); // checksum Assert.assertEquals(checksum, testValue); } // end for rounds // pool initial size is untouched anyway Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer)); }
@Test public void testPooledIteratorBrokenIteratorLoop() { // A) for-each loop interrupted // must accommodate even the smallest primitive type // so that the iteration do not break before it should... final int TEST_SIZE = 126; final long TEST_ROUNDS = 5000; final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE); final int startingPoolSize = getEntryPoolSize(testContainer); int count = 0; for (int round = 0; round < TEST_ROUNDS; round++) { // Classical iterator loop, with manually allocated Iterator final long initialPoolSize = getEntryPoolSize(testContainer); final AbstractIterator<KTypeCursor<KType>> loopIterator = (AbstractIterator<KTypeCursor<KType>>) testContainer.iterator(); Assert.assertEquals(initialPoolSize - 1, getEntryPoolSize(testContainer)); count = 0; int guard = 0; while (loopIterator.hasNext()) { guard += castType(loopIterator.next().value); // brutally interrupt in the middle if (count > TEST_SIZE / 2) { break; } count++; } // end IteratorLoop // iterator is NOT returned to its pool, due to the break. Assert.assertEquals(initialPoolSize - 1, getEntryPoolSize(testContainer)); // manual return to the pool loopIterator.release(); // now the pool is restored Assert.assertEquals(initialPoolSize, getEntryPoolSize(testContainer)); } // end for rounds // pool initial size is untouched anyway Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer)); }
@Test public void testPooledIteratorExceptionIteratorLoop() { // must accommodate even the smallest primitive type // so that the iteration do not break before it should... final int TEST_SIZE = 126; final long TEST_ROUNDS = 5000; final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE); final long checksum = testContainer.forEach( new KTypeProcedure<KType>() { long count; @Override public void apply(final KType value) { this.count += castType(value); } }) .count; final int startingPoolSize = getEntryPoolSize(testContainer); int count = 0; AbstractIterator<KTypeCursor<KType>> loopIterator = null; for (int round = 0; round < TEST_ROUNDS; round++) { try { loopIterator = (AbstractIterator<KTypeCursor<KType>>) testContainer.iterator(); Assert.assertEquals(startingPoolSize - 1, getEntryPoolSize(testContainer)); int guard = 0; count = 0; while (loopIterator.hasNext()) { guard += castType(loopIterator.next().value); // brutally interrupt in the middle some of the loops, but not all if (round > TEST_ROUNDS / 2 && count > TEST_SIZE / 2) { throw new Exception("Oups some problem in the loop occured"); } count++; } // end while // iterator is returned to its pool in case of normal loop termination Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer)); Assert.assertEquals(checksum, guard); } catch (final Exception e) { // iterator is NOT returned to its pool because of the exception Assert.assertEquals(startingPoolSize - 1, getEntryPoolSize(testContainer)); // manual return to the pool then loopIterator.release(); // now the pool is restored Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer)); } } // end for rounds // pool initial size is untouched anyway Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer)); }