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

Using awaitability instead of Thread.sleep #5445

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -64,7 +67,7 @@ void setUp() {
}

@Test
void testRenewToken() throws InterruptedException {
void testRenewToken() {
Instant testStartTime = Instant.now();
Map<String, Object> firstMockResponseMap = Map.of("access_token", "first_mock_access_token",
"refresh_token", "first_mock_refresh_token",
Expand All @@ -76,10 +79,11 @@ void testRenewToken() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<?> firstCall = executor.submit(jiraOauthConfig::renewCredentials);
Future<?> secondCall = executor.submit(jiraOauthConfig::renewCredentials);
while (!firstCall.isDone() || !secondCall.isDone()) {
// Do nothing. Wait for the calls to complete
Thread.sleep(10);
}

await()
.atMost(10, SECONDS)
.pollInterval(10, MILLISECONDS)
.until(() -> firstCall.isDone() && secondCall.isDone());
executor.shutdown();
assertNotNull(jiraOauthConfig.getAccessToken());
assertNotNull(jiraOauthConfig.getExpireTime());
Expand Down Expand Up @@ -124,7 +128,7 @@ void testFailedToRenewAccessToken_with_unauthorized_and_trigger_secrets_refresh(


@Test
void testGetTestAccountCloudId() throws InterruptedException {
void testGetTestAccountCloudId() {
Map<String, Object> mockGetCallResponse = new HashMap<>();
mockGetCallResponse.put("id", "test_cloud_id");
when(restTemplateMock.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class)))
Expand All @@ -135,12 +139,12 @@ void testGetTestAccountCloudId() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<?> firstCall = executor.submit(jiraOauthConfig::getUrl);
Future<?> secondCall = executor.submit(jiraOauthConfig::getUrl);
while (!firstCall.isDone() || !secondCall.isDone()) {
// Do nothing. Wait for the calls to complete
Thread.sleep(10);
}
executor.shutdown();

await().atMost(10, SECONDS)
.pollInterval(10, MILLISECONDS)
.until(() -> firstCall.isDone() && secondCall.isDone());

executor.shutdown();
assertEquals("test_cloud_id", jiraOauthConfig.getAtlassianAccountCloudId());
assertEquals("https://api.atlassian.com/ex/test/test_cloud_id/", jiraOauthConfig.getUrl());
//calling second time shouldn't trigger rest call
Expand All @@ -163,7 +167,7 @@ void testGetAtlassianAccountCloudIdUnauthorizedCase() {
jiraOauthConfig.restTemplate = restTemplateMock;


assertThrows(UnauthorizedException.class, () -> jiraOauthConfig.initCredentials());
assertThrows(UnauthorizedException.class, jiraOauthConfig::initCredentials);
verify(restTemplateMock, times(6))
.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class));
verify(restTemplateMock, times(1))
Expand Down
Loading