Skip to content

Commit

Permalink
chore: add momento-local-provider tests, remove enable retry for inse…
Browse files Browse the repository at this point in the history
…cure grpc conn, make getPort abstract
  • Loading branch information
rishtigupta committed Jan 22, 2025
1 parent e6da7cb commit 3f8c3a2
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.auth.MomentoLocalProvider;
import momento.sdk.config.Configuration;
import momento.sdk.config.transport.GrpcConfiguration;
import momento.sdk.internal.GrpcChannelOptions;
Expand Down Expand Up @@ -38,15 +37,9 @@ final class ScsControlGrpcStubsManager implements AutoCloseable {

private static ManagedChannel setupConnection(
CredentialProvider credentialProvider, Configuration configuration) {
final NettyChannelBuilder channelBuilder;

if (credentialProvider.isControlEndpointSecure()) {
channelBuilder = NettyChannelBuilder.forAddress(credentialProvider.getControlEndpoint(), 443);
} else {
int port = ((MomentoLocalProvider) credentialProvider).getPort();
channelBuilder =
NettyChannelBuilder.forAddress(credentialProvider.getControlEndpoint(), port);
}
int port = credentialProvider.getPort();
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forAddress(credentialProvider.getControlEndpoint(), port);

// Override grpc config to disable keepalive for control clients
final GrpcConfiguration controlConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,9 @@ private static void eagerlyConnect(

private ManagedChannel setupChannel(
CredentialProvider credentialProvider, Configuration configuration) {
final NettyChannelBuilder channelBuilder;
if (credentialProvider.isCacheEndpointSecure()) {
channelBuilder = NettyChannelBuilder.forAddress(credentialProvider.getCacheEndpoint(), 443);
} else {
channelBuilder = NettyChannelBuilder.forAddress(credentialProvider.getCacheEndpoint(), 8080);
}
int port = credentialProvider.getPort();
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forAddress(credentialProvider.getCacheEndpoint(), port);

// set additional channel options (message size, keepalive, auth, etc)
GrpcChannelOptions.applyGrpcConfigurationToChannelBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ public static CredentialProvider forMomentoLocal() {
* @return true if connecting to the token endpoint connection with TLS; false if not using TLS
*/
public abstract boolean isTokenEndpointSecure();

/**
* Gets the port with which the Momento client will connect to the Momento control plane.
*
* @return The port.
*/
public abstract int getPort();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public boolean isStorageEndpointSecure() {
return isSecureConnection(storageEndpoint);
}

private boolean isSecureConnection(String endpoint) {
return endpoint.startsWith("https://");
}

/** Returns the port used by this credential provider. */
@Override
public int getPort() {
return port;
}

private boolean isSecureConnection(String endpoint) {
return endpoint.startsWith("https://");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ public String getTokenEndpoint() {
public boolean isTokenEndpointSecure() {
return true;
}

@Override
public int getPort() {
return 443;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static void applyGrpcConfigurationToChannelBuilder(
channelBuilder.disableRetry();
} else {
channelBuilder.usePlaintext();
channelBuilder.enableRetry();
}

grpcConfig.getMaxReceivedMessageSize().ifPresent(channelBuilder::maxInboundMessageSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package momento.sdk.auth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class MomentoLocalProviderTest {

@Test
void testDefaultConstructor() {
MomentoLocalProvider provider = new MomentoLocalProvider();

assertEquals("127.0.0.1", provider.getCacheEndpoint());
assertEquals("127.0.0.1", provider.getControlEndpoint());
assertEquals("127.0.0.1", provider.getTokenEndpoint());
assertEquals("127.0.0.1", provider.getStorageEndpoint());
assertEquals(8080, provider.getPort());
assertFalse(provider.isCacheEndpointSecure());
assertFalse(provider.isControlEndpointSecure());
assertFalse(provider.isTokenEndpointSecure());
assertFalse(provider.isStorageEndpointSecure());
assertEquals("", provider.getAuthToken());
}

@Test
void testConstructorWithHostnameAndPort() {
MomentoLocalProvider provider = new MomentoLocalProvider("localhost", 9090);

assertEquals("localhost", provider.getCacheEndpoint());
assertEquals("localhost", provider.getControlEndpoint());
assertEquals("localhost", provider.getTokenEndpoint());
assertEquals("localhost", provider.getStorageEndpoint());
assertEquals(9090, provider.getPort());
assertFalse(provider.isCacheEndpointSecure());
}

@Test
void testConstructorWithHostnameOnly() {
MomentoLocalProvider provider = new MomentoLocalProvider("custom-host");

assertEquals("custom-host", provider.getCacheEndpoint());
assertEquals("custom-host", provider.getControlEndpoint());
assertEquals("custom-host", provider.getTokenEndpoint());
assertEquals("custom-host", provider.getStorageEndpoint());
assertEquals(8080, provider.getPort());
}

@Test
void testConstructorWithPortOnly() {
MomentoLocalProvider provider = new MomentoLocalProvider(7070);

assertEquals("127.0.0.1", provider.getCacheEndpoint());
assertEquals("127.0.0.1", provider.getControlEndpoint());
assertEquals("127.0.0.1", provider.getTokenEndpoint());
assertEquals("127.0.0.1", provider.getStorageEndpoint());
assertEquals(7070, provider.getPort());
}

@Test
void testSecureEndpointDetection() {
MomentoLocalProvider provider = new MomentoLocalProvider("https://secure-host");

assertTrue(provider.isCacheEndpointSecure());
assertTrue(provider.isControlEndpointSecure());
assertTrue(provider.isTokenEndpointSecure());
assertTrue(provider.isStorageEndpointSecure());
}

@Test
void testInsecureEndpointDetection() {
MomentoLocalProvider provider = new MomentoLocalProvider("http://insecure-host");

assertFalse(provider.isCacheEndpointSecure());
assertFalse(provider.isControlEndpointSecure());
assertFalse(provider.isTokenEndpointSecure());
assertFalse(provider.isStorageEndpointSecure());
}
}

0 comments on commit 3f8c3a2

Please sign in to comment.