Skip to content
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

End-to-end tests: fix leak threads. #283

Merged
merged 3 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ trait StyxProxySpec extends StyxClientSupplier
styxServer = styxConfig.startServer(new RegistryServiceAdapter(backendsRegistry))
println("Styx http port is: [%d]".format(styxServer.httpPort))
println("Styx https port is: [%d]".format(styxServer.secureHttpPort))
super.beforeAll()
}

override protected def afterAll() = {
println("Styx http port was: [%d]".format(styxServer.httpPort))
println("Styx https port was: [%d]".format(styxServer.secureHttpPort))
styxServer.stopAsync().awaitTerminated()
super.afterAll()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ object StyxServerSupport {

def newAdminServerConfigBuilder(adminHttpConnConfig: HttpConnectorConfig) = {
new AdminServerConfig.Builder()
.setBossThreadsCount(1)
.setWorkerThreadsCount(1)
.setHttpConnector(adminHttpConnConfig)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import com.hotels.styx.support.server.UrlMatchingStrategies._
import org.hamcrest.MatcherAssert._
import org.hamcrest.Matchers.hasItem
import org.scalatest._
import org.scalatest.concurrent.Eventually
import scala.concurrent.duration._

class UnwiseCharactersSpec extends FunSpec with StyxProxySpec {
class UnwiseCharactersSpec extends FunSpec with StyxProxySpec with Eventually {

val recordingBackend = FakeHttpServer.HttpStartupConfig().start()

Expand Down Expand Up @@ -66,7 +68,9 @@ class UnwiseCharactersSpec extends FunSpec with StyxProxySpec {
decodedRequest(req)

recordingBackend.verify(receivedRewrittenUrl("/url/unwise%51%51blah"))
assertThat(logger.log(), hasItem(loggingEvent(WARN, "Value contains unwise chars. you should fix this. raw=/url/unwiseQQblah, escaped=/url/unwise%51%51blah.*")))
eventually(timeout(3.seconds)) {
assertThat(logger.log(), hasItem(loggingEvent(WARN, "Value contains unwise chars. you should fix this. raw=/url/unwiseQQblah, escaped=/url/unwise%51%51blah.*")))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class ProtocolsSpec extends FunSpec

override protected def afterAll(): Unit = {
httpsOriginWithoutCert.stop()
httpsOriginWithCert.stop()
httpServer.stop()
super.afterAll()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import com.hotels.styx.support.configuration.ProxyConfig.proxyServerDefaults


case class ProxyConfig(connectors: Connectors = Connectors(HttpConnectorConfig(), null),
bossThreadCount: Int = proxyServerDefaults.bossThreadsCount(),
workerThreadsCount: Int = proxyServerDefaults.workerThreadsCount(),
bossThreadCount: Int = 1,
workerThreadsCount: Int = 1,
nioAcceptorBacklog: Int = proxyServerDefaults.nioAcceptorBacklog(),
tcpNoDelay: Boolean = proxyServerDefaults.tcpNoDelay(),
nioReuseAddress: Boolean = proxyServerDefaults.nioReuseAddress(),
Expand All @@ -33,7 +33,7 @@ case class ProxyConfig(connectors: Connectors = Connectors(HttpConnectorConfig()
requestTimeoutMillis: Int = proxyServerDefaults.requestTimeoutMillis(),
keepAliveTimeoutMillis: Int = proxyServerDefaults.keepAliveTimeoutMillis(),
maxConnectionsCount: Int = proxyServerDefaults.maxConnectionsCount(),
clientWorkerThreadsCount: Int = proxyServerDefaults.clientWorkerThreadsCount()) {
clientWorkerThreadsCount: Int = 1) {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
Expand All @@ -42,6 +44,9 @@
import static java.util.concurrent.TimeUnit.SECONDS;

public class HttpTestClient {
private static final NioEventLoopGroup eventLoopGroup =
new NioEventLoopGroup(1, new ThreadFactoryBuilder().setNameFormat("Test-Client-%d").build());

private final HostAndPort destination;
private final LinkedBlockingDeque<Object> receivedResponses = new LinkedBlockingDeque<>();
private final Supplier<Bootstrap> bootstrap;
Expand All @@ -51,8 +56,6 @@ public class HttpTestClient {
public HttpTestClient(HostAndPort destination, ChannelInitializer<Channel> initializer) {
this.destination = requireNonNull(destination);

NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1, new ThreadFactoryBuilder().setNameFormat("Test-Client-%d").build());

this.bootstrap = lazy(() -> new Bootstrap()
.group(eventLoopGroup)
.channel(NioSocketChannel.class)
Expand All @@ -73,8 +76,20 @@ public HttpTestClient connect() throws InterruptedException {
return this;
}

public void disconnect() throws InterruptedException {
channelFuture.channel().close().await();
public CompletableFuture<Void> disconnect() {
return toCompletableFuture(channelFuture.channel().close());
}

private CompletableFuture<Void> toCompletableFuture(Future<?> nettyFuture) {
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
nettyFuture.addListener(it -> {
if (it.isSuccess()) {
completableFuture.complete(null);
} else {
completableFuture.completeExceptionally(it.cause());
}
});
return completableFuture;
}

public ChannelFuture channelFuture() {
Expand Down