|
| 1 | +/* |
| 2 | + * Copyright 2015-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.rsocket.transport.netty; |
| 18 | + |
| 19 | +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
| 20 | +import io.netty.incubator.codec.http3.Http3; |
| 21 | +import io.netty.incubator.codec.quic.QuicSslContext; |
| 22 | +import io.netty.incubator.codec.quic.QuicSslContextBuilder; |
| 23 | +import io.rsocket.RSocket; |
| 24 | +import io.rsocket.core.RSocketConnector; |
| 25 | +import io.rsocket.frame.decoder.PayloadDecoder; |
| 26 | +import io.rsocket.test.PerfTest; |
| 27 | +import io.rsocket.test.PingClient; |
| 28 | +import io.rsocket.transport.netty.client.Http3ClientTransport; |
| 29 | +import java.time.Duration; |
| 30 | +import org.HdrHistogram.Recorder; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import reactor.core.publisher.Mono; |
| 34 | + |
| 35 | +@PerfTest |
| 36 | +public final class Http3Ping { |
| 37 | + private static final int INTERACTIONS_COUNT = |
| 38 | + Integer.valueOf(System.getProperty("RSOCKET_HTTP3_INTERACTIONS", "1000000000")); |
| 39 | + private static final String host = System.getProperty("RSOCKET_TEST_HOST", "127.0.0.1"); |
| 40 | + private static final int port = |
| 41 | + Integer.valueOf( |
| 42 | + System.getProperty( |
| 43 | + "RSOCKET_TEST_PORT", String.valueOf(Http3TransportConfig.DEFAULT_PORT))); |
| 44 | + private static final String path = |
| 45 | + System.getProperty("RSOCKET_TEST_PATH", Http3TransportConfig.DEFAULT_PATH); |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + System.out.println("Starting ping-pong test (HTTP/3 transport)"); |
| 50 | + System.out.println("host: " + host); |
| 51 | + System.out.println("port: " + port); |
| 52 | + System.out.println("path: " + path); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void requestResponseTest() throws Exception { |
| 57 | + PingClient pingClient = newPingClient(); |
| 58 | + Recorder recorder = pingClient.startTracker(Duration.ofSeconds(1)); |
| 59 | + |
| 60 | + pingClient |
| 61 | + .requestResponsePingPong(INTERACTIONS_COUNT, recorder) |
| 62 | + .doOnTerminate(() -> System.out.println("Sent " + INTERACTIONS_COUNT + " messages.")) |
| 63 | + .blockLast(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void requestStreamTest() throws Exception { |
| 68 | + PingClient pingClient = newPingClient(); |
| 69 | + Recorder recorder = pingClient.startTracker(Duration.ofSeconds(1)); |
| 70 | + |
| 71 | + pingClient |
| 72 | + .requestStreamPingPong(INTERACTIONS_COUNT, recorder) |
| 73 | + .doOnTerminate(() -> System.out.println("Sent " + INTERACTIONS_COUNT + " messages.")) |
| 74 | + .blockLast(); |
| 75 | + } |
| 76 | + |
| 77 | + private static PingClient newPingClient() throws Exception { |
| 78 | + QuicSslContext clientSslContext = |
| 79 | + QuicSslContextBuilder.forClient() |
| 80 | + .trustManager(InsecureTrustManagerFactory.INSTANCE) |
| 81 | + .applicationProtocols(Http3.supportedApplicationProtocols()) |
| 82 | + .build(); |
| 83 | + |
| 84 | + Http3TransportConfig clientConfig = |
| 85 | + Http3TransportConfig.builder() |
| 86 | + .path(path) |
| 87 | + .quicConfig( |
| 88 | + QuicTransportConfig.builder() |
| 89 | + .sslContext(clientSslContext) |
| 90 | + .validateCertificates(false) |
| 91 | + .build()) |
| 92 | + .build(); |
| 93 | + |
| 94 | + Mono<RSocket> rSocket = |
| 95 | + RSocketConnector.create() |
| 96 | + .payloadDecoder(PayloadDecoder.ZERO_COPY) |
| 97 | + .keepAlive(Duration.ofMinutes(1), Duration.ofMinutes(30)) |
| 98 | + .connect(Http3ClientTransport.create(host, port).config(clientConfig)); |
| 99 | + |
| 100 | + return new PingClient(rSocket); |
| 101 | + } |
| 102 | +} |
0 commit comments