Skip to content

Commit

Permalink
refactor: java cache and topics integration tests should use random c…
Browse files Browse the repository at this point in the history
…ache names (#379)

Refactors the cache and topics integration tests to use a random cache
name instead of reading from the environment. This follows the
JavaScript SDK pattern.

We have re-written the tests to use a base test class that sets up the
client, assigns a random cache name, creates the cache, and tears it
down at the end of tests.

Because a test class now re-uses the same cache, we have refactored
the tests to use random keys in each test case. These two changes have
fixed race conditions that have caused consistent test failures when
running locally.

Lastly the auth tests were not closing clients in the tests. Because
of this, warning messages appeared in the logs consistently. We have
fixed these problems by closing clients between reuse.
  • Loading branch information
malandis authored Jul 9, 2024
1 parent 7e6741e commit de3dbe1
Show file tree
Hide file tree
Showing 15 changed files with 1,326 additions and 1,211 deletions.
572 changes: 313 additions & 259 deletions momento-sdk/src/intTest/java/momento/sdk/AuthClientCacheTests.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
import momento.sdk.auth.accessControl.TopicSelector;
import momento.sdk.exceptions.MomentoErrorCode;
import momento.sdk.responses.auth.GenerateDisposableTokenResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class AuthClientTopicTests extends BaseTestClass {
private AuthClient authClient;
private String cacheName;
private String topicName = "topic";
private static AuthClient authClient;
private static String topicName = "topic";

@BeforeEach
void setup() {
@BeforeAll
static void setup() {
authClient = AuthClient.builder(credentialProvider).build();
cacheName = System.getenv("TEST_CACHE_NAME");
}

@Test
Expand Down
58 changes: 41 additions & 17 deletions momento-sdk/src/intTest/java/momento/sdk/BaseTestClass.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
package momento.sdk;

import java.time.Duration;
import java.util.UUID;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.responses.cache.control.CacheCreateResponse;
import momento.sdk.responses.cache.control.CacheDeleteResponse;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

public class BaseTestClass {
public static final Duration FIVE_SECONDS = Duration.ofSeconds(5);
protected static final Duration DEFAULT_TTL_SECONDS = Duration.ofSeconds(60);
protected static final Duration FIVE_SECONDS = Duration.ofSeconds(5);
protected static CredentialProvider credentialProvider;

public static final CredentialProvider credentialProvider =
CredentialProvider.fromEnvVar("TEST_AUTH_TOKEN");
protected static CacheClient cacheClient;
protected static String cacheName;

@BeforeAll
static void beforeAll() {
if (System.getenv("TEST_AUTH_TOKEN") == null) {
throw new IllegalArgumentException(
"Integration tests require TEST_AUTH_TOKEN env var; see README for more details.");
}
if (System.getenv("TEST_CACHE_NAME") == null) {
throw new IllegalArgumentException(
"Integration tests require TEST_CACHE_NAME env var; see README for more details.");
credentialProvider = CredentialProvider.fromEnvVar("TEST_AUTH_TOKEN");
cacheClient =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_TTL_SECONDS)
.build();
cacheName = testCacheName();
ensureTestCacheExists(cacheName);
}

@AfterAll
static void afterAll() {
cleanupTestCache(cacheName);
cacheClient.close();
}

protected static void ensureTestCacheExists(String cacheName) {
CacheCreateResponse response = cacheClient.createCache(cacheName).join();
if (response instanceof CacheCreateResponse.Error) {
throw new RuntimeException(
"Failed to test create cache: " + ((CacheCreateResponse.Error) response).getMessage());
}
ensureTestCacheExists();
}

private static void ensureTestCacheExists() {
try (CacheClient client =
CacheClient.builder(
credentialProvider, Configurations.Laptop.latest(), Duration.ofSeconds(10))
.build()) {
client.createCache(System.getenv("TEST_CACHE_NAME")).join();
public static void cleanupTestCache(String cacheName) {
CacheDeleteResponse response = cacheClient.deleteCache(cacheName).join();
if (response instanceof CacheDeleteResponse.Error) {
throw new RuntimeException(
"Failed to test delete cache: " + ((CacheDeleteResponse.Error) response).getMessage());
}
}

public static String testCacheName() {
return "java-integration-test-default-" + UUID.randomUUID();
}

public static String testStoreName() {
return "java-integration-test-default-" + UUID.randomUUID();
}
}
Loading

0 comments on commit de3dbe1

Please sign in to comment.