Skip to content

Cannot use IterableStream with Schedulers.parallel() or Schedulers.single() #6515

Description

@conniey

Repo:

  1. Copy the snippet below
  2. Execute test.

Expected: It passes
Actual:

java.lang.AssertionError: expectation "assertNext" failed
(expected: onNext(); actual: onError(java.lang.IllegalStateException: 
Iterating over a toIterable() / toStream() is blocking, which is not supported in thread parallel-1))

Snippet

import com.azure.core.util.IterableStream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.util.Arrays;

class IterableStreamTest {
    @Test
    public void repro() {
        Foo foo = new Foo();

        // Also fails with Schedulers.single().
        Scheduler scheduler = Schedulers.parallel();

        System.out.println("Using array.");
        StepVerifier.create(foo.getPartitionIdsFromArray().subscribeOn(scheduler))
            .assertNext(id -> Assertions.assertEquals("0", id))
            .assertNext(id -> Assertions.assertEquals("1", id))
            .assertNext(id -> Assertions.assertEquals("2", id))
            .expectComplete()
            .verify(Duration.ofSeconds(5));

        System.out.println("Using IterableStream.");
        StepVerifier.create(foo.getPartitionIds().subscribeOn(scheduler))
            .assertNext(id -> Assertions.assertEquals("0", id))
            .assertNext(id -> Assertions.assertEquals("1", id))
            .assertNext(id -> Assertions.assertEquals("2", id))
            .expectComplete()
            .verify(Duration.ofSeconds(5));
    }

    static class Foo {
        Mono<Bar> getProperties() {
            return Mono.fromCallable(() -> new Bar(new String[]{"0", "1", "2"}));
        }

        Flux<String> getPartitionIds() {
            return getProperties().flatMapMany(x -> Flux.fromIterable(x.getPartitionIds()));
        }

        Flux<String> getPartitionIdsFromArray() {
            return getProperties().flatMapMany(x -> Flux.fromArray(x.getPartitionIdsArray()));
        }
    }

    static class Bar {
        private final IterableStream<String> partitionIds;
        private final String[] partitionArrays;

        Bar(String[] partitionIds) {
            this.partitionIds = partitionIds != null
                ? new IterableStream<>(Flux.fromArray(partitionIds))
                : new IterableStream<>(Flux.empty());
            this.partitionArrays = partitionIds;
        }

        IterableStream<String> getPartitionIds() {
            return partitionIds;
        }

        String[] getPartitionIdsArray() {
            return Arrays.copyOf(partitionArrays, partitionArrays.length);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Azure.Coreazure-corebugThis issue requires a change to an existing behavior in the product in order to be resolved.

    Type

    No type

    Fields

    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