Skip to content

Commit

Permalink
Merge pull request #27 from facebookincubator/hunter/loopback_not_loc…
Browse files Browse the repository at this point in the history
…alhost

use loopback in tests instead of localhost
  • Loading branch information
hunterjackson committed Mar 26, 2024
2 parents a2ab57f + 5cb52fb commit 5e7b8fe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
Expand Up @@ -95,7 +95,7 @@ void setUp() throws UnknownHostException, URISyntaxException {
app.post(PATH, ctx -> ctx.result(MAPPER.writeValueAsString(SAMPLE_RESPONSE)));
app.start(0);
endpoint =
URIBuilder.localhost().setScheme("http").appendPath(PATH).setPort(app.port()).build();
URIBuilder.loopbackAddress().setScheme("http").appendPath(PATH).setPort(app.port()).build();
}

@Test
Expand Down Expand Up @@ -286,7 +286,11 @@ void inPipeline() throws IOException, URISyntaxException, InterruptedException {
BlockingQueue<OutboundRequest> metaRequests = new LinkedBlockingDeque<>();
String metaPath = "/meta";
URI messageReceiver =
URIBuilder.localhost().appendPath(metaPath).setScheme("http").setPort(app.port()).build();
URIBuilder.loopbackAddress()
.appendPath(metaPath)
.setScheme("http")
.setPort(app.port())
.build();
app.post(
metaPath,
ctx ->
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/meta/cp4m/llm/OpenAIPluginTest.java
Expand Up @@ -101,7 +101,7 @@ void setUp() throws UnknownHostException, URISyntaxException {
app.post(PATH, ctx -> ctx.result(MAPPER.writeValueAsString(SAMPLE_RESPONSE)));
app.start(0);
endpoint =
URIBuilder.localhost().setScheme("http").appendPath(PATH).setPort(app.port()).build();
URIBuilder.loopbackAddress().setScheme("http").appendPath(PATH).setPort(app.port()).build();
}

@ParameterizedTest
Expand Down Expand Up @@ -230,7 +230,11 @@ void inPipeline() throws IOException, URISyntaxException, InterruptedException {
BlockingQueue<OutboundRequest> metaRequests = new LinkedBlockingDeque<>();
String metaPath = "/meta";
URI messageReceiver =
URIBuilder.localhost().appendPath(metaPath).setScheme("http").setPort(app.port()).build();
URIBuilder.loopbackAddress()
.appendPath(metaPath)
.setScheme("http")
.setPort(app.port())
.build();
app.post(
metaPath,
ctx ->
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/meta/cp4m/message/FBMessageHandlerTest.java
Expand Up @@ -30,7 +30,6 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
Expand Down Expand Up @@ -97,7 +96,11 @@ private static Request createMessageRequest(
FBMessageHandler messageHandler = (FBMessageHandler) service.messageHandler();

URI uri =
URIBuilder.localhost().setScheme("http").setPort(runner.port()).appendPath(path).build();
URIBuilder.loopbackAddress()
.setScheme("http")
.setPort(runner.port())
.appendPath(path)
.build();

Request request =
Request.post(uri)
Expand Down Expand Up @@ -306,8 +309,8 @@ private Function<Identifier, URI> testURLFactoryFactory(Identifier pageId) {
return p -> {
Preconditions.checkArgument(p.equals(pageId));
try {
return URIBuilder.localhost().setScheme("http").setPort(app.port()).build();
} catch (URISyntaxException | UnknownHostException e) {
return URIBuilder.loopbackAddress().setScheme("http").setPort(app.port()).build();
} catch (URISyntaxException e) {
fail("failed building url");
throw new RuntimeException(e);
}
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/com/meta/cp4m/message/HandlerTestUtils.java
Expand Up @@ -14,7 +14,6 @@
import com.meta.cp4m.Identifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.function.Function;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
Expand All @@ -31,20 +30,23 @@ private HandlerTestUtils() {}
public static Function<Identifier, URI> baseURLFactory(String path, int port) {
return identifier -> {
try {
return URIBuilder.localhost().setPort(port).appendPath(path).setScheme("http").build();
} catch (UnknownHostException | URISyntaxException e) {
return URIBuilder.loopbackAddress()
.setPort(port)
.appendPath(path)
.setScheme("http")
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
};
}

public static Function<JsonNode, Request> MessageRequestFactory(
Method method, String path, String appSecret, int port)
throws UnknownHostException, URISyntaxException {
Method method, String path, String appSecret, int port) throws URISyntaxException {
Request request =
Request.create(
method,
URIBuilder.localhost().setScheme("http").appendPath(path).setPort(port).build());
URIBuilder.loopbackAddress().setScheme("http").appendPath(path).setPort(port).build());
return jn -> {
try {
String body = MAPPER.writeValueAsString(jn);
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/com/meta/cp4m/message/ServiceTestHarness.java
Expand Up @@ -18,7 +18,6 @@
import com.meta.cp4m.store.MemoryStoreConfig;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.net.URIBuilder;
Expand Down Expand Up @@ -75,25 +74,25 @@ public Request post(String body) {

public URI webserverURI() {
try {
return URIBuilder.localhost()
return URIBuilder.loopbackAddress()
.appendPath(WEBSERVER_PATH)
.setScheme("http")
.setPort(webserverPort())
.build();
} catch (URISyntaxException | UnknownHostException e) {
} catch (URISyntaxException e) {
// this should be impossible
throw new RuntimeException(e);
}
}

public URI serviceURI() {
try {
return URIBuilder.localhost()
return URIBuilder.loopbackAddress()
.appendPath(SERVICE_PATH)
.setScheme("http")
.setPort(servicePort())
.build();
} catch (URISyntaxException | UnknownHostException e) {
} catch (URISyntaxException e) {
// this should be impossible
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 5e7b8fe

Please sign in to comment.