Skip to content

Commit

Permalink
Fix Testing Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jan 31, 2024
1 parent e74cc6d commit 56006f4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
run: |
mvn -s settings.xml -Dgpg.passphrase=${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} --batch-mode deploy -P publish
env:
AZURE_KEY: ${{ secrets.AZURE_KEY }}
AZURE_REGION: ${{ secrets.AZURE_REGION }}
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}

Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ jobs:
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
env:
AZURE_KEY: ${{ secrets.AZURE_KEY }}
AZURE_REGION: ${{ secrets.AZURE_REGION }}

formatting:
runs-on: ubuntu-latest
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ jobs:
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=brenoepics_at4j -P coverage
env:
AZURE_KEY: ${{ secrets.AZURE_KEY }}
AZURE_REGION: ${{ secrets.AZURE_REGION }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public int getTimeTillSpaceGetsAvailable() {
return (int) (Math.max(rateLimitResetTimestamp, globalRLResetTimestamp) - timestamp);
}

/**
* Gets the remaining RateLimit
* @return int the remaining RateLimit
*/
public int getRateLimitRemaining() {
return rateLimitRemaining;
}

/**
* Checks if a bucket created with the given parameters would equal this bucket.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ private RateLimitBucket<T> getMatchingBucket(RestRequest<T> request) {
* @param bucket The bucket the request belongs to.
* @param responseTimestamp The timestamp directly after the response finished.
*/
private void handleResponse(
RestRequest<T> request,
RestRequestResult<T> result,
RateLimitBucket<T> bucket,
long responseTimestamp) {
void handleResponse(
RestRequest<T> request,
RestRequestResult<T> result,
RateLimitBucket<T> bucket,
long responseTimestamp) {
try {
HttpResponse<String> response = result.getResponse();

Expand Down
25 changes: 13 additions & 12 deletions src/test/java/io/github/brenoepics/at4j/AzureApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ void translateEmptySourceLanguage() {

@Test
void translateHelloWorld() {
String subscriptionKey = System.getenv("AZURE_KEY");
String subscriptionRegion = System.getenv("AZURE_REGION");
String azureKey = System.getenv("AZURE_KEY");
String region = System.getenv("AZURE_REGION");
Assumptions.assumeTrue(
subscriptionKey != null && subscriptionRegion != null,
"Azure Credentials are null, skipping the test");
azureKey != null && region != null, "Azure Credentials are null, skipping the test");
Assumptions.assumeTrue(
!azureKey.isEmpty() && !region.isEmpty(), "Azure Credentials are empty, skipping the test");

AzureApiBuilder builder =
new AzureApiBuilder().setKey(subscriptionKey).region(subscriptionRegion);
AzureApiBuilder builder = new AzureApiBuilder().setKey(azureKey).region(region);
AzureApi api = builder.build();

TranslateParams params = new TranslateParams("Hello World!", List.of("pt", "es"));
Expand All @@ -117,13 +117,14 @@ void translateHelloWorld() {

@Test
void detectHelloWorldLanguage() {
String subscriptionKey = System.getenv("AZURE_KEY");
String subscriptionRegion = System.getenv("AZURE_REGION");
String azureKey = System.getenv("AZURE_KEY");
String region = System.getenv("AZURE_REGION");
Assumptions.assumeTrue(
azureKey != null && region != null, "Azure Credentials are null, skipping the test");
Assumptions.assumeTrue(
subscriptionKey != null && subscriptionRegion != null,
"Azure Credentials are null, skipping the test");
AzureApiBuilder builder =
new AzureApiBuilder().setKey(subscriptionKey).region(subscriptionRegion);
!azureKey.isEmpty() && !region.isEmpty(), "Azure Credentials are empty, skipping the test");

AzureApiBuilder builder = new AzureApiBuilder().setKey(azureKey).region(region);
AzureApi api = builder.build();

Optional<DetectedLanguage> detect =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.github.brenoepics.at4j.core.ratelimit;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import io.github.brenoepics.at4j.core.AzureApiImpl;
import io.github.brenoepics.at4j.util.rest.RestEndpoint;
import io.github.brenoepics.at4j.util.rest.RestRequest;

import java.net.http.HttpHeaders;
import java.net.http.HttpResponse;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import io.github.brenoepics.at4j.util.rest.RestRequestResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class RateLimitManagerTest<T> {
private AzureApiImpl<T> api;
private RestRequest<T> request;
private RateLimitManager<T> rateLimitManager;

@BeforeEach
public void setUp() {
api = mock(AzureApiImpl.class, RETURNS_DEEP_STUBS);
request = mock(RestRequest.class);
rateLimitManager = new RateLimitManager<>(api);
}

@Test
void testQueueRequest() {
when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES);
rateLimitManager.queueRequest(request);
verify(api.getThreadPool().getExecutorService()).submit(any(Runnable.class));
}

@Test
void testSearchBucket() {
when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES);
Optional<RateLimitBucket<T>> bucket = rateLimitManager.searchBucket(request);
assertTrue(bucket.isPresent());
}

@Test
void testWaitUntilSpaceGetsAvailable() {
RateLimitBucket<T> bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES);
bucket.setRateLimitRemaining(1000);
bucket.setRateLimitResetTimestamp(System.currentTimeMillis() + 1000);
assertDoesNotThrow(() -> rateLimitManager.waitUntilSpaceGetsAvailable(bucket));
}

@Test
void testRetryRequest() {
when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES);
rateLimitManager.searchBucket(request);
RestRequest<T> retriedRequest = rateLimitManager.retryRequest(new RateLimitBucket<>(RestEndpoint.LANGUAGES ));
assertNull(retriedRequest);
}

@Test
void retryRequestWhenBucketIsEmpty() {
when(request.getEndpoint()).thenReturn(RestEndpoint.LANGUAGES);
rateLimitManager.searchBucket(request);
RestRequest<T> retriedRequest = rateLimitManager.retryRequest(new RateLimitBucket<>(RestEndpoint.LANGUAGES));
assertNull(retriedRequest);
}

@Test
void handleResponseWhenStatusCodeIsNot429() {
HttpHeaders headers = mock(HttpHeaders.class);
when(headers.firstValue(RateLimitManager.RATE_LIMITED_HEADER)).thenReturn(Optional.of("1"));
when(headers.firstValue(RateLimitManager.RATE_LIMIT_RESET_HEADER)).thenReturn(Optional.of("0"));
RateLimitBucket<T> bucket = new RateLimitBucket<>(RestEndpoint.LANGUAGES);
RestRequestResult<T> result = mock(RestRequestResult.class);
when(result.getResponse()).thenReturn(mock(HttpResponse.class));
when(result.getResponse().statusCode()).thenReturn(200);
when(result.getResponse().headers()).thenReturn(headers);
CompletableFuture<RestRequestResult<T>> future = new CompletableFuture<>();
future.complete(result);
when(request.getResult()).thenReturn(future);
rateLimitManager.handleResponse(request, result, bucket, System.currentTimeMillis());
assertEquals(1, bucket.getRateLimitRemaining());
}
}

0 comments on commit 56006f4

Please sign in to comment.