RoundRobinSet extends AbstractCollection<E> implements Set<E> { @Override public boolean add(final E o) { return realList.add(o); }  @Override boolean add(final E o); @Override Iterator<E> iterator(); @Override int size(); @Override String toString();  }
@Test void test() { final RoundRobinSet<RoundRobinSetTestObject> rrList = new RoundRobinSet<>(); rrList.add(new RoundRobinSetTestObject(1)); rrList.add(new RoundRobinSetTestObject(2)); rrList.add(new RoundRobinSetTestObject(N3)); rrList.add(new RoundRobinSetTestObject(N4)); assertThat(getString(rrList)).isEqualTo("123"); assertThat(getString(rrList)).isEqualTo("234"); assertThat(getString(rrList)).isEqualTo("341"); assertThat(getString(rrList)).isEqualTo("412"); assertThat(getString(rrList)).isEqualTo("123"); }
@Test void testSync() throws InterruptedException { final RoundRobinSet<RoundRobinSetTestObject> rrList = new RoundRobinSet<>(); rrList.add(new RoundRobinSetTestObject(1)); rrList.add(new RoundRobinSetTestObject(2)); rrList.add(new RoundRobinSetTestObject(N3)); rrList.add(new RoundRobinSetTestObject(N4)); final MutableLong failureCount = new MutableLong(0); final ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { executorService.execute(() -> { try { for (int j = 0; j < 100; j++) { Thread.sleep(10); try { getString(rrList); } catch (final RuntimeException e) { System.err.println(e.getMessage()); failureCount.increment(); } } } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } }); } executorService.shutdown(); executorService.awaitTermination(2, TimeUnit.MINUTES); assertThat(failureCount.longValue()).isEqualTo(0); }