jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.09k stars 168 forks source link

A strange issue on java 8: consumer isn't being called #392

Closed cvgaviao closed 2 years ago

cvgaviao commented 2 years ago

Expected behavior and actual behavior:

I've wrapped a method that throws an exception inside the Unchecked.consumer I was expecting this method to be called, but strangely it is not.

Steps to reproduce the problem:

    public long generateCSV(final File pOutputFile, 
                          final Supplier<Stream<String[]>> pRowDataStream,
                          Consumer<Throwable> pExceptionHandler) {
    AtomicLong counter = new AtomicLong();
    try (CSVPrinter pw = new CSVPrinter(
        new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(pOutputFile, false), StandardCharsets.ISO_8859_1)),
        CSVFormat.TDF)) {
      // print the sequence of rows
      pRowDataStream.get().forEach(r -> {
        Unchecked.consumer(row -> pw.printRecord((Object[]) row), pExceptionHandler);
        counter.incrementAndGet();
      });
    } catch (IOException e) {
      pExceptionHandler.accept(e);
    }
    return counter.get();
  }
}
public class FileGeneratorTest {
  Stream<String[]> getRows() {
    return Stream.of(new String[] { "a1", "b1" }, new String[] { "a2", "b2" });
  }
  @Test
  public void testFileGenerator() {
    FileGenerator generator = new FileGenerator();
    File output = Files.newTemporaryFile();
    long count = generator.generateCSV(output, this::getRows, e -> {
      fail(e.getMessage());
    });
    assertThat(count).isEqualTo(2);
    assertThat(output).exists().isNotEmpty().hasContent("a\tb\ra1\tb1\ra2\tb2");
  }
}

I've tried to setup a breakpoint inside the method, but eclipse just pass for it and don't stop.

Versions:

lukaseder commented 2 years ago

Thanks for your message.

Unchecked::consumer wraps a CheckedConsumer<T> to produce a JDK Consumer<T>. It does not have any side effect, let alone run your consumer, so your statement does not have any effect.

You probably meant to write this?

pRowDataStream.get().forEach(Unchecked.consumer(
  r -> {
    pw.printRecord((Object[]) r);
    counter.incrementAndGet();
  }, 
  pExceptionHandler
));