Search in sources :

Example 31 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer 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 32 with DataBuffer

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

the class ServerHttpResponseTests method writeWithError.

void writeWithError(Publisher<DataBuffer> body) {
    TestServerHttpResponse response = new TestServerHttpResponse();
    HttpHeaders headers = response.getHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set(HttpHeaders.CONTENT_ENCODING, "gzip");
    headers.setContentLength(12);
    response.writeWith(body).onErrorResume(ex -> Mono.empty()).block();
    assertThat(response.statusCodeWritten).isFalse();
    assertThat(response.headersWritten).isFalse();
    assertThat(response.cookiesWritten).isFalse();
    assertThat(headers).doesNotContainKeys(HttpHeaders.CONTENT_TYPE, HttpHeaders.CONTENT_LENGTH, HttpHeaders.CONTENT_ENCODING);
    assertThat(response.body.isEmpty()).isTrue();
}
Also used : StepVerifier(reactor.test.StepVerifier) AbortedException(reactor.netty.channel.AbortedException) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Map(java.util.Map) ResolvableType(org.springframework.core.ResolvableType) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) HttpHeaders(org.springframework.http.HttpHeaders)

Example 33 with DataBuffer

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

the class ListenerWriteProcessorTests method writePublisherError.

// SPR-17410
@Test
public void writePublisherError() {
    // Turn off writing so next item will be cached
    this.processor.setWritePossible(false);
    DataBuffer buffer = mock(DataBuffer.class);
    this.processor.onNext(buffer);
    // Send error while item cached
    this.processor.onError(new IllegalStateException());
    assertThat(this.resultSubscriber.getError()).as("Error should flow to result publisher").isNotNull();
    assertThat(this.processor.getDiscardedBuffers().size()).isEqualTo(1);
    assertThat(this.processor.getDiscardedBuffers().get(0)).isSameAs(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 34 with DataBuffer

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

the class JettyClientHttpConnector method toDataBuffer.

private DataBuffer toDataBuffer(ContentChunk chunk) {
    // Originally we copy due to do:
    // https://github.com/eclipse/jetty.project/issues/2429
    // Now that the issue is marked fixed we need to replace the below with a
    // PooledDataBuffer that adapts "release()" to "succeeded()", and also
    // evaluate if the concern here is addressed.
    DataBuffer buffer = this.bufferFactory.allocateBuffer(chunk.buffer.capacity());
    buffer.write(chunk.buffer);
    chunk.callback.succeeded();
    return buffer;
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 35 with DataBuffer

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

the class MockClientHttpResponse method setBody.

public void setBody(String body, Charset charset) {
    DataBuffer buffer = toDataBuffer(body, charset);
    this.body = Flux.just(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)30 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23