/** * Textual representation format for a listen socket address. * @param hostname of the address. * @param port of the address. * @return a string representing the address. */ public static String listenAddress( String hostname, int port ) { return new ListenSocketAddress( hostname, port ).toString(); } }
private static SocketTransport newSocketTransport( NetworkConnectionTracker connectionTracker, TransportThrottleGroup throttleGroup ) { return new SocketTransport( "bolt", new ListenSocketAddress( "localhost", 7687 ), null, false, NullLogProvider.getInstance(), throttleGroup, mock( BoltProtocolFactory.class ), connectionTracker ); } }
@Test void shouldSupportMultipleBoltConnectorsWithCustomNames() { Config config = Config.defaults( stringMap( "dbms.connector.bolt1.type", "BOLT", "dbms.connector.bolt1.enabled", "true", "dbms.connector.bolt1.listen_address", ":8000", "dbms.connector.bolt2.type", "BOLT", "dbms.connector.bolt2.enabled", "true", "dbms.connector.bolt2.listen_address", ":9000" ) ); // when List<ListenSocketAddress> addresses = config.boltConnectors().stream() .map( c -> config.get( c.listen_address ) ) .collect( Collectors.toList() ); // then assertEquals( 2, addresses.size() ); if ( addresses.get( 0 ).getPort() == 8000 ) { assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), addresses.get( 0 ) ); assertEquals( new ListenSocketAddress( "127.0.0.1", 9000 ), addresses.get( 1 ) ); } else { assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), addresses.get( 1 ) ); assertEquals( new ListenSocketAddress( "127.0.0.1", 9000 ), addresses.get( 0 ) ); } }
@Test void shouldSupportMultipleBoltConnectorsWithDefaultAndCustomName() { Config config = Config.defaults( stringMap( "dbms.connector.bolt.type", "BOLT", "dbms.connector.bolt.enabled", "true", "dbms.connector.bolt.listen_address", ":8000", "dbms.connector.bolt2.type", "BOLT", "dbms.connector.bolt2.enabled", "true", "dbms.connector.bolt2.listen_address", ":9000" ) ); // when BoltConnector boltConnector1 = config.boltConnectors().get( 0 ); BoltConnector boltConnector2 = config.boltConnectors().get( 1 ); // then assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( boltConnector1.listen_address ) ); assertEquals( new ListenSocketAddress( "127.0.0.1", 9000 ), config.get( boltConnector2.listen_address ) ); }
@Test void testServerDefaultSettings() { // given Config config = Config.builder().withServerDefaults().build(); // when List<HttpConnector> connectors = config.httpConnectors(); // then assertEquals( 2, connectors.size() ); if ( connectors.get( 0 ).encryptionLevel().equals( TLS ) ) { assertEquals( new ListenSocketAddress( "localhost", 7474 ), config.get( connectors.get( 1 ).listen_address ) ); assertEquals( new ListenSocketAddress( "localhost", 7473 ), config.get( connectors.get( 0 ).listen_address ) ); } else { assertEquals( new ListenSocketAddress( "127.0.0.1", 7474 ), config.get( connectors.get( 0 ).listen_address ) ); assertEquals( new ListenSocketAddress( "127.0.0.1", 7473 ), config.get( connectors.get( 1 ).listen_address ) ); } }
@Test void shouldDeriveBoltListenAddressFromDefaultListenAddress() { // given Config config = Config.defaults( stringMap( "dbms.connector.bolt.enabled", "true", "dbms.connectors.default_listen_address", "0.0.0.0" ) ); BoltConnector boltConnector = config.boltConnectors().get( 0 ); // then assertEquals( new ListenSocketAddress( "0.0.0.0", 7687 ), config.get( boltConnector.listen_address ) ); }
@Test void shouldDeriveBoltListenAddressFromDefaultListenAddressAndSpecifiedPort() { // given Config config = Config.defaults( stringMap( "dbms.connectors.default_listen_address", "0.0.0.0", "dbms.connector.bolt.enabled", "true", "dbms.connector.bolt.listen_address", ":8000" ) ); BoltConnector boltConnector = config.boltConnectors().get( 0 ); // then assertEquals( new ListenSocketAddress( "0.0.0.0", 8000 ), config.get( boltConnector.listen_address ) ); }
@Test void shouldBeAbleToOverrideBoltListenAddressesWithJustOneParameter() { // given Config config = Config.defaults( stringMap( "dbms.connector.bolt.enabled", "true", "dbms.connector.bolt.listen_address", ":8000" ) ); BoltConnector boltConnector = config.boltConnectors().get( 0 ); // then assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( boltConnector.listen_address ) ); }
@Test void shouldStillSupportCustomNameForBoltConnector() { Config config = Config.defaults( stringMap( "dbms.connector.random_name_that_will_be_unsupported.type", "BOLT", "dbms.connector.random_name_that_will_be_unsupported.enabled", "true", "dbms.connector.random_name_that_will_be_unsupported.listen_address", ":8000" ) ); // when BoltConnector boltConnector = config.boltConnectors().get( 0 ); // then assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( boltConnector.listen_address ) ); }
@Test void shouldBeAbleToOverrideHttpListenAddressWithJustOneParameter() { // given Config config = Config.defaults( stringMap( "dbms.connector.http.enabled", "true", "dbms.connector.http.listen_address", ":8000" ) ); // then assertEquals( 1, config.enabledHttpConnectors().size() ); HttpConnector httpConnector = config.enabledHttpConnectors().get( 0 ); assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( httpConnector.listen_address ) ); }
@Test void shouldBeAbleToOverrideHttpsListenAddressWithJustOneParameter() { // given Config config = Config.defaults( stringMap( "dbms.connector.https.enabled", "true", "dbms.connector.https.listen_address", ":8000" ) ); // then assertEquals( 1, config.enabledHttpConnectors().size() ); HttpConnector httpConnector = config.enabledHttpConnectors().get( 0 ); assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( httpConnector.listen_address ) ); }
@Test void shouldEnableBoltByDefault() { // given Config config = Config.builder().withServerDefaults().build(); // when BoltConnector boltConnector = config.boltConnectors().get( 0 ); ListenSocketAddress listenSocketAddress = config.get( boltConnector.listen_address ); // then assertEquals( new ListenSocketAddress( "127.0.0.1", 7687 ), listenSocketAddress ); }
@Test void shouldStillSupportCustomNameForHttpConnector() { Config config = Config.defaults( stringMap( "dbms.connector.random_name_that_will_be_unsupported.type", "HTTP", "dbms.connector.random_name_that_will_be_unsupported.encryption", "NONE", "dbms.connector.random_name_that_will_be_unsupported.enabled", "true", "dbms.connector.random_name_that_will_be_unsupported.listen_address", ":8000" ) ); // then assertEquals( 1, config.enabledHttpConnectors().size() ); assertEquals( new ListenSocketAddress( "127.0.0.1", 8000 ), config.get( config.enabledHttpConnectors().get( 0 ).listen_address ) ); }
@Test void shouldStillSupportCustomNameForHttpsConnector() { Config config = Config.defaults( stringMap( "dbms.connector.random_name_that_will_be_unsupported.type", "HTTP", "dbms.connector.random_name_that_will_be_unsupported.encryption", "TLS", "dbms.connector.random_name_that_will_be_unsupported.enabled", "true", "dbms.connector.random_name_that_will_be_unsupported.listen_address", ":9000" ) ); // then assertEquals( 1, config.enabledHttpConnectors().size() ); assertEquals( new ListenSocketAddress( "127.0.0.1", 9000 ), config.get( config.enabledHttpConnectors().get( 0 ).listen_address ) ); }
@Test void shouldDeriveListenAddressFromDefaultListenAddressAndSpecifiedPorts() { // given Config config = Config.defaults( stringMap( "dbms.connector.https.enabled", "true", "dbms.connector.http.enabled", "true", "dbms.connectors.default_listen_address", "0.0.0.0", "dbms.connector.http.listen_address", ":8000", "dbms.connector.https.listen_address", ":9000" ) ); // then assertEquals( 2, config.enabledHttpConnectors().size() ); config.enabledHttpConnectors().forEach( c -> { if ( c.key().equals( "https" ) ) { assertEquals( new ListenSocketAddress( "0.0.0.0", 9000 ), config.get( c.listen_address ) ); } else { assertEquals( new ListenSocketAddress( "0.0.0.0", 8000 ), config.get( c.listen_address ) ); } } ); }
@Test public void shouldBeAbleToUsePortZero() throws Exception { // Given webServer = new Jetty9WebServer( NullLogProvider.getInstance(), Config.defaults(), NetworkConnectionTracker.NO_OP ); webServer.setHttpAddress( new ListenSocketAddress( "localhost", 0 ) ); // When webServer.start(); // Then no exception }
@Test public void shouldBeAbleToRestart() throws Throwable { // given webServer = new Jetty9WebServer( NullLogProvider.getInstance(), Config.defaults(), NetworkConnectionTracker.NO_OP ); webServer.setHttpAddress( new ListenSocketAddress( "127.0.0.1", 7878 ) ); // when webServer.start(); webServer.stop(); webServer.start(); // then no exception }
@Test public void shouldGivePortConflictErrorWithPortNumberInIt() throws Throwable { // Given an occupied port int port = 16000; try ( ServerSocketChannel ignore = ServerSocketChannel.open().bind( new InetSocketAddress( "localhost", port ) ) ) { final ListenSocketAddress address = new ListenSocketAddress( "localhost", port ); // Expect exception.expect( PortBindException.class ); // When Map<BoltConnector,NettyServer.ProtocolInitializer> initializersMap = genericMap( new BoltConnector( "test" ), protocolOnAddress( address ) ); new NettyServer( new NamedThreadFactory( "mythreads" ), initializersMap, new ConnectorPortRegister(), NullLog.getInstance() ).start(); } }
@Test public void shouldHaveConfigurableJettyThreadPoolSize() throws Exception { Jetty9WebServer server = new Jetty9WebServer( NullLogProvider.getInstance(), Config.defaults(), NetworkConnectionTracker.NO_OP ); int numCores = 1; int configuredMaxThreads = 12; // 12 is the new min max Threads value, for one core int acceptorThreads = 1; // In this configuration, 1 thread will become an acceptor... int selectorThreads = 1; // ... and 1 thread will become a selector... int jobThreads = configuredMaxThreads - acceptorThreads - selectorThreads; // ... and the rest are job threads server.setMaxThreads( numCores ); server.setHttpAddress( new ListenSocketAddress( "localhost", 0 ) ); try { server.start(); QueuedThreadPool threadPool = (QueuedThreadPool) server.getJetty().getThreadPool(); threadPool.start(); CountDownLatch startLatch = new CountDownLatch( jobThreads ); CountDownLatch endLatch = loadThreadPool( threadPool, configuredMaxThreads + 1, startLatch ); startLatch.await(); // Wait for threadPool to create threads int threads = threadPool.getThreads(); assertEquals( "Wrong number of threads in pool", configuredMaxThreads, threads ); endLatch.countDown(); } finally { server.stop(); } }
@Test public void shouldPickUpAddressFromConfig() throws Exception { ListenSocketAddress nonDefaultAddress = new ListenSocketAddress( "0.0.0.0", 0 ); server = server().onAddress( nonDefaultAddress ) .usingDataDir( folder.directory( name.getMethodName() ).getAbsolutePath() ) .build(); server.start(); HostnamePort localHttpAddress = getLocalHttpAddress(); assertNotEquals( HttpConnector.Encryption.NONE.defaultPort, localHttpAddress.getPort() ); assertEquals( nonDefaultAddress.getHostname(), localHttpAddress.getHost() ); JaxRsResponse response = new RestRequest( server.baseUri() ).get(); assertThat( response.getStatus(), is( 200 ) ); response.close(); }