use of org.reactivestreams.Publisher in project RxJava by ReactiveX.
the class TrampolineSchedulerTest method testTrampolineWorkerHandlesConcurrentScheduling.
/**
* This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
* action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
* sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
* trampoline with enqueue requests from multiple threads concurrently.
*/
@Test
public void testTrampolineWorkerHandlesConcurrentScheduling() {
final Worker trampolineWorker = Schedulers.trampoline().createWorker();
final Subscriber<Object> observer = TestHelper.mockSubscriber();
final TestSubscriber<Disposable> ts = new TestSubscriber<Disposable>(observer);
// Spam the trampoline with actions.
Flowable.range(0, 50).flatMap(new Function<Integer, Publisher<Disposable>>() {
@Override
public Publisher<Disposable> apply(Integer count) {
return Flowable.interval(1, TimeUnit.MICROSECONDS).map(new Function<Long, Disposable>() {
@Override
public Disposable apply(Long ount1) {
return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
}
}).take(100);
}
}).subscribeOn(Schedulers.computation()).subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNoErrors();
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class DataBufferUtils method skipUntilByteCount.
/**
* Skip 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 with the remaining part of the given publisher
*/
public static Flux<DataBuffer> skipUntilByteCount(Publisher<DataBuffer> publisher, long maxByteCount) {
Assert.notNull(publisher, "Publisher must not be null");
Assert.isTrue(maxByteCount >= 0, "'maxByteCount' must be a positive number");
AtomicLong byteCountDown = new AtomicLong(maxByteCount);
return Flux.from(publisher).skipUntil(dataBuffer -> {
int delta = -dataBuffer.readableByteCount();
long currentCount = byteCountDown.addAndGet(delta);
if (currentCount < 0) {
return true;
} else {
DataBufferUtils.release(dataBuffer);
return false;
}
}).map(dataBuffer -> {
long currentCount = byteCountDown.get();
if (currentCount < 0) {
int skip = (int) (currentCount + dataBuffer.readableByteCount());
byteCountDown.set(0);
return dataBuffer.slice(skip, dataBuffer.readableByteCount() - skip);
}
return dataBuffer;
});
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriter method write.
@Override
public Mono<Void> write(Publisher<?> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
outputMessage.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
Flux<Publisher<DataBuffer>> body = encode(inputStream, bufferFactory, elementType, hints);
return outputMessage.writeAndFlushWith(body);
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class Jackson2JsonEncoder method encode.
@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
Assert.notNull(elementType, "'elementType' must not be null");
if (inputStream instanceof Mono) {
return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
} else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
return Flux.from(inputStream).map(value -> {
DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
buffer.write(new byte[] { '\n' });
return buffer;
});
}
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class RxNettyHttpHandlerAdapter method handle.
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> nativeRequest, HttpServerResponse<ByteBuf> nativeResponse) {
Channel channel = nativeResponse.unsafeNettyChannel();
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(channel.alloc());
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
RxNettyServerHttpRequest request = new RxNettyServerHttpRequest(nativeRequest, bufferFactory, remoteAddress);
RxNettyServerHttpResponse response = new RxNettyServerHttpResponse(nativeResponse, bufferFactory);
Publisher<Void> result = this.httpHandler.handle(request, response).otherwise(ex -> {
logger.error("Could not complete request", ex);
nativeResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return Mono.empty();
}).doOnSuccess(aVoid -> logger.debug("Successfully completed request"));
return RxReactiveStreams.toObservable(result);
}
Aggregations