use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.
the class SingleInstanceGetRoutingTableProcedureTest method shouldThrowIfClientProvidedPortIsNegative.
@ParameterizedTest
@EnumSource(value = RoutingMode.class)
void shouldThrowIfClientProvidedPortIsNegative(RoutingMode routingMode) {
// given
var advertisedBoldPort = 8776;
var clientProvidedPort = -5;
var advertisedBoltAddress = new SocketAddress("neo4j.com", advertisedBoldPort);
var clientProvidedHostPortStr = String.format("%s:%d", "my.neo4j-service.com", clientProvidedPort);
var ctxContents = new MapValueBuilder();
ctxContents.add(ADDRESS_CONTEXT_KEY, Values.stringValue(clientProvidedHostPortStr));
var ctx = ctxContents.build();
var portRegister = mock(ConnectorPortRegister.class);
when(portRegister.getLocalAddress(BoltConnector.NAME)).thenReturn(new HostnamePort("neo4j.com", advertisedBoldPort));
var config = newConfig(Config.defaults(SERVER_DEFAULTS), Duration.ofSeconds(100), advertisedBoltAddress);
config.set(routing_default_router, routingMode);
var databaseManager = databaseManagerMock(config, true);
var logProvider = new AssertableLogProvider();
var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
var expectedMessage = "An address key is included in the query string provided to the GetRoutingTableProcedure, but its value could not be parsed.";
// when
assertThrows(ProcedureException.class, () -> invoke(procedure, ID, ctx), expectedMessage);
}
use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.
the class SingleInstanceGetRoutingTableProcedureTest method shouldUseConfiguredAdvertisedAddressIfClientProvidedPortIsZero.
/**
* This tests a very specific non-documented behaviour - if the client provided address has the port explicitly set to zero then the returned routing table
* uses the advertised address. We make use of this behaviour in some internal tests. In "real life" we should never encounter a client provided address
* specifically set to zero so there's no particular reason to change this.
*/
@RoutingConfigsTest
void shouldUseConfiguredAdvertisedAddressIfClientProvidedPortIsZero(Config config, String clientAddress) throws Exception {
// given
var advertisedBoldPort = 8776;
var clientProvidedPort = 0;
var advertisedBoltAddress = new SocketAddress("neo4j.com", advertisedBoldPort);
config.set(BoltConnector.advertised_address, advertisedBoltAddress);
var clientProvidedHost = clientAddress == null ? "mydomain.com" : clientAddress.split(":")[0];
var clientProvidedHostPortStr = String.format("%s:%d", clientProvidedHost, clientProvidedPort);
var portRegister = mock(ConnectorPortRegister.class);
when(portRegister.getLocalAddress(BoltConnector.NAME)).thenReturn(new HostnamePort("neo4j.com", advertisedBoldPort));
var databaseManager = databaseManagerMock(config, true);
var logProvider = new AssertableLogProvider();
var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
var expectedAddress = advertisedBoltAddress;
// when
RoutingResult result = invoke(procedure, clientProvidedHostPortStr);
// then
assertEquals(singletonList(expectedAddress), result.readEndpoints());
assertEquals(expectedWriters(expectedAddress), result.writeEndpoints());
assertEquals(singletonList(expectedAddress), result.routeEndpoints());
}
use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.
the class SingleInstanceGetRoutingTableProcedureTest method shouldThrowWhenAddressCtxIsPresentButEmpty.
@ParameterizedTest
@EnumSource(value = RoutingMode.class)
void shouldThrowWhenAddressCtxIsPresentButEmpty(RoutingMode routingMode) {
// given
var ctxContents = new MapValueBuilder();
ctxContents.add(ADDRESS_CONTEXT_KEY, Values.EMPTY_STRING);
var ctx = ctxContents.build();
var portRegister = mock(ConnectorPortRegister.class);
var config = newConfig(Config.defaults(SERVER_DEFAULTS), Duration.ofSeconds(100), new SocketAddress("neo4j.com", 7687));
config.set(routing_default_router, routingMode);
var databaseManager = databaseManagerMock(config, true);
var logProvider = new AssertableLogProvider();
var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
var expectedMessage = "An address key is included in the query string provided to the GetRoutingTableProcedure, but its value could not be parsed.";
// when/then
assertThrows(ProcedureException.class, () -> invoke(procedure, ID, ctx), expectedMessage);
}
use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.
the class NeoWebServerPortConflictIT method shouldComplainIfServerPortIsAlreadyTaken.
@Test
public void shouldComplainIfServerPortIsAlreadyTaken() throws IOException {
try (ServerSocket socket = new ServerSocket(0, 0, InetAddress.getLocalHost())) {
SocketAddress contestedAddress = new SocketAddress(socket.getInetAddress().getHostName(), socket.getLocalPort());
AssertableLogProvider logProvider = new AssertableLogProvider();
TestWebContainer testWebContainer = null;
try {
testWebContainer = builder(logProvider).onAddress(contestedAddress).usingDataDir(folder.directory(name.getMethodName()).toAbsolutePath().toString()).build();
fail("Should have reported failure to start");
} catch (Exception e) {
assertThat(e.getMessage()).contains("Error starting Neo4j database server");
} finally {
if (testWebContainer != null) {
testWebContainer.shutdown();
}
}
assertThat(logProvider).forClass(CommunityNeoWebServer.class).forLevel(ERROR).containsMessageWithArguments("Failed to start Neo4j on %s: %s", contestedAddress, format("Address %s is already in use, cannot bind to it.", contestedAddress));
}
}
use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.
the class NeoWebServerPortConflictIT method shouldComplainIfServerHTTPSPortIsAlreadyTaken.
@Test
public void shouldComplainIfServerHTTPSPortIsAlreadyTaken() throws IOException {
try (ServerSocket httpsSocket = new ServerSocket(0, 0, InetAddress.getLocalHost())) {
SocketAddress unContestedAddress = new SocketAddress(httpsSocket.getInetAddress().getHostName(), 0);
SocketAddress httpsAddress = new SocketAddress(httpsSocket.getInetAddress().getHostName(), httpsSocket.getLocalPort());
AssertableLogProvider logProvider = new AssertableLogProvider();
TestWebContainer testWebContainer = null;
try {
testWebContainer = builder(logProvider).onAddress(unContestedAddress).onHttpsAddress(httpsAddress).withHttpsEnabled().usingDataDir(folder.directory(name.getMethodName()).toAbsolutePath().toString()).build();
fail("Should have reported failure to start");
} catch (Exception e) {
assertThat(e.getMessage()).contains("Error starting Neo4j database server");
} finally {
if (testWebContainer != null) {
testWebContainer.shutdown();
}
}
assertThat(logProvider).forClass(CommunityNeoWebServer.class).forLevel(ERROR).containsMessageWithArguments("Failed to start Neo4j on %s: %s", unContestedAddress, format("At least one of the addresses %s or %s is already in use, cannot bind to it.", unContestedAddress, httpsAddress));
}
}
Aggregations