@Override public Flux<?> apply(Single<?> source) { return Flux.defer(() -> RxReactiveStreams.toPublisher(source)); } }
@Override public Publisher<?> apply(Observable<?> source) { return Flux.defer(() -> new PublisherAdapter<>(source)); } }
@Override public Publisher<?> apply(Completable source) { return Flux.defer(() -> RxReactiveStreams.toPublisher(source)); } }
@Override public Publisher<?> apply(Single<?> source) { return Flux.defer(() -> RxReactiveStreams.toPublisher(source)); } }
@Override public Flux<InstanceEvent> find(InstanceId id) { return Flux.defer(() -> Flux.fromIterable(eventLog.getOrDefault(id, Collections.emptyList()))); }
@Override public Flux<InstanceEvent> findAll() { return Flux.defer(() -> Flux.fromIterable(eventLog.values()) .flatMapIterable(Function.identity()) .sort(byTimestampAndIdAndVersion)); }
/** * Relay buffers from the given {@link Publisher} until the total * {@linkplain DataBuffer#readableByteCount() byte count} reaches * the given maximum byte count, or until the publisher is complete. * @param publisher the publisher to filter * @param maxByteCount the maximum byte count * @return a flux whose maximum byte count is {@code maxByteCount} */ public static Flux<DataBuffer> takeUntilByteCount(Publisher<DataBuffer> publisher, long maxByteCount) { Assert.notNull(publisher, "Publisher must not be null"); Assert.isTrue(maxByteCount >= 0, "'maxByteCount' must be a positive number"); return Flux.defer(() -> { AtomicLong countDown = new AtomicLong(maxByteCount); return Flux.from(publisher) .map(buffer -> { long remainder = countDown.addAndGet(-buffer.readableByteCount()); if (remainder < 0) { int length = buffer.readableByteCount() + (int) remainder; return buffer.slice(0, length); } else { return buffer; } }) .takeUntil(buffer -> countDown.get() <= 0); }); // no doOnDiscard necessary, as this method does not drop buffers }
Assert.isTrue(maxByteCount >= 0, "'maxByteCount' must be a positive number"); return Flux.defer(() -> { AtomicLong countDown = new AtomicLong(maxByteCount); return Flux.from(publisher)
@Override public Flux<?> apply(Observable<?> source) { return Flux.defer(() -> Flux.from((Publisher<?>) RxReactiveStreams.toPublisher(source))); } }
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }
private Flux<DataBuffer> getRegionSuffix(DataBufferFactory bufferFactory, String boundaryString) { byte[] endBoundary = getAsciiBytes("\r\n--" + boundaryString + "--"); return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(endBoundary.length).write(endBoundary))); }
private Flux<DataBuffer> getRegionPrefix(DataBufferFactory bufferFactory, byte[] startBoundary, byte[] contentType, ResourceRegion region) { return Flux.defer(() -> Flux.just( bufferFactory.allocateBuffer(startBoundary.length).write(startBoundary), bufferFactory.allocateBuffer(contentType.length).write(contentType), bufferFactory.wrap(ByteBuffer.wrap(getContentRangeHeader(region)))) ); }
@Test(expected = RuntimeException.class) public void blockingLastError2() { Flux.defer(() -> Mono.error(new RuntimeException("test"))) .subscribeOn(scheduler) .blockLast(Duration.ofSeconds(1)); }
@Test public void deferStream(){ AtomicInteger i = new AtomicInteger(); Flux<Integer> source = Flux.defer(() -> Flux.just(i.incrementAndGet())); Assert.assertEquals(source.blockLast().intValue(), 1); Assert.assertEquals(source.blockLast().intValue(), 2); Assert.assertEquals(source.blockLast().intValue(), 3); } }
@Test(expected = RuntimeException.class) public void blockingLastError() { Flux.defer(() -> Mono.error(new RuntimeException("test"))) .subscribeOn(scheduler) .blockLast(); }
@Test public void testMonoThenManySupplier() { AssertSubscriber<String> ts = AssertSubscriber.create(); Flux<String> test = Mono.just(1).thenMany(Flux.defer(() -> Flux.just("A", "B"))); test.subscribe(ts); ts.assertValues("A", "B"); ts.assertComplete(); }
@Test public void supplierThrows() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.<Integer>defer(() -> { throw new RuntimeException("forced failure"); }).subscribe(ts); ts.assertNoValues() .assertNotComplete() .assertError(RuntimeException.class) .assertErrorMessage("forced failure"); }
@Test public void supplierReturnsNull() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.<Integer>defer(() -> null).subscribe(ts); ts.assertNoValues() .assertNotComplete() .assertError(NullPointerException.class); }
@Test public void thenManySupplier(){ StepVerifier.create(Flux.just(1, 2, 3).thenMany(Flux.defer(() -> Flux.just("test", "test2")))) .expectNext("test", "test2") .verifyComplete(); }