Search in sources :

Example 1 with LeakAwareDataBufferFactory

use of org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method errorFromWriteFunctionWhileItemCached.

// gh-22720
@Test
public void errorFromWriteFunctionWhileItemCached() {
    // 1. First item received
    // 2. writeFunction applied and writeCompletionBarrier subscribed to it
    // 3. writeFunction fails, e.g. to flush status and headers, before request(n) from server
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Flux.create(sink -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        sink.next(dataBuffer);
    }), publisher -> {
        publisher.subscribe(new ZeroDemandSubscriber());
        return Mono.error(new IllegalStateException("err"));
    });
    StepVerifier.create(operator).expectErrorMessage("err").verify(Duration.ofSeconds(5));
    bufferFactory.checkForLeaks();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Signal(reactor.core.publisher.Signal) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with LeakAwareDataBufferFactory

use of org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method cancelWhileItemCached.

// gh-22720
@Test
public void cancelWhileItemCached() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Mono.fromCallable(() -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        return dataBuffer;
    }), publisher -> {
        ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
        publisher.subscribe(subscriber);
        return Mono.never();
    });
    BaseSubscriber<Void> subscriber = new BaseSubscriber<>() {
    };
    operator.subscribe(subscriber);
    subscriber.cancel();
    bufferFactory.checkForLeaks();
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with LeakAwareDataBufferFactory

use of org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method errorFromWriteSourceWhileItemCached.

// gh-22720
@Test
public void errorFromWriteSourceWhileItemCached() {
    // 1. First item received
    // 2. writeFunction applied and writeCompletionBarrier subscribed to it
    // 3. Write Publisher fails right after that and before request(n) from server
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ZeroDemandSubscriber writeSubscriber = new ZeroDemandSubscriber();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Flux.create(sink -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        sink.next(dataBuffer);
        sink.error(new IllegalStateException("err"));
    }), publisher -> {
        publisher.subscribe(writeSubscriber);
        return Mono.never();
    });
    operator.subscribe(new BaseSubscriber<Void>() {
    });
    try {
        // Let cached signals ("foo" and error) be published..
        writeSubscriber.signalDemand(1);
    } catch (Throwable ex) {
        assertThat(ex.getCause()).isNotNull();
        assertThat(ex.getCause().getMessage()).isEqualTo("err");
    }
    bufferFactory.checkForLeaks();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Signal(reactor.core.publisher.Signal) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with LeakAwareDataBufferFactory

use of org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory in project spring-framework by spring-projects.

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
    response.setWriteHandler(flux -> {
        throw AbortedException.beforeSend();
    });
    HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
    Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Map(java.util.Map) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 LeakAwareDataBufferFactory (org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory)4 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 BaseSubscriber (reactor.core.publisher.BaseSubscriber)3 Mono (reactor.core.publisher.Mono)3 StandardCharsets (java.nio.charset.StandardCharsets)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Executors (java.util.concurrent.Executors)2 TimeUnit (java.util.concurrent.TimeUnit)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Publisher (org.reactivestreams.Publisher)2 Subscriber (org.reactivestreams.Subscriber)2 Subscription (org.reactivestreams.Subscription)2 Flux (reactor.core.publisher.Flux)2 Signal (reactor.core.publisher.Signal)2 StepVerifier (reactor.test.StepVerifier)2 Map (java.util.Map)1