use of org.reactivestreams.Publisher in project quasar by puniverse.
the class TestHelper method createDummyFailedPublisher.
public static <T> Publisher<T> createDummyFailedPublisher() {
return new Publisher<T>() {
@Override
public void subscribe(Subscriber<? super T> s) {
s.onSubscribe(new Subscription() {
@Override
public void request(long n) {
}
@Override
public void cancel() {
}
});
s.onError(new RuntimeException("Can't subscribe subscriber: " + s + ", because of reasons."));
}
};
}
use of org.reactivestreams.Publisher in project quasar by puniverse.
the class TwoSidedTest method twoSidedTest.
@Test
public void twoSidedTest() throws Exception {
// Publisher
final Channel<Integer> publisherChannel = Channels.newChannel(random() ? 0 : 5, OverflowPolicy.BLOCK);
final Strand publisherStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (long i = 0; i < ELEMENTS; i++) publisherChannel.send((int) (i % 1000));
publisherChannel.close();
}
}).start();
final Publisher publisher = ReactiveStreams.toPublisher(publisherChannel);
// Subscriber
final ReceivePort<Integer> subscriberChannel = ReactiveStreams.subscribe(buffer, overflowPolicy, publisher);
final Strand subscriberStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
long count = 0;
for (; ; ) {
Integer x = subscriberChannel.receive();
if (x == null)
break;
assertEquals(count % 1000, x.longValue());
count++;
}
subscriberChannel.close();
assertEquals(ELEMENTS, count);
}
}).start();
subscriberStrand.join(5, TimeUnit.SECONDS);
publisherStrand.join(5, TimeUnit.SECONDS);
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class BodyExtractors method readWithMessageReaders.
private static <T, S extends Publisher<T>> S readWithMessageReaders(ReactiveHttpInputMessage inputMessage, BodyExtractor.Context context, ResolvableType elementType, Function<HttpMessageReader<T>, S> readerFunction, Function<Throwable, S> unsupportedError) {
MediaType contentType = contentType(inputMessage);
Supplier<Stream<HttpMessageReader<?>>> messageReaders = context.messageReaders();
return messageReaders.get().filter(r -> r.canRead(elementType, contentType)).findFirst().map(BodyExtractors::<T>cast).map(readerFunction).orElseGet(() -> {
List<MediaType> supportedMediaTypes = messageReaders.get().flatMap(reader -> reader.getReadableMediaTypes().stream()).collect(Collectors.toList());
UnsupportedMediaTypeException error = new UnsupportedMediaTypeException(contentType, supportedMediaTypes);
return unsupportedError.apply(error);
});
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method requestBodyError.
@Test
public void requestBodyError() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)).toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> {
assertThat(error, instanceOf(ServerWebInputException.class));
assertSame(EXCEPTION, error.getCause());
}).verify();
}
use of org.reactivestreams.Publisher in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method controllerThrowsException.
@Test
public void controllerThrowsException() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/raise-exception").toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> assertSame(EXCEPTION, error)).verify();
}
Aggregations