Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResultCollector not compliant with parallelStream requirements #2

Open
ilkosta opened this issue Jul 14, 2020 · 0 comments
Open

ResultCollector not compliant with parallelStream requirements #2

ilkosta opened this issue Jul 14, 2020 · 0 comments

Comments

@ilkosta
Copy link

ilkosta commented Jul 14, 2020

hi,

I'm trying to learn java and i found your article very interesting and useful, able to solve in a clear and simple way what seems to me a deficiency in the current implementation of java.

By trying to use the library I noticed that the characteristics of ResultCollector were not specified.

public Set<Characteristics> characteristics() { return Set.of(); }

So I tried to verify its behavior with a parallel stream and I noticed a wrong result.

  @Test
  void collectorSyncronization() {
    Supplier<Integer> fn = () -> new Random().nextInt(10);
 
    // ok this is std collector
    assertDoesNotThrow(() -> Stream.generate(fn).parallel().limit(100).collect(Collectors.toList()));

    // trying the same with a CheckedSupplier and a Result
    Supplier<Result<Integer,Exception>> randomNumbersSupp=() -> Result.attempt(() -> fn.get());
    Stream<Result<Integer,Exception>> streamOfValues = Stream.generate(randomNumbersSupp)
                                                             .limit(100);

    // with a serial stream all is ok
    List<Integer> values = assertDoesNotThrow( () ->
                                streamOfValues
                                      .collect(new ResultCollector<>())
                                      .getValue().get()
                      );

    // with a parallelStream something go wrong  
    assertThrows( Exception.class, () ->
                          streamOfValues
                                      .parallel()
                                      .collect(new ResultCollector<>())
                                      .getValue().get()
                      );

  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant