-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #61 Offer a simple blocking alternative API for client/server
This commit offers a simplified API to start/stop client and servers in a blocking fashion. - `start` method blocks until initialized, then offers a simplified API to shutdown. - `startAndAwait` blocks for the whole lifecycle of the client/server, which would usually be used in a main class (only way to shut down some servers would be to sigkill).
- Loading branch information
1 parent
4cc5e04
commit d9d9563
Showing
7 changed files
with
349 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
98 changes: 98 additions & 0 deletions
98
src/main/java/reactor/ipc/netty/tcp/BlockingNettyContext.java
This file contains 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,98 @@ | ||
package reactor.ipc.netty.tcp; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import reactor.core.publisher.Mono; | ||
import reactor.ipc.netty.NettyContext; | ||
import reactor.util.Logger; | ||
import reactor.util.Loggers; | ||
|
||
/** | ||
* Wrap a {@link NettyContext} obtained from a {@link Mono} and offer methods to manage | ||
* its lifecycle in a blocking fashion. | ||
* | ||
* @author Simon Baslé | ||
*/ | ||
public class BlockingNettyContext { | ||
|
||
private static final Logger LOG = Loggers.getLogger(BlockingNettyContext.class); | ||
|
||
private final NettyContext context; | ||
private final String description; | ||
|
||
private Duration lifecycleTimeout; | ||
|
||
public BlockingNettyContext(Mono<? extends NettyContext> contextAsync, | ||
String description) { | ||
this(contextAsync, description, Duration.ofSeconds(3)); | ||
} | ||
|
||
public BlockingNettyContext(Mono<? extends NettyContext> contextAsync, | ||
String description, Duration lifecycleTimeout) { | ||
this.description = description; | ||
this.lifecycleTimeout = lifecycleTimeout; | ||
this.context = contextAsync | ||
.timeout(lifecycleTimeout, Mono.error(new TimeoutException(description + " couldn't be started within " + lifecycleTimeout.toMillis() + "ms"))) | ||
.doOnNext(ctx -> LOG.info("Started {} on {}", description, ctx.address())) | ||
.block(); | ||
|
||
context.onClose().subscribe(null, | ||
e -> LOG.error("Stopped {} on {} with an error {}", description, context.address(), e), | ||
() -> LOG.info("Stopped {} on {}", description, context.address())); | ||
} | ||
|
||
/** | ||
* Change the lifecycle timeout applied to the {@link #shutdown()} operation (as this can | ||
* only be called AFTER the {@link NettyContext} has been "started"). | ||
* | ||
* @param timeout the new timeout to apply on shutdown. | ||
*/ | ||
public void setLifecycleTimeout(Duration timeout) { | ||
this.lifecycleTimeout = timeout; | ||
} | ||
|
||
/** | ||
* Get the {@link NettyContext} wrapped by this facade. | ||
* @return the original NettyContext. | ||
*/ | ||
public NettyContext getContext() { | ||
return context; | ||
} | ||
|
||
/** | ||
* Return this server's port. | ||
* @return The port the server is bound to. | ||
*/ | ||
public int getPort() { | ||
return context.address().getPort(); | ||
} | ||
|
||
/** | ||
* Return the server's host String. That is, the hostname or in case the server was bound | ||
* to a literal IP adress, the IP string representation (rather than performing a reverse-DNS | ||
* lookup). | ||
* | ||
* @return the host string, without reverse DNS lookup | ||
* @see NettyContext#address() | ||
* @see InetSocketAddress#getHostString() | ||
*/ | ||
public String getHost() { | ||
return context.address().getHostString(); | ||
} | ||
|
||
/** | ||
* Shut down the {@link NettyContext} and wait for its termination, up to the | ||
* {@link #setLifecycleTimeout(Duration) lifecycle timeout}. | ||
*/ | ||
public void shutdown() { | ||
if (context.isDisposed()) { | ||
return; | ||
} | ||
context.dispose(); | ||
context.onClose() | ||
.timeout(lifecycleTimeout, Mono.error(new TimeoutException(description + " couldn't be stopped within " + lifecycleTimeout.toMillis() + "ms"))) | ||
.block(); | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/test/java/reactor/ipc/netty/tcp/BlockingNettyContextTest.java
This file contains 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,157 @@ | ||
package reactor.ipc.netty.tcp; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import org.junit.Test; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.ipc.netty.NettyContext; | ||
import reactor.ipc.netty.NettyPipeline; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class BlockingNettyContextTest { | ||
|
||
static final NettyContext NEVER_STOP_CONTEXT = new NettyContext() { | ||
@Override | ||
public Channel channel() { | ||
return new EmbeddedChannel(); | ||
} | ||
|
||
@Override | ||
public InetSocketAddress address() { | ||
return InetSocketAddress.createUnresolved("localhost", 4321); | ||
} | ||
|
||
@Override | ||
public Mono<Void> onClose() { | ||
return Mono.never(); | ||
} | ||
}; | ||
|
||
@Test | ||
public void simpleServerFromAsyncServer() throws InterruptedException { | ||
BlockingNettyContext simpleServer = | ||
TcpServer.create() | ||
.start((in, out) -> out | ||
.options(NettyPipeline.SendOptions::flushOnEach) | ||
.sendString( | ||
in.receive() | ||
.asString() | ||
.takeUntil(s -> s.endsWith("CONTROL")) | ||
.map(s -> "ECHO: " + s.replaceAll("CONTROL", "")) | ||
.concatWith(Mono.just("DONE")) | ||
) | ||
.neverComplete() | ||
); | ||
|
||
System.out.println(simpleServer.getHost()); | ||
System.out.println(simpleServer.getPort()); | ||
|
||
AtomicReference<List<String>> data1 = new AtomicReference<>(); | ||
AtomicReference<List<String>> data2 = new AtomicReference<>(); | ||
|
||
BlockingNettyContext simpleClient1 = | ||
TcpClient.create(simpleServer.getPort()) | ||
.start((in, out) -> out.options(NettyPipeline.SendOptions::flushOnEach) | ||
.sendString(Flux.just("Hello", "World", "CONTROL")) | ||
.then(in.receive() | ||
.asString() | ||
.takeUntil(s -> s.endsWith("DONE")) | ||
.map(s -> s.replaceAll("DONE", "")) | ||
.filter(s -> !s.isEmpty()) | ||
.collectList() | ||
.doOnNext(data1::set) | ||
.doOnNext(System.err::println) | ||
.then())); | ||
|
||
BlockingNettyContext simpleClient2 = | ||
TcpClient.create(simpleServer.getPort()) | ||
.start((in, out) -> out.options(NettyPipeline.SendOptions::flushOnEach) | ||
.sendString(Flux.just("How", "Are", "You?", "CONTROL")) | ||
.then(in.receive() | ||
.asString() | ||
.takeUntil(s -> s.endsWith("DONE")) | ||
.map(s -> s.replaceAll("DONE", "")) | ||
.filter(s -> !s.isEmpty()) | ||
.collectList() | ||
.doOnNext(data2::set) | ||
.doOnNext(System.err::println) | ||
.then())); | ||
|
||
Thread.sleep(100); | ||
System.err.println("STOPPING 1"); | ||
simpleClient1.shutdown(); | ||
|
||
System.err.println("STOPPING 2"); | ||
simpleClient2.shutdown(); | ||
|
||
System.err.println("STOPPING SERVER"); | ||
simpleServer.shutdown(); | ||
|
||
assertThat(data1.get()) | ||
.allSatisfy(s -> assertThat(s).startsWith("ECHO: ")); | ||
assertThat(data2.get()) | ||
.allSatisfy(s -> assertThat(s).startsWith("ECHO: ")); | ||
|
||
assertThat(data1.get() | ||
.toString() | ||
.replaceAll("ECHO: ", "") | ||
.replaceAll(", ", "")) | ||
.isEqualTo("[HelloWorld]"); | ||
assertThat(data2.get() | ||
.toString() | ||
.replaceAll("ECHO: ", "") | ||
.replaceAll(", ", "")) | ||
.isEqualTo("[HowAreYou?]"); | ||
} | ||
|
||
@Test | ||
public void testTimeoutOnStart() { | ||
assertThatExceptionOfType(RuntimeException.class) | ||
.isThrownBy(() -> new BlockingNettyContext(Mono.never(), "TEST NEVER START", Duration.ofMillis(100))) | ||
.withCauseExactlyInstanceOf(TimeoutException.class) | ||
.withMessage("java.util.concurrent.TimeoutException: TEST NEVER START couldn't be started within 100ms"); | ||
} | ||
|
||
@Test | ||
public void testTimeoutOnStop() { | ||
final BlockingNettyContext neverStop = | ||
new BlockingNettyContext(Mono.just(NEVER_STOP_CONTEXT), "TEST NEVER STOP", Duration.ofMillis(100)); | ||
|
||
assertThatExceptionOfType(RuntimeException.class) | ||
.isThrownBy(neverStop::shutdown) | ||
.withCauseExactlyInstanceOf(TimeoutException.class) | ||
.withMessage("java.util.concurrent.TimeoutException: TEST NEVER STOP couldn't be stopped within 100ms"); | ||
} | ||
|
||
@Test | ||
public void testTimeoutOnStopChangedTimeout() { | ||
final BlockingNettyContext neverStop = | ||
new BlockingNettyContext(Mono.just(NEVER_STOP_CONTEXT), "TEST NEVER STOP", Duration.ofMillis(500)); | ||
|
||
neverStop.setLifecycleTimeout(Duration.ofMillis(100)); | ||
|
||
assertThatExceptionOfType(RuntimeException.class) | ||
.isThrownBy(neverStop::shutdown) | ||
.withCauseExactlyInstanceOf(TimeoutException.class) | ||
.withMessage("java.util.concurrent.TimeoutException: TEST NEVER STOP couldn't be stopped within 100ms"); | ||
} | ||
|
||
@Test | ||
public void getContextAddressAndHost() { | ||
BlockingNettyContext | ||
facade = new BlockingNettyContext(Mono.just(NEVER_STOP_CONTEXT), "foo"); | ||
|
||
assertThat(facade.getContext()).isSameAs(NEVER_STOP_CONTEXT); | ||
assertThat(facade.getPort()).isEqualTo(NEVER_STOP_CONTEXT.address().getPort()); | ||
assertThat(facade.getHost()).isEqualTo(NEVER_STOP_CONTEXT.address().getHostString()); | ||
} | ||
} |
Oops, something went wrong.