-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: java cache and topics integration tests should use random c…
…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
Showing
15 changed files
with
1,326 additions
and
1,211 deletions.
There are no files selected for viewing
572 changes: 313 additions & 259 deletions
572
momento-sdk/src/intTest/java/momento/sdk/AuthClientCacheTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 41 additions & 17 deletions
58
momento-sdk/src/intTest/java/momento/sdk/BaseTestClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.