< prev index next >

core/tests/org.openjdk.jmc.common.test/src/test/java/org/openjdk/jmc/common/util/BoundedListTest.java

Print this page




 135                 }
 136                 assertEquals(4, val);
 137         }
 138 
 139         @Test
 140         public void testWrappingIterator() {
 141                 BoundedList<Integer> bl = new BoundedList<>(10);
 142                 for (int i = 1; i <= 20; i++) {
 143                         bl.add(i);
 144                 }
 145 
 146                 int val = 11;
 147                 for (Integer iterVal : bl) {
 148                         assertEquals(val++, iterVal.intValue());
 149                 }
 150         }
 151 
 152         @Test
 153         public void testEmptyIterator() {
 154                 BoundedList<Integer> bl = new BoundedList<>(10);
 155                 assertFalse("Empty list should have no elements!", bl.iterator().hasNext()); //$NON-NLS-1$
 156                 try {
 157                         bl.iterator().next();
 158                         fail("next should have generated an exception!"); //$NON-NLS-1$
 159                 } catch (NoSuchElementException el) {
 160                         // Fall through...
 161                 }
 162         }
 163 
 164         @Test
 165         public void testMultipleIterators() {
 166                 // Shows that we can leak memory if we add faster than we can consume...
 167                 BoundedList<Integer> bl = new BoundedList<>(10);
 168                 for (int i = 1; i <= 10; i++) {
 169                         bl.add(i);
 170                 }
 171                 Iterator<Integer> iter1 = bl.iterator();
 172                 for (int i = 11; i <= 20; i++) {
 173                         bl.add(i);
 174                 }
 175                 Iterator<Integer> iter2 = bl.iterator();
 176 
 177                 int val1 = 1;
 178                 while (iter1.hasNext()) {
 179                         assertEquals(val1++, iter1.next().intValue());
 180                 }
 181 
 182                 int val2 = 11;
 183                 while (iter2.hasNext()) {
 184                         assertEquals(val2++, iter2.next().intValue());
 185                 }
 186                 // Iterated through all values, even though we've wrapped.
 187                 assertEquals(11, val1);
 188                 assertEquals(21, val2);
 189         }
 190 
 191         @Category(value = SlowTests.class)
 192         @Test
 193         public void testMultiThreadedConsumption() throws InterruptedException {
 194                 final BoundedList<Long> bl = new BoundedList<>(20);
 195                 ProducerThread t = new ProducerThread(bl);
 196                 ValidationThread[] validators = new ValidationThread[10];
 197                 new Thread(t, "Producer").start(); //$NON-NLS-1$
 198                 for (int i = 0; i < validators.length; i++) {
 199                         validators[i] = new ValidationThread(bl);
 200                         new Thread(validators[i], "Validator " + i).start(); //$NON-NLS-1$
 201                 }
 202                 Thread.sleep(30000);
 203                 for (ValidationThread validator : validators) {
 204                         validator.stop();
 205                 }
 206                 t.stop();
 207                 long maxNo = 0;
 208                 for (ValidationThread validator : validators) {
 209                         assertEquals("Failed count validation!", -1, validator.countError); //$NON-NLS-1$
 210                         assertEquals("Failed sequence validation!", -1, validator.sequenceError); //$NON-NLS-1$
 211                         maxNo = Math.max(maxNo, validator.maxNum);
 212                 }
 213                 System.out.println("Allocated up to " + t.counter); //$NON-NLS-1$
 214                 System.out.println("Max no was " + maxNo); //$NON-NLS-1$
 215         }
 216 
 217         // FIXME: This test has been commented out for a long time. Check if it is still relevant and either remove or reintroduce it.
 218 //      @Category(value = SlowTests.class)
 219 //      @Test
 220 //      public void testNoLeak() {
 221 //              BoundedList<Long> bl = new BoundedList<Long>(10);
 222 //              // Adding a few billion numbers, just to show that we do not leak under normal circumstances...
 223 //              for (long i = 1; i <= Integer.MAX_VALUE; i++) {
 224 //                      if (i % (Integer.MAX_VALUE / 20) == 0) {
 225 //                              System.out.println(String.format("Passed %.0f%%", (i * 100f) / Integer.MAX_VALUE)); //$NON-NLS-1$
 226 //                      }
 227 //                      bl.add(i);
 228 //              }
 229 //              // On a 32-bit platform we should either crash or pass...
 230 //      }
 231 
 232 }


 135                 }
 136                 assertEquals(4, val);
 137         }
 138 
 139         @Test
 140         public void testWrappingIterator() {
 141                 BoundedList<Integer> bl = new BoundedList<>(10);
 142                 for (int i = 1; i <= 20; i++) {
 143                         bl.add(i);
 144                 }
 145 
 146                 int val = 11;
 147                 for (Integer iterVal : bl) {
 148                         assertEquals(val++, iterVal.intValue());
 149                 }
 150         }
 151 
 152         @Test
 153         public void testEmptyIterator() {
 154                 BoundedList<Integer> bl = new BoundedList<>(10);
 155                 assertFalse("Empty list should have no elements!", bl.iterator().hasNext());
 156                 try {
 157                         bl.iterator().next();
 158                         fail("next should have generated an exception!");
 159                 } catch (NoSuchElementException el) {
 160                         // Fall through...
 161                 }
 162         }
 163 
 164         @Test
 165         public void testMultipleIterators() {
 166                 // Shows that we can leak memory if we add faster than we can consume...
 167                 BoundedList<Integer> bl = new BoundedList<>(10);
 168                 for (int i = 1; i <= 10; i++) {
 169                         bl.add(i);
 170                 }
 171                 Iterator<Integer> iter1 = bl.iterator();
 172                 for (int i = 11; i <= 20; i++) {
 173                         bl.add(i);
 174                 }
 175                 Iterator<Integer> iter2 = bl.iterator();
 176 
 177                 int val1 = 1;
 178                 while (iter1.hasNext()) {
 179                         assertEquals(val1++, iter1.next().intValue());
 180                 }
 181 
 182                 int val2 = 11;
 183                 while (iter2.hasNext()) {
 184                         assertEquals(val2++, iter2.next().intValue());
 185                 }
 186                 // Iterated through all values, even though we've wrapped.
 187                 assertEquals(11, val1);
 188                 assertEquals(21, val2);
 189         }
 190 
 191         @Category(value = SlowTests.class)
 192         @Test
 193         public void testMultiThreadedConsumption() throws InterruptedException {
 194                 final BoundedList<Long> bl = new BoundedList<>(20);
 195                 ProducerThread t = new ProducerThread(bl);
 196                 ValidationThread[] validators = new ValidationThread[10];
 197                 new Thread(t, "Producer").start();
 198                 for (int i = 0; i < validators.length; i++) {
 199                         validators[i] = new ValidationThread(bl);
 200                         new Thread(validators[i], "Validator " + i).start();
 201                 }
 202                 Thread.sleep(30000);
 203                 for (ValidationThread validator : validators) {
 204                         validator.stop();
 205                 }
 206                 t.stop();
 207                 long maxNo = 0;
 208                 for (ValidationThread validator : validators) {
 209                         assertEquals("Failed count validation!", -1, validator.countError);
 210                         assertEquals("Failed sequence validation!", -1, validator.sequenceError);
 211                         maxNo = Math.max(maxNo, validator.maxNum);
 212                 }
 213                 System.out.println("Allocated up to " + t.counter);
 214                 System.out.println("Max no was " + maxNo);
 215         }
 216 
 217         // FIXME: This test has been commented out for a long time. Check if it is still relevant and either remove or reintroduce it.
 218 //      @Category(value = SlowTests.class)
 219 //      @Test
 220 //      public void testNoLeak() {
 221 //              BoundedList<Long> bl = new BoundedList<Long>(10);
 222 //              // Adding a few billion numbers, just to show that we do not leak under normal circumstances...
 223 //              for (long i = 1; i <= Integer.MAX_VALUE; i++) {
 224 //                      if (i % (Integer.MAX_VALUE / 20) == 0) {
 225 //                              System.out.println(String.format("Passed %.0f%%", (i * 100f) / Integer.MAX_VALUE));
 226 //                      }
 227 //                      bl.add(i);
 228 //              }
 229 //              // On a 32-bit platform we should either crash or pass...
 230 //      }
 231 
 232 }
< prev index next >