Search in sources :

Example 1 with LeakAwareDataBufferFactory

use of cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory in project today-infrastructure by TAKETODAY.

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) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) Subscription(org.reactivestreams.Subscription) Duration(java.time.Duration) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with LeakAwareDataBufferFactory

use of cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory in project today-framework by TAKETODAY.

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) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) Subscription(org.reactivestreams.Subscription) Duration(java.time.Duration) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with LeakAwareDataBufferFactory

use of cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory in project today-framework by TAKETODAY.

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) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) Subscription(org.reactivestreams.Subscription) Duration(java.time.Duration) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with LeakAwareDataBufferFactory

use of cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory in project today-framework by TAKETODAY.

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<Void>() {
    };
    operator.subscribe(subscriber);
    subscriber.cancel();
    bufferFactory.checkForLeaks();
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with LeakAwareDataBufferFactory

use of cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory in project today-framework by TAKETODAY.

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

@Test
// gh-26232
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.fromClass(Mono.class), ResolvableType.fromClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) Map(java.util.Map) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Aggregations

LeakAwareDataBufferFactory (cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory)8 Test (org.junit.jupiter.api.Test)8 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)6 BaseSubscriber (reactor.core.publisher.BaseSubscriber)6 Mono (reactor.core.publisher.Mono)6 StandardCharsets (java.nio.charset.StandardCharsets)4 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Executors (java.util.concurrent.Executors)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Publisher (org.reactivestreams.Publisher)4 Subscriber (org.reactivestreams.Subscriber)4 Subscription (org.reactivestreams.Subscription)4 Flux (reactor.core.publisher.Flux)4 Signal (reactor.core.publisher.Signal)4 StepVerifier (reactor.test.StepVerifier)4 EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)2