Skip to content

Commit

Permalink
feat: Cache Exception handling and CI Updates (#45)
Browse files Browse the repository at this point in the history
* feat: Exception handling and integration test for Cache

Also attempting to enable ci

* Add ci

* formatting fixes

* fix tracing test

* formatting fixes

* formatting
  • Loading branch information
gautamomento committed Sep 29, 2021
1 parent 3f4326f commit 674812b
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
# - name: Run integration tests
# run: ./gradlew integrationTest
- name: Run integration tests
run: ./gradlew integrationTest
167 changes: 80 additions & 87 deletions momento-sdk/src/intTest/java/momento/sdk/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
*/
package momento.sdk;

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import static momento.sdk.TestHelpers.DEFAULT_CACHE_ENDPOINT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
Expand All @@ -13,26 +15,28 @@
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.exceptions.ClientSdkException;
import momento.sdk.exceptions.PermissionDeniedException;
import momento.sdk.messages.ClientGetResponse;
import momento.sdk.messages.ClientSetResponse;
import momento.sdk.messages.MomentoCacheResult;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class CacheTest {
Cache cache;
final class CacheTest {

private Cache cache;

@BeforeAll
static void beforeAll() {
Expand All @@ -59,7 +63,7 @@ Cache getCache(Optional<OpenTelemetry> openTelemetry) {
Cache getCache(String authToken, String cacheName, Optional<OpenTelemetry> openTelemetry) {
String endpoint = System.getenv("TEST_ENDPOINT");
if (endpoint == null) {
endpoint = "alpha.cacheservice.com";
endpoint = DEFAULT_CACHE_ENDPOINT;
}

return new Cache(
Expand Down Expand Up @@ -88,28 +92,22 @@ void testBlockingClientHappyPathWithTracing() throws Exception {
verifyGetTrace("1");
}

void testHappyPath(Cache cache) {
try {
String key = UUID.randomUUID().toString();

// Set Key sync
ClientSetResponse setRsp =
cache.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 2);
Assertions.assertEquals(MomentoCacheResult.Ok, setRsp.getResult());
private static void testHappyPath(Cache cache) {
String key = UUID.randomUUID().toString();

// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = cache.get(key);
// Set Key sync
ClientSetResponse setRsp =
cache.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 2);
assertEquals(MomentoCacheResult.Ok, setRsp.getResult());

Assertions.assertEquals(MomentoCacheResult.Hit, rsp.getResult());
Assertions.assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());

} catch (IOException e) {
Assertions.fail(e);
}
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = cache.get(key);
assertEquals(MomentoCacheResult.Hit, rsp.getResult());
assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());
}

@Test
void testAsyncClientHappyPath() {
void testAsyncClientHappyPath() throws Exception {
testAsyncHappyPath(cache);
}

Expand All @@ -125,28 +123,22 @@ void testAsyncClientHappyPathWithTracing() throws Exception {
verifyGetTrace("1");
}

void testAsyncHappyPath(Cache client) {
try {
String key = UUID.randomUUID().toString();
// Set Key Async
CompletionStage<ClientSetResponse> setRsp =
client.setAsync(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 10);
Assertions.assertEquals(
MomentoCacheResult.Ok, setRsp.toCompletableFuture().get().getResult());

// Get Key Async
ClientGetResponse<ByteBuffer> rsp = client.getAsync(key).toCompletableFuture().get();
private static void testAsyncHappyPath(Cache client) throws Exception {
String key = UUID.randomUUID().toString();
// Set Key Async
CompletionStage<ClientSetResponse> setRsp =
client.setAsync(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 10);
assertEquals(MomentoCacheResult.Ok, setRsp.toCompletableFuture().get().getResult());

Assertions.assertEquals(MomentoCacheResult.Hit, rsp.getResult());
Assertions.assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());
// Get Key Async
ClientGetResponse<ByteBuffer> rsp = client.getAsync(key).toCompletableFuture().get();

} catch (IOException | InterruptedException | ExecutionException e) {
Assertions.fail(e);
}
assertEquals(MomentoCacheResult.Hit, rsp.getResult());
assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());
}

@Test
void testTtlHappyPath() {
void testTtlHappyPath() throws Exception {
testTtlHappyPath(cache);
}

Expand All @@ -162,25 +154,19 @@ void testTtlHappyPathWithTracing() throws Exception {
verifyGetTrace("1");
}

void testTtlHappyPath(Cache client) {
try {
String key = UUID.randomUUID().toString();

// Set Key sync
ClientSetResponse setRsp =
client.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 1);
Assertions.assertEquals(MomentoCacheResult.Ok, setRsp.getResult());

Thread.sleep(1500);
private static void testTtlHappyPath(Cache client) throws Exception {
String key = UUID.randomUUID().toString();

// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = client.get(key);
// Set Key sync
ClientSetResponse setRsp =
client.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 1);
assertEquals(MomentoCacheResult.Ok, setRsp.getResult());

Assertions.assertEquals(MomentoCacheResult.Miss, rsp.getResult());
Thread.sleep(1500);

} catch (IOException | InterruptedException e) {
Assertions.fail(e);
}
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = client.get(key);
assertEquals(MomentoCacheResult.Miss, rsp.getResult());
}

@Test
Expand All @@ -200,16 +186,11 @@ void testMissHappyPathWithTracing() throws Exception {
verifyGetTrace("1");
}

void testMissHappyPathInternal(Cache client) {
try {
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = client.get(UUID.randomUUID().toString());

Assertions.assertEquals(MomentoCacheResult.Miss, rsp.getResult());
private static void testMissHappyPathInternal(Cache client) {
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = client.get(UUID.randomUUID().toString());

} catch (IOException e) {
Assertions.fail(e);
}
assertEquals(MomentoCacheResult.Miss, rsp.getResult());
}

@Test
Expand All @@ -226,31 +207,43 @@ void testBadAuthTokenWithTracing() throws Exception {
testBadAuthToken(client);
// To accommodate for delays in tracing logs to appear in docker
Thread.sleep(1000);
verifySetTrace("0");
verifySetTrace("1");
verifyGetTrace("1");
}

void testBadAuthToken(Cache badCredClient) {

try {
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = badCredClient.get(UUID.randomUUID().toString());
private static void testBadAuthToken(Cache badCredClient) {
// Bad Auth for Get
assertThrows(PermissionDeniedException.class, () -> badCredClient.get("myCacheKey"));

// Bad Auth for Set
assertThrows(
PermissionDeniedException.class,
() ->
badCredClient.set(
"myCacheKey",
ByteBuffer.wrap("cache me if you can".getBytes(StandardCharsets.UTF_8)),
500));
}

Assertions.fail("expected PERMISSION_DENIED io.grpc.StatusRuntimeException");
@Test
public void unreachableEndpoint_ThrowsException() {
Cache cache =
new Cache(
System.getenv("TEST_AUTH_TOKEN"),
System.getenv("TEST_CACHE_NAME"),
"nonexistent.preprod.a.momentohq.com");

assertThrows(ClientSdkException.class, () -> cache.get("key"));
}

} catch (IOException e) {
Assertions.fail(e);
} catch (io.grpc.StatusRuntimeException e) {
@Disabled("TODO: Update to catch cache not ready and then do a get again to see not found.")
@Test
public void invalidCache_ThrowsNotFoundException() {
Cache cache =
getCache(System.getenv("TEST_AUTH_TOKEN"), UUID.randomUUID().toString(), Optional.empty());

// Make sure we get permission denied error the way we would expected
Assertions.assertEquals(
new StatusRuntimeException(
Status.PERMISSION_DENIED.withDescription("Malformed authorization token"))
.toString(),
e.toString());
}
assertThrows(CacheNotFoundException.class, () -> cache.get("key"));
}

/** ================ HELPER FUNCTIONS ====================================== */
OpenTelemetrySdk setOtelSDK() {
String otelGwUrl = "0.0.0.0";
Expand Down Expand Up @@ -305,7 +298,7 @@ void verifySetTrace(String expectedCount) throws Exception {
true,
"Verify set trace",
"failed to verify set trace");
Assertions.assertEquals(expectedCount, count.trim());
assertEquals(expectedCount, count.trim());
}

void verifyGetTrace(String expectedCount) throws Exception {
Expand All @@ -315,7 +308,7 @@ void verifyGetTrace(String expectedCount) throws Exception {
true,
"Verify get trace",
"failed to verify get trace");
Assertions.assertEquals(expectedCount, count.trim());
assertEquals(expectedCount, count.trim());
}

// Polls a command until the expected result comes back
Expand Down
65 changes: 41 additions & 24 deletions momento-sdk/src/intTest/java/momento/sdk/MomentoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
*/
package momento.sdk;

import static momento.sdk.TestHelpers.DEFAULT_MOMENTO_HOSTED_ZONE_ENDPOINT;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import momento.sdk.exceptions.CacheAlreadyExistsException;
import momento.sdk.messages.ClientGetResponse;
import momento.sdk.messages.ClientSetResponse;
import momento.sdk.messages.MomentoCacheResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

final class MomentoTest {

class MomentoTest {
// Cache cache;
private String authToken;
private String cacheName;

// @org.junit.jupiter.api.BeforeAll
@BeforeAll
static void beforeAll() {
if (System.getenv("TEST_AUTH_TOKEN") == null) {
throw new IllegalArgumentException(
Expand All @@ -25,31 +33,40 @@ static void beforeAll() {
}
}

@org.junit.jupiter.api.Test
void testHappyPath() {
try {
Momento m =
Momento.builder()
.authToken(System.getenv("TEST_AUTH_TOKEN"))
.endpointOverride("cell-alpha-dev.preprod.a.momentohq.com")
.build();
Cache cache = m.createCache(System.getenv("TEST_CACHE_NAME"));
@BeforeEach
void setup() {
this.authToken = System.getenv("TEST_AUTH_TOKEN");
this.cacheName = System.getenv("TEST_CACHE_NAME");
}

String key = java.util.UUID.randomUUID().toString();
@Test
void testHappyPath() {
Momento momento =
Momento.builder()
.authToken(authToken)
.endpointOverride(DEFAULT_MOMENTO_HOSTED_ZONE_ENDPOINT)
.build();
Cache cache = getOrCreate(momento, cacheName);

// Set Key sync
ClientSetResponse setRsp =
cache.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 2);
Assertions.assertEquals(MomentoCacheResult.Ok, setRsp.getResult());
String key = java.util.UUID.randomUUID().toString();

// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = cache.get(key);
// Set Key sync
ClientSetResponse setRsp =
cache.set(key, ByteBuffer.wrap("bar".getBytes(StandardCharsets.UTF_8)), 2);
assertEquals(MomentoCacheResult.Ok, setRsp.getResult());

Assertions.assertEquals(MomentoCacheResult.Hit, rsp.getResult());
Assertions.assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());
// Get Key that was just set
ClientGetResponse<ByteBuffer> rsp = cache.get(key);
assertEquals(MomentoCacheResult.Hit, rsp.getResult());
assertEquals("bar", StandardCharsets.US_ASCII.decode(rsp.getBody()).toString());
}

} catch (java.io.IOException e) {
Assertions.fail(e);
// TODO: Update this to be recreated each time and add a separate test case for Already Exists
private static Cache getOrCreate(Momento momento, String cacheName) {
try {
return momento.createCache(cacheName);
} catch (CacheAlreadyExistsException e) {
return momento.getCache(cacheName);
}
}
}
11 changes: 11 additions & 0 deletions momento-sdk/src/intTest/java/momento/sdk/TestHelpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package momento.sdk;

public final class TestHelpers {

private TestHelpers() {}

public static final String DEFAULT_MOMENTO_HOSTED_ZONE_ENDPOINT =
"cell-alpha-dev.preprod.a.momentohq.com";
public static final String DEFAULT_CACHE_ENDPOINT =
"cache." + DEFAULT_MOMENTO_HOSTED_ZONE_ENDPOINT;
}
Loading

0 comments on commit 674812b

Please sign in to comment.