@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { iterator = new DynamicIterator<Integer>(100); }
@Override protected T computeNext() { try { available.acquire(); synchronized (queue) { T element = queue.poll(); if (element == null) { Preconditions.checkState(finished, "Expected iterator to be finished"); endOfData(); } return element; } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } }
public Void call() throws InterruptedException { for (final int value : values) { iterator.add(value); } return null; } };
@Test (groups = "fast") public void testMultipleWriters() throws ExecutionException, InterruptedException, TimeoutException { ExecutorService executor = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setDaemon(true).build()); Future<Set<Integer>> consumer = executor.submit( new Callable<Set<Integer>>() { public Set<Integer> call() throws Exception { return Sets.newHashSet(iterator); } } ); //noinspection unchecked executor.invokeAll(asList( producer(iterator, 0, 1, 2, 3, 4), producer(iterator, 5, 6, 7, 8, 9) ), 1, TimeUnit.SECONDS); // failsafe in case there's a deadlock iterator.finish(); Set<Integer> result = consumer.get(1, TimeUnit.SECONDS);// failsafe in case there's a deadlock assertEquals(result, Sets.newHashSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); }
public Void call() throws InterruptedException { for (final int value : values) { iterator.add(value); } return null; } };
@Test (groups = "fast") public void testMultipleWriters() throws ExecutionException, InterruptedException, TimeoutException { ExecutorService executor = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setDaemon(true).build()); Future<Set<Integer>> consumer = executor.submit( new Callable<Set<Integer>>() { public Set<Integer> call() throws Exception { return Sets.newHashSet(iterator); } } ); //noinspection unchecked executor.invokeAll(asList( producer(iterator, 0, 1, 2, 3, 4), producer(iterator, 5, 6, 7, 8, 9) ), 1, TimeUnit.SECONDS); // failsafe in case there's a deadlock iterator.finish(); Set<Integer> result = consumer.get(1, TimeUnit.SECONDS);// failsafe in case there's a deadlock assertEquals(result, Sets.newHashSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); }
@Test(groups = "fast") public void testInterleaved() throws InterruptedException { List<Integer> expected = asList(1, 2, 3, 4, 5); for (int value : expected) { iterator.add(value); assertTrue(iterator.hasNext()); assertEquals(iterator.next().intValue(), value); } iterator.finish(); assertFalse(iterator.hasNext()); }
@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { iterator = new DynamicIterator<Integer>(100); }
@Test(groups = "fast") public void testAddMultiple() throws InterruptedException { List<Integer> expected = asList(1, 2, 3, 4, 5); for (Integer value : expected) { iterator.add(value); } iterator.finish(); for (int value : expected) { assertTrue(iterator.hasNext()); assertEquals(iterator.next().intValue(), value); } assertFalse(iterator.hasNext()); }
@Test(groups = "fast") public void testAddMultiple() throws InterruptedException { List<Integer> expected = asList(1, 2, 3, 4, 5); for (Integer value : expected) { iterator.add(value); } iterator.finish(); for (int value : expected) { assertTrue(iterator.hasNext()); assertEquals(iterator.next().intValue(), value); } assertFalse(iterator.hasNext()); }
@Test(groups = "fast") public void testInterleaved() throws InterruptedException { List<Integer> expected = asList(1, 2, 3, 4, 5); for (int value : expected) { iterator.add(value); assertTrue(iterator.hasNext()); assertEquals(iterator.next().intValue(), value); } iterator.finish(); assertFalse(iterator.hasNext()); }