Search in sources :

Example 1 with Msg

use of cn.taketoday.protobuf.Msg in project today-infrastructure by TAKETODAY.

the class ProtobufDecoderTests method decodeSplitMessageSize.

// SPR-17429
@Test
public void decodeSplitMessageSize() {
    this.decoder.setMaxMessageSize(100009);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 10000; i++) {
        builder.append("azertyuiop");
    }
    Msg bigMessage = Msg.newBuilder().setFoo(builder.toString()).setBlah(secondMsg2).build();
    Flux<DataBuffer> input = Flux.just(bigMessage, bigMessage).flatMap(msg -> Mono.defer(() -> {
        DataBuffer buffer = this.bufferFactory.allocateBuffer();
        try {
            msg.writeDelimitedTo(buffer.asOutputStream());
            return Mono.just(buffer);
        } catch (IOException e) {
            release(buffer);
            return Mono.error(e);
        }
    })).flatMap(buffer -> {
        int len = 2;
        Flux<DataBuffer> result = Flux.just(DataBufferUtils.retain(buffer.slice(0, len)), DataBufferUtils.retain(buffer.slice(len, buffer.readableByteCount() - len)));
        release(buffer);
        return result;
    });
    testDecode(input, Msg.class, step -> step.expectNext(bigMessage).expectNext(bigMessage).verifyComplete());
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) Collections.emptyMap(java.util.Collections.emptyMap) Arrays(java.util.Arrays) Msg(cn.taketoday.protobuf.Msg) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResolvableType(cn.taketoday.core.ResolvableType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) AbstractDecoderTests(cn.taketoday.core.codec.AbstractDecoderTests) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) DataBufferUtils.release(cn.taketoday.core.io.buffer.DataBufferUtils.release) SecondMsg(cn.taketoday.protobuf.SecondMsg) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Message(com.google.protobuf.Message) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MediaType(cn.taketoday.http.MediaType) ResolvableType.fromClass(cn.taketoday.core.ResolvableType.fromClass) DecodingException(cn.taketoday.core.codec.DecodingException) MimeType(cn.taketoday.util.MimeType) IOException(java.io.IOException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with Msg

use of cn.taketoday.protobuf.Msg in project today-infrastructure by TAKETODAY.

the class CancelWithoutDemandCodecTests method cancelWithProtobufDecoder.

// gh-22731
@Test
public void cancelWithProtobufDecoder() throws InterruptedException {
    ProtobufDecoder decoder = new ProtobufDecoder();
    Mono<DataBuffer> input = Mono.fromCallable(() -> {
        Msg msg = Msg.newBuilder().setFoo("Foo").build();
        byte[] bytes = msg.toByteArray();
        DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
        buffer.write(bytes);
        return buffer;
    });
    Flux<Message> messages = decoder.decode(input, ResolvableType.fromType(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    ZeroDemandMessageSubscriber subscriber = new ZeroDemandMessageSubscriber();
    messages.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with Msg

use of cn.taketoday.protobuf.Msg in project today-infrastructure by TAKETODAY.

the class CancelWithoutDemandCodecTests method cancelWithProtobufEncoder.

// gh-22543
@Test
public void cancelWithProtobufEncoder() {
    ProtobufEncoder encoder = new ProtobufEncoder();
    Msg msg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
    Flux<DataBuffer> flux = encoder.encode(Mono.just(msg), this.bufferFactory, ResolvableType.fromClass(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
    // Assume sync execution (e.g. encoding with Flux.just)..
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with Msg

use of cn.taketoday.protobuf.Msg in project today-framework by TAKETODAY.

the class CancelWithoutDemandCodecTests method cancelWithProtobufDecoder.

// gh-22731
@Test
public void cancelWithProtobufDecoder() throws InterruptedException {
    ProtobufDecoder decoder = new ProtobufDecoder();
    Mono<DataBuffer> input = Mono.fromCallable(() -> {
        Msg msg = Msg.newBuilder().setFoo("Foo").build();
        byte[] bytes = msg.toByteArray();
        DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
        buffer.write(bytes);
        return buffer;
    });
    Flux<Message> messages = decoder.decode(input, ResolvableType.fromType(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    ZeroDemandMessageSubscriber subscriber = new ZeroDemandMessageSubscriber();
    messages.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with Msg

use of cn.taketoday.protobuf.Msg in project today-framework by TAKETODAY.

the class CancelWithoutDemandCodecTests method cancelWithProtobufEncoder.

// gh-22543
@Test
public void cancelWithProtobufEncoder() {
    ProtobufEncoder encoder = new ProtobufEncoder();
    Msg msg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
    Flux<DataBuffer> flux = encoder.encode(Mono.just(msg), this.bufferFactory, ResolvableType.fromClass(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
    // Assume sync execution (e.g. encoding with Flux.just)..
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 Msg (cn.taketoday.protobuf.Msg)8 SecondMsg (cn.taketoday.protobuf.SecondMsg)8 MimeType (cn.taketoday.util.MimeType)8 Test (org.junit.jupiter.api.Test)8 Message (com.google.protobuf.Message)6 ResolvableType (cn.taketoday.core.ResolvableType)4 ResolvableType.fromClass (cn.taketoday.core.ResolvableType.fromClass)4 AbstractDecoderTests (cn.taketoday.core.codec.AbstractDecoderTests)4 DecodingException (cn.taketoday.core.codec.DecodingException)4 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)4 DataBufferUtils.release (cn.taketoday.core.io.buffer.DataBufferUtils.release)4 MediaType (cn.taketoday.http.MediaType)4 IOException (java.io.IOException)4 Arrays (java.util.Arrays)4 Collections.emptyMap (java.util.Collections.emptyMap)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 Flux (reactor.core.publisher.Flux)4 Mono (reactor.core.publisher.Mono)4