Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Exception not bubbled to the consumer, need write access to push repro PR #4

@sakthib7

Description

@sakthib7

package gov.nih.ncats.common;

import gov.nih.ncats.common.yield.Yield;
import org.junit.Test;

import java.rmi.UnexpectedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class MoreYieldTest {

@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));
    }
}

}

class CustomGenerator {
private final int throwAt;
public CustomGenerator(int bufferSize, int throwAt) {
this.throwAt = throwAt;
}

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);
        }
    });
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions