Skip to content

Commit

Permalink
feat: fix BasicStandardEndpointMapperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 8, 2024
1 parent 524d44a commit 7eb449b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public long getLastUnreachableTs() {
return lastUnreachableTs;
}

void reportAsUnreachable(final long timestamp, final String cause) {
public void reportAsUnreachable(final long timestamp, final String cause) {
LOG.info("{}: reportAsUnreachable {} cause {}", hostPort, new Timestamp(timestamp), cause);
this.lastUnreachableTs = timestamp;
this.status = Status.DOWN;
this.connections.set(0);
}

void reportAsReachable(final long timestamp) {
public void reportAsReachable(final long timestamp) {
this.lastReachableTs = timestamp;
if (this.lastReachableTs - this.lastUnreachableTs >= WARMUP_MILLIS) {
this.status = Status.STABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
public class BasicStandardEndpointMapperTest {

@Rule
public WireMockRule backend1 = new WireMockRule(0);
public WireMockRule backend = new WireMockRule(0);

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
Expand All @@ -92,7 +92,7 @@ public void test() throws Exception {
.withHeader("Content-Type", "text/html")
.withBody("it <b>works</b> !!")));

int backendPort = backend1.port();
int backendPort = backend.port();
StandardEndpointMapper mapper = new StandardEndpointMapper();

mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/"));
Expand Down Expand Up @@ -182,7 +182,7 @@ public void testRouteErrors() throws Exception {

configuration.put("backend.1.id", "backend");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend1.port()));
configuration.put("backend.1.port", String.valueOf(backend.port()));
configuration.put("backend.1.enabled", "true");

configuration.put("director.1.id", "director");
Expand All @@ -192,7 +192,7 @@ public void testRouteErrors() throws Exception {
// unreachable backend -> expected service unavailable
configuration.put("backend.2.id", "backend-down");
configuration.put("backend.2.host", "localhost-down");
configuration.put("backend.2.port", String.valueOf(backend1.port()));
configuration.put("backend.2.port", String.valueOf(backend.port()));
configuration.put("backend.2.enabled", "true");

configuration.put("director.2.id", "director-down");
Expand Down Expand Up @@ -256,12 +256,14 @@ public void testRouteErrors() throws Exception {
PropertiesConfigurationStore config = new PropertiesConfigurationStore(configuration);

BackendHealthManager bhMan = mock(BackendHealthManager.class);
final EndpointKey alive = EndpointKey.make("localhost:" + backend1.port());
final EndpointKey hostPort1 = eq(alive);
when(bhMan.getBackendStatus(hostPort1).getStatus()).thenReturn(BackendHealthStatus.Status.STABLE);
final EndpointKey down = EndpointKey.make("localhost-down:" + backend1.port());
final EndpointKey hostPort = eq(down);
when(bhMan.getBackendStatus(hostPort).getStatus()).thenReturn(BackendHealthStatus.Status.DOWN); // simulate unreachable backend -> expected 500 error
final EndpointKey alive = EndpointKey.make("localhost:" + backend.port());
final BackendHealthStatus mockAliveStatus = mock(BackendHealthStatus.class);
when(mockAliveStatus.getStatus()).thenReturn(BackendHealthStatus.Status.STABLE);
when(bhMan.getBackendStatus(eq(alive))).thenReturn(mockAliveStatus);
final EndpointKey down = EndpointKey.make("localhost-down:" + backend.port());
final BackendHealthStatus mockDownStatus = mock(BackendHealthStatus.class);
when(mockDownStatus.getStatus()).thenReturn(BackendHealthStatus.Status.DOWN);
when(bhMan.getBackendStatus(eq(down))).thenReturn(mockDownStatus); // simulate unreachable backend -> expected 500 error
server.setBackendHealthManager(bhMan);
server.configureAtBoot(config);
server.start();
Expand Down Expand Up @@ -312,7 +314,7 @@ public void testDefaultRoute() throws Exception {
.withHeader("Content-Type", "text/html")
.withBody("it <b>works</b> !!")));

int backendPort = backend1.port();
int backendPort = backend.port();

StandardEndpointMapper mapper = new StandardEndpointMapper();
mapper.addBackend(new BackendConfiguration("backend", "localhost", backendPort, "/"));
Expand All @@ -327,11 +329,13 @@ public void testDefaultRoute() throws Exception {

BackendHealthManager bhMan = mock(BackendHealthManager.class);
final EndpointKey alive = EndpointKey.make("localhost:" + backendPort);
final EndpointKey hostPort1 = eq(alive);
when(bhMan.getBackendStatus(hostPort1).getStatus()).thenReturn(BackendHealthStatus.Status.STABLE);
final BackendHealthStatus mockAliveStatus = mock(BackendHealthStatus.class);
when(mockAliveStatus.getStatus()).thenReturn(BackendHealthStatus.Status.STABLE);
when(bhMan.getBackendStatus(eq(alive))).thenReturn(mockAliveStatus);
final EndpointKey down = EndpointKey.make("localhost-down:" + backendPort);
final EndpointKey hostPort = eq(down);
when(bhMan.getBackendStatus(hostPort).getStatus()).thenReturn(BackendHealthStatus.Status.DOWN); // simulate unreachable backend -> expected 500 error
final BackendHealthStatus mockDownStatus = mock(BackendHealthStatus.class);
when(mockDownStatus.getStatus()).thenReturn(BackendHealthStatus.Status.DOWN);
when(bhMan.getBackendStatus(eq(down))).thenReturn(mockDownStatus); // simulate unreachable backend -> expected 500 error

try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.setBackendHealthManager(bhMan);
Expand Down Expand Up @@ -371,7 +375,7 @@ public void testAlwaysServeStaticContent() throws Exception {
Properties configuration = new Properties();
configuration.put("backend.1.id", "foo");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend1.port()));
configuration.put("backend.1.port", String.valueOf(backend.port()));
configuration.put("backend.1.enabled", "true");

configuration.put("director.1.id", "*");
Expand Down Expand Up @@ -448,7 +452,7 @@ public void testServeACMEChallengeToken() throws Exception {
Properties configuration = new Properties();
configuration.put("backend.1.id", "foo");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend1.port()));
configuration.put("backend.1.port", String.valueOf(backend.port()));
configuration.put("backend.1.enabled", "true");

configuration.put("director.1.id", "*");
Expand Down Expand Up @@ -522,11 +526,11 @@ public void testCustomAndDebuggingHeaders() throws Exception {
Properties configuration = new Properties();
configuration.put("backend.1.id", "b1");
configuration.put("backend.1.host", "localhost");
configuration.put("backend.1.port", String.valueOf(backend1.port()));
configuration.put("backend.1.port", String.valueOf(backend.port()));
configuration.put("backend.1.enabled", "true");
configuration.put("backend.2.id", "b2");
configuration.put("backend.2.host", "localhost");
configuration.put("backend.2.port", String.valueOf(backend1.port()));
configuration.put("backend.2.port", String.valueOf(backend.port()));
configuration.put("backend.2.enabled", "true");

configuration.put("director.1.id", "d1");
Expand Down

0 comments on commit 7eb449b

Please sign in to comment.