Skip to content

Commit

Permalink
Introduce Flux.to{Stream,Iterable} Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
benhalasi committed Jan 20, 2023
1 parent 499f922 commit fd8e0ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static reactor.function.TupleUtils.function;

Expand All @@ -27,6 +28,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -1298,4 +1300,35 @@ Duration after(StepVerifier.LastStep step, Duration duration) {
return step.verifyTimeout(duration);
}
}

/** Avoid accidental blocking with {@link Flux#toStream()} */
// XXX: The alternative may lose some performance due to
// buffering and prefetching
static final class FluxToStream<T> {

@BeforeTemplate
Stream<T> before(Flux<T> flux) {
return flux.toStream();
}

@AfterTemplate
Stream<T> after(Flux<T> flux) {
return flux.collect(toList()).block().stream();
}
}

/** Avoid accidental blocking with {@link Flux#toIterable()} */
// XXX: The alternative may lose some performance due to buffering and prefetching
static final class FluxToIterable<T> {

@BeforeTemplate
Iterable<T> before(Flux<T> flux) {
return flux.toIterable();
}

@AfterTemplate
Iterable<T> after(Flux<T> flux) {
return flux.collect(toList()).block();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand All @@ -23,7 +25,7 @@
final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class);
return ImmutableSet.of(assertThat(0), Collectors.class, HashMap.class, ImmutableMap.class);
}

ImmutableSet<Mono<?>> testMonoFromSupplier() {
Expand Down Expand Up @@ -402,4 +404,12 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return StepVerifier.create(Mono.empty()).expectTimeout(Duration.ZERO).verify();
}

Stream<Integer> testFluxToStream() {
return Flux.just(1, 2, 3).toStream();
}

Iterable<Integer> testFluxToIterable() {
return Flux.just(1, 2, 3).toIterable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.function.TupleUtils;
Expand All @@ -26,7 +28,7 @@
final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class);
return ImmutableSet.of(assertThat(0), Collectors.class, HashMap.class, ImmutableMap.class);
}

ImmutableSet<Mono<?>> testMonoFromSupplier() {
Expand Down Expand Up @@ -391,4 +393,12 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return StepVerifier.create(Mono.empty()).verifyTimeout(Duration.ZERO);
}

Stream<Integer> testFluxToStream() {
return Flux.just(1, 2, 3).collect(Collectors.toList()).block().stream();
}

Iterable<Integer> testFluxToIterable() {
return Flux.just(1, 2, 3).collect(Collectors.toList()).block();
}
}

0 comments on commit fd8e0ef

Please sign in to comment.