Search in sources :

Example 21 with Publisher

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."));
        }
    };
}
Also used : Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription)

Example 22 with Publisher

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);
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Publisher(org.reactivestreams.Publisher) Strand(co.paralleluniverse.strands.Strand)

Example 23 with Publisher

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);
    });
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) ResolvableType(org.springframework.core.ResolvableType) HttpMessage(org.springframework.http.HttpMessage) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Stream(java.util.stream.Stream)

Example 24 with Publisher

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();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 25 with Publisher

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();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Aggregations

Publisher (org.reactivestreams.Publisher)28 List (java.util.List)11 Mono (reactor.core.publisher.Mono)11 Test (org.junit.Test)9 Collections (java.util.Collections)8 Assert.assertSame (org.junit.Assert.assertSame)7 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)7 HttpStatus (org.springframework.http.HttpStatus)7 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)7 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)7 Duration (java.time.Duration)6 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)6 CoreMatchers.startsWith (org.hamcrest.CoreMatchers.startsWith)6 Matchers.is (org.hamcrest.Matchers.is)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertThat (org.junit.Assert.assertThat)6 Before (org.junit.Before)6 Subscriber (org.reactivestreams.Subscriber)6 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)6 Bean (org.springframework.context.annotation.Bean)6