Search in sources :

Example 11 with HostnamePort

use of org.neo4j.internal.helpers.HostnamePort in project neo4j by neo4j.

the class ConnectionTrackingIT method connectSocketTo.

private TransportConnection connectSocketTo(URI uri) throws IOException {
    SocketConnection connection = new SocketConnection();
    connections.add(connection);
    connection.connect(new HostnamePort(uri.getHost(), uri.getPort()));
    return connection;
}
Also used : SocketConnection(org.neo4j.bolt.testing.client.SocketConnection) HostnamePort(org.neo4j.internal.helpers.HostnamePort)

Example 12 with HostnamePort

use of org.neo4j.internal.helpers.HostnamePort in project neo4j by neo4j.

the class DiscoveryServiceTest method init.

public void init(String description, String baseUri, Consumer<ConnectorPortRegister> portRegistryOverrider, Consumer<Config.Builder> configOverrider, String expectedBoltUri) throws URISyntaxException {
    this.baseUri = new URI(baseUri);
    this.dbUri = new URI("/db");
    this.portRegistryOverrider = portRegistryOverrider;
    this.configOverrider = configOverrider;
    this.expectedDatabaseUri = this.baseUri.resolve(this.dbUri).toString();
    this.expectedBoltUri = expectedBoltUri;
    if (portRegistryOverrider != null) {
        portRegistryOverrider.accept(portRegistry);
    } else {
        when(portRegistry.getLocalAddress("bolt")).thenReturn(new HostnamePort("localhost", 7687));
    }
    DependencyResolver dependencyResolver = mock(DependencyResolver.class);
    when(dependencyResolver.resolveDependency(ConnectorPortRegister.class)).thenReturn(portRegistry);
}
Also used : HostnamePort(org.neo4j.internal.helpers.HostnamePort) URI(java.net.URI) DependencyResolver(org.neo4j.common.DependencyResolver)

Example 13 with HostnamePort

use of org.neo4j.internal.helpers.HostnamePort in project neo4j by neo4j.

the class SingleInstanceGetRoutingTableProcedureTest method shouldUseClientProvidedHostAndAdvertisedPortAsAdvertisedAddress.

@RoutingConfigsTest
void shouldUseClientProvidedHostAndAdvertisedPortAsAdvertisedAddress(Config config, String clientAddress) throws Exception {
    // given
    var advertisedBoldPort = 8776;
    var defaultBoltPort = 7687;
    var advertisedBoltAddress = new SocketAddress("neo4j.com", advertisedBoldPort);
    config.set(BoltConnector.advertised_address, advertisedBoltAddress);
    var clientProvidedHost = clientAddress == null ? "mydomain.com" : clientAddress.split(":")[0];
    var ctxContents = new MapValueBuilder();
    ctxContents.add(ADDRESS_CONTEXT_KEY, Values.stringValue(clientProvidedHost));
    var ctx = ctxContents.build();
    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 = new SocketAddress(clientProvidedHost, defaultBoltPort);
    // when
    var result = invoke(procedure, ID, ctx);
    // then
    assertEquals(singletonList(expectedAddress), result.readEndpoints());
    assertEquals(expectedWriters(expectedAddress), result.writeEndpoints());
    assertEquals(singletonList(expectedAddress), result.routeEndpoints());
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) HostnamePort(org.neo4j.internal.helpers.HostnamePort) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider)

Example 14 with HostnamePort

use of org.neo4j.internal.helpers.HostnamePort 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);
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) HostnamePort(org.neo4j.internal.helpers.HostnamePort) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with HostnamePort

use of org.neo4j.internal.helpers.HostnamePort 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());
}
Also used : HostnamePort(org.neo4j.internal.helpers.HostnamePort) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider)

Aggregations

HostnamePort (org.neo4j.internal.helpers.HostnamePort)18 Test (org.junit.jupiter.api.Test)7 SocketAddress (org.neo4j.configuration.helpers.SocketAddress)5 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)5 SocketConnection (org.neo4j.bolt.testing.client.SocketConnection)4 InputStream (java.io.InputStream)3 Socket (java.net.Socket)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MapValueBuilder (org.neo4j.values.virtual.MapValueBuilder)3 URI (java.net.URI)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 DynamicTest (org.junit.jupiter.api.DynamicTest)1 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)1 DependencyResolver (org.neo4j.common.DependencyResolver)1