Skip to content

Commit

Permalink
use appropriate connector instance based on protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar10 committed Mar 13, 2024
1 parent 7c3bddc commit 32df8a6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
11 changes: 8 additions & 3 deletions gateway-ha/src/main/java/io/trino/gateway/baseapp/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.dropwizard.core.server.SimpleServerFactory;
import io.dropwizard.jetty.ConnectorFactory;
import io.dropwizard.jetty.HttpConnectorFactory;
import io.dropwizard.jetty.HttpsConnectorFactory;

import java.util.stream.Stream;

Expand All @@ -43,9 +44,13 @@ protected int getApplicationPort()
.map(SimpleServerFactory::getConnector);

return connectors
.filter(connector -> connector.getClass().isAssignableFrom(HttpConnectorFactory.class))
.map(connector -> (HttpConnectorFactory) connector)
.mapToInt(HttpConnectorFactory::getPort)
.filter(connector -> connector instanceof HttpConnectorFactory)
.mapToInt(connector -> {
if (connector instanceof HttpsConnectorFactory httpsConnectorFactory) {
return httpsConnectorFactory.getPort();
}
return ((HttpConnectorFactory) connector).getPort();
})
.findFirst()
.orElseThrow(IllegalStateException::new);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.gateway.baseapp;

import io.dropwizard.core.server.DefaultServerFactory;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.jetty.HttpConnectorFactory;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;

public class TestAppModule
{
@Test
public void testGetApplicationPort()
{
AppConfiguration mockConfig = Mockito.mock(AppConfiguration.class);
DefaultServerFactory mockServerFactory = Mockito.mock(DefaultServerFactory.class);
HttpConnectorFactory mockConnector = Mockito.mock(HttpConnectorFactory.class);

when(mockConfig.getServerFactory()).thenReturn(mockServerFactory);
when(mockServerFactory.getApplicationConnectors()).thenReturn(List.of(mockConnector));
when(mockConnector.getPort()).thenReturn(8090);

AppModule<AppConfiguration, Object> appModule = new AppModule<>(mockConfig, new Object()) {};

int port = appModule.getApplicationPort();

assertThat(port).isEqualTo(8090);
}

@Test
public void testGetApplicationPortThrowsException()
{
DefaultServerFactory defaultServerFactory = new DefaultServerFactory();
defaultServerFactory.setApplicationConnectors(Collections.emptyList());

AppConfiguration appConfiguration = new AppConfiguration();
appConfiguration.setServerFactory(defaultServerFactory);

Environment environment = new Environment("test");

AppModule<AppConfiguration, Environment> appModule = new AppModule<>(appConfiguration, environment) {};

assertThatThrownBy(appModule::getApplicationPort).isInstanceOf(IllegalStateException.class);
}
}

0 comments on commit 32df8a6

Please sign in to comment.