-
Notifications
You must be signed in to change notification settings - Fork 356
ensures connection is closed on keepalive timeout #1118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b3a9a5
ensures connection is close on keepalive timeout
OlegDokuka 6ff8f71
fix format
OlegDokuka 1501c76
improve KeepaliveTest
OlegDokuka ff740b3
fix format and failing test
OlegDokuka a8dd315
adds reference to the original GH issue
OlegDokuka b30704b
fixes google format
OlegDokuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
rsocket-transport-netty/src/test/java/io/rsocket/integration/KeepaliveTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package io.rsocket.integration; | ||
|
||
import io.rsocket.Payload; | ||
import io.rsocket.RSocket; | ||
import io.rsocket.core.RSocketClient; | ||
import io.rsocket.core.RSocketConnector; | ||
import io.rsocket.core.RSocketServer; | ||
import io.rsocket.frame.decoder.PayloadDecoder; | ||
import io.rsocket.transport.netty.client.TcpClientTransport; | ||
import io.rsocket.transport.netty.server.CloseableChannel; | ||
import io.rsocket.transport.netty.server.TcpServerTransport; | ||
import io.rsocket.util.DefaultPayload; | ||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Function; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.netty.tcp.TcpClient; | ||
import reactor.netty.tcp.TcpServer; | ||
import reactor.test.StepVerifier; | ||
import reactor.util.retry.Retry; | ||
import reactor.util.retry.RetryBackoffSpec; | ||
|
||
/** | ||
* Test case that reproduces the following <a | ||
* href="https://github.com/rsocket/rsocket-java/issues/1099">GitHub Issue</a> | ||
*/ | ||
public class KeepaliveTest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KeepaliveTest.class); | ||
private static final int PORT = 23200; | ||
|
||
private CloseableChannel server; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
server = createServer().block(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
server.dispose(); | ||
server.onClose().block(); | ||
} | ||
|
||
@Test | ||
void keepAliveTest() { | ||
RSocketClient rsocketClient = createClient(); | ||
|
||
int expectedCount = 4; | ||
AtomicBoolean sleepOnce = new AtomicBoolean(true); | ||
StepVerifier.create( | ||
Flux.range(0, expectedCount) | ||
.delayElements(Duration.ofMillis(2000)) | ||
.concatMap( | ||
i -> | ||
rsocketClient | ||
.requestResponse(Mono.just(DefaultPayload.create(""))) | ||
.doOnNext( | ||
__ -> { | ||
if (sleepOnce.getAndSet(false)) { | ||
try { | ||
LOG.info("Sleeping..."); | ||
Thread.sleep(1_000); | ||
LOG.info("Waking up."); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}) | ||
.log("id " + i) | ||
.onErrorComplete())) | ||
.expectSubscription() | ||
.expectNextCount(expectedCount) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
void keepAliveTestLazy() { | ||
Mono<RSocket> rsocketMono = createClientLazy(); | ||
|
||
int expectedCount = 4; | ||
AtomicBoolean sleepOnce = new AtomicBoolean(true); | ||
StepVerifier.create( | ||
Flux.range(0, expectedCount) | ||
.delayElements(Duration.ofMillis(2000)) | ||
.concatMap( | ||
i -> | ||
rsocketMono.flatMap( | ||
rsocket -> | ||
rsocket | ||
.requestResponse(DefaultPayload.create("")) | ||
.doOnNext( | ||
__ -> { | ||
if (sleepOnce.getAndSet(false)) { | ||
try { | ||
LOG.info("Sleeping..."); | ||
Thread.sleep(1_000); | ||
LOG.info("Waking up."); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}) | ||
.log("id " + i) | ||
.onErrorComplete()))) | ||
.expectSubscription() | ||
.expectNextCount(expectedCount) | ||
.verifyComplete(); | ||
} | ||
|
||
private static Mono<CloseableChannel> createServer() { | ||
LOG.info("Starting server at port {}", PORT); | ||
|
||
TcpServer tcpServer = TcpServer.create().host("localhost").port(PORT); | ||
|
||
return RSocketServer.create( | ||
(setupPayload, rSocket) -> { | ||
rSocket | ||
.onClose() | ||
.doFirst(() -> LOG.info("Connected on server side.")) | ||
.doOnTerminate(() -> LOG.info("Connection closed on server side.")) | ||
.subscribe(); | ||
|
||
return Mono.just(new MyServerRsocket()); | ||
}) | ||
.payloadDecoder(PayloadDecoder.ZERO_COPY) | ||
.bind(TcpServerTransport.create(tcpServer)) | ||
.doOnNext(closeableChannel -> LOG.info("RSocket server started.")); | ||
} | ||
|
||
private static RSocketClient createClient() { | ||
LOG.info("Connecting...."); | ||
|
||
Function<String, RetryBackoffSpec> reconnectSpec = | ||
reason -> | ||
Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(10L)) | ||
.doBeforeRetry(retrySignal -> LOG.info("Reconnecting. Reason: {}", reason)); | ||
|
||
Mono<RSocket> rsocketMono = | ||
RSocketConnector.create() | ||
.fragment(16384) | ||
.reconnect(reconnectSpec.apply("connector-close")) | ||
.keepAlive(Duration.ofMillis(100L), Duration.ofMillis(900L)) | ||
.connect(TcpClientTransport.create(TcpClient.create().host("localhost").port(PORT))); | ||
|
||
RSocketClient client = RSocketClient.from(rsocketMono); | ||
|
||
client | ||
.source() | ||
.doOnNext(r -> LOG.info("Got RSocket")) | ||
.flatMap(RSocket::onClose) | ||
.doOnError(err -> LOG.error("Error during onClose.", err)) | ||
.retryWhen(reconnectSpec.apply("client-close")) | ||
.doFirst(() -> LOG.info("Connected on client side.")) | ||
.doOnTerminate(() -> LOG.info("Connection closed on client side.")) | ||
.repeat() | ||
.subscribe(); | ||
|
||
return client; | ||
} | ||
|
||
private static Mono<RSocket> createClientLazy() { | ||
LOG.info("Connecting...."); | ||
|
||
Function<String, RetryBackoffSpec> reconnectSpec = | ||
reason -> | ||
Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(10L)) | ||
.doBeforeRetry(retrySignal -> LOG.info("Reconnecting. Reason: {}", reason)); | ||
|
||
return RSocketConnector.create() | ||
.fragment(16384) | ||
.reconnect(reconnectSpec.apply("connector-close")) | ||
.keepAlive(Duration.ofMillis(100L), Duration.ofMillis(900L)) | ||
.connect(TcpClientTransport.create(TcpClient.create().host("localhost").port(PORT))); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The createClient methods maybe can also be combined into one with a |
||
|
||
public static class MyServerRsocket implements RSocket { | ||
|
||
@Override | ||
public Mono<Payload> requestResponse(Payload payload) { | ||
return Mono.just("Pong").map(DefaultPayload::create); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.