Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: java cache and topics integration tests should use random cache names #379

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth changing TEST_AUTH_TOKEN to MOMENTO_API_KEY in this pr? I thought we were slowly changing to that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to change the CI / env vars in a separate PR. This one is pretty busy already.

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
Loading