Skip to content

Commit

Permalink
test: drop mapper injection again
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 15, 2024
1 parent c8925be commit 33b5ccd
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ public int getLocalPort() {
return listeners.getLocalPort();
}

@VisibleForTesting
public void setMapper(final EndpointMapper mapper) {
this.mapper = mapper;
}

public void startAdminInterface() throws Exception {
if (!adminServerEnabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public BackendHealthManager(final RuntimeServerConfiguration conf, final Endpoin
this.period = DEFAULT_PERIOD;
}

public boolean isTolerant() {
return tolerant;
}

public int getPeriod() {
return period;
}
Expand Down Expand Up @@ -217,7 +221,7 @@ public boolean exceedsCapacity(final String backendId) {
return false;
}
final BackendHealthStatus backendStatus = getBackendStatus(backendConfiguration.hostPort());
return backendConfiguration.safeCapacity() > backendStatus.getConnections() && !this.tolerant;
return backendConfiguration.safeCapacity() > backendStatus.getConnections();
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,23 @@ public MapResult map(final ProxyRequest request) {
final BackendHealthStatus backendStatus = backendHealthManager.getBackendStatus(backend.hostPort());
switch (backendStatus.getStatus()) {
case DOWN:
LOG.info("Backend {} is down, skipping...", backendId);
continue;
case COLD:
final int capacity = backend.safeCapacity();
if (backendHealthManager.exceedsCapacity(backendId)) {
final int capacity = backend.safeCapacity();
if (!backendHealthManager.isTolerant()) {
// default behavior, exceeding safe capacity is not tolerated...
LOG.info("Backend {} is cold and exceeds safe capacity of {} connections, skipping...", backendId, capacity);
continue;
}
/*
* backends are returned by the mapper sorted
* from the most desirable to the less desirable;
* if the execution reaches this point,
* we should use a cold backend even if over the recommended capacity anyway...
* we may use the cold backend even if over the recommended capacity anyway...
*/
LOG.warn("Backend {} exceeds cold capacity of {}, but will use it anyway", backendId, capacity);
LOG.warn("Cold backend {} exceeds safe capacity of {} connections, but will use it anyway", backendId, capacity);
}
// falls through
case STABLE: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.carapaceproxy;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.carapaceproxy.api.UseAdminServer;
import org.carapaceproxy.utils.TestUtils;
import org.junit.Rule;
import org.junit.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;
import java.util.Properties;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.carapaceproxy.api.UseAdminServer;
import org.carapaceproxy.utils.TestUtils;
import org.junit.Rule;
import org.junit.Test;

public class MaintenanceModeTest extends UseAdminServer {

Expand All @@ -35,6 +36,7 @@ public void test() throws Exception {
.withBody("it <b>works</b> !!")));

config = new Properties(HTTP_ADMIN_SERVER_CONFIG);
config.put("healthmanager.tolerant", "true");
startServer(config);

// Default certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class ConnectionPoolTest extends UseAdminServer {
public WireMockRule wireMockRule = new WireMockRule(0);

private void configureAndStartServer() throws Exception {

HttpTestUtils.overrideJvmWideHttpsVerifier();

stubFor(get(urlEqualTo("/index.html"))
Expand All @@ -85,6 +84,7 @@ private void configureAndStartServer() throws Exception {
config.put("db.server.base.dir", tmpDir.newFolder().getAbsolutePath());
config.put("aws.accesskey", "accesskey");
config.put("aws.secretkey", "secretkey");
config.put("healthmanager.tolerant", "true");
startServer(config);

// Default certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import org.carapaceproxy.server.config.NetworkListenerConfiguration;
import org.carapaceproxy.server.config.RouteConfiguration;
import org.carapaceproxy.server.config.SafeBackendSelector;
import org.carapaceproxy.server.mapper.EndpointMapper;
import org.carapaceproxy.server.mapper.StandardEndpointMapper;
import org.carapaceproxy.server.mapper.requestmatcher.RegexpRequestMatcher;
import org.carapaceproxy.utils.EndpointMapperHolder;
import org.carapaceproxy.utils.RawHttpClient;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -64,6 +64,7 @@ public class StuckRequestsTest {

@Test
@Parameters({"true", "false"})
@junitparams.naming.TestCaseName("test(backend unreachable on stuck request: {0})")
public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnStuckRequests) throws Exception {

stubFor(get(urlEqualTo("/index.html"))
Expand All @@ -85,22 +86,39 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt
final int theport = wireMockRule.port();
EndpointKey key = new EndpointKey("localhost", theport);

final EndpointMapperHolder mapperFactory = new EndpointMapperHolder(parent -> {
final EndpointMapper.Factory mapperFactory = parent -> {
StandardEndpointMapper mapper = new StandardEndpointMapper(parent, SafeBackendSelector::new);
mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/", -1));
mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a"));
mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1));
mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*")));
return mapper;
});
try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder());) {
};
try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder())) {
Properties properties = new Properties();
properties.put("healthmanager.tolerant", "true");
properties.put("backend.1.id", "backend-a");
properties.put("backend.1.enabled", "true");
properties.put("backend.1.host", "localhost");
properties.put("backend.1.port", theport);
properties.put("backend.1.probePath", "/");
properties.put("director.1.id", "director-1");
properties.put("director.1.backends", properties.getProperty("backend.1.id"));
properties.put("director.1.enabled", "true");
properties.put("action.1.id", "proxy-1");
properties.put("action.1.enabled", "true");
properties.put("action.1.type", ActionConfiguration.TYPE_PROXY);
properties.put("action.1.director", properties.getProperty("director.1.id"));
properties.put("route.100.id", "route-1");
properties.put("route.100.enabled", "true");
properties.put("route.100.match", "request.uri ~ \".*index.html.*\"");
properties.put("route.100.action", properties.getProperty("action.1.id"));

properties.put("connectionsmanager.stuckrequesttimeout", "100"); // ms
properties.put("connectionsmanager.backendsunreachableonstuckrequests", backendsUnreachableOnStuckRequests + "");
// configure resets all listeners configurations
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapperFactory.getEndpointMapper());
assertEquals(100, server.getCurrentConfiguration().getStuckRequestTimeout());
assertEquals(backendsUnreachableOnStuckRequests, server.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests());
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public UnreachableBackendTest(boolean useCache) {

@Test
public void testWithUnreachableBackend() throws Exception {

stubFor(get(urlEqualTo("/index.html"))
.willReturn(aResponse()
.withStatus(200)
Expand Down Expand Up @@ -113,12 +114,12 @@ public void testEmptyResponse() throws Exception {
TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
EndpointKey key = new EndpointKey("localhost", dummyport);

try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
Properties properties = new Properties();
properties.put("healthmanager.tolerant", "true");
// configure resets all listeners configurations
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapper);
server.start();
int port = server.getLocalPort();
assertTrue(port > 0);
Expand Down Expand Up @@ -182,11 +183,11 @@ public void testNonHttpResponseThenClose() throws Exception {
TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
EndpointKey key = new EndpointKey("localhost", dummyport);

try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
Properties properties = new Properties();
properties.put("healthmanager.tolerant", "true");
server.configureAtBoot(new PropertiesConfigurationStore(properties));
server.addListener(new NetworkListenerConfiguration("localhost", 0));
server.setMapper(mapper);
server.start();
int port = server.getLocalPort();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.carapaceproxy.core;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertEquals;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.carapaceproxy.api.UseAdminServer;
import org.carapaceproxy.utils.TestUtils;
import org.junit.Rule;
import org.junit.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Properties;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;
import org.carapaceproxy.api.UseAdminServer;
import org.carapaceproxy.utils.TestUtils;
import org.junit.Rule;
import org.junit.Test;

public class MaxHeaderSizeTest extends UseAdminServer {

Expand All @@ -33,6 +34,7 @@ public void test() throws Exception {
.withBody("it <b>works</b> !!")));

config = new Properties(HTTP_ADMIN_SERVER_CONFIG);
config.put("healthmanager.tolerant", "true");
startServer(config);

// Default certificate
Expand Down Expand Up @@ -71,7 +73,6 @@ public void test() throws Exception {

changeDynamicConfiguration(config);


HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private void configureAndStartServer() throws Exception {
config.put("db.server.base.dir", tmpDir.newFolder().getAbsolutePath());
config.put("aws.accesskey", "accesskey");
config.put("aws.secretkey", "secretkey");
config.put("healthmanager.tolerant", "true");
startServer(config);

// Default certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ public void testAlwaysServeStaticContent() throws Exception {

{
Properties configuration = new Properties();
configuration.put("healthmanager.tolerant", "true");
configuration.put("backend.1.id", "foo");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend.port()));
Expand Down Expand Up @@ -531,6 +532,7 @@ public void testCustomAndDebuggingHeaders() throws Exception {

try (HttpProxyServer server = new HttpProxyServer(StandardEndpointMapper::new, tmpDir.newFolder())) {
Properties configuration = new Properties();
configuration.put("healthmanager.tolerant", "true");
configuration.put("backend.1.id", "b1");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend.port()));
Expand Down

This file was deleted.

0 comments on commit 33b5ccd

Please sign in to comment.