@Test public void getOneToFive() throws Exception {
Iterator<Integer> i = new CustomGenerator(1, 10).call(4, null, null);
List<Integer> l = new ArrayList<>();
while (i.hasNext()) {
l.add(i.next());
}
assertIterableEquals(l, Arrays.asList(0, 1, 2, 3));
}
@Test public void getOneToN() throws Exception {
// this seems too slow takes 3secs, can it have a buffer of N(1000) so generator can pre-generate records
Integer max = 1_000_000;
Iterator<Integer> i = new CustomGenerator(1000, Integer.MAX_VALUE).call(max, null, null);
List<Integer> l = new ArrayList<>();
while (i.hasNext()) {
l.add(i.next());
}
assertEquals(l.size(), max);
}
@Test public void exceptionTest() throws Exception {
// hangs on error, exception not surfaced to caller
Iterator<Integer> i = new CustomGenerator(1, 3).call(10, null, null);
i.next();
i.next();
String expectedMessage = "foo";
Exception e = assertThrows(RuntimeException.class, () -> { i.next();i.next(); });
assertTrue(e.getCause().getMessage().contains(expectedMessage));
Iterator<Integer> i2 = new CustomGenerator(10, 2).call(3, null, null);
try {
while (i2.hasNext()) {
i2.next();
}
throw new UnexpectedException("should not reach here");
} catch (RuntimeException e1) {
assertTrue(e.getCause().getMessage().contains(expectedMessage));
}
}
public Iterator<Integer> call(Integer key, Iterator<String> left, Iterator<String> right) throws Exception {
return call(key).iterator();
}
public Yield<Integer> call(Integer key) {
return Yield.create(yielder -> {
for(int i = 0;i <key; i++) {
if(i==throwAt) throw new NullPointerException("foo");
yielder.returning(i);
}
});
}