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

Use LockSupport.parkNanos instead of Thread.sleep in systemtests #10522

Merged
merged 1 commit into from
Sep 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -278,11 +280,7 @@ public static void verifyUnchangedTopicAbsence(String namespaceName, String quer

while (System.currentTimeMillis() < endTime) {
assertThat(KafkaCmdClient.listTopicsUsingPodCli(namespaceName, queryingPodName, KafkaResources.plainBootstrapAddress(clusterName)), not(hasItems(absentTopicName)));
try {
Thread.sleep(TestConstants.POLL_INTERVAL_FOR_RESOURCE_READINESS);
} catch (InterruptedException e) {
e.printStackTrace();
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(TestConstants.POLL_INTERVAL_FOR_RESOURCE_READINESS));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -763,7 +765,7 @@ void setupEnvironment() throws Exception {
// wait some time for metrics to be stable - at least reconciliation interval + 10s
LOGGER.info("Sleeping for {} to give operators and operands some time to stable the metrics values before collecting",
TestConstants.SAFETY_RECONCILIATION_INTERVAL);
Thread.sleep(TestConstants.SAFETY_RECONCILIATION_INTERVAL);
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(TestConstants.SAFETY_RECONCILIATION_INTERVAL));

kafkaCollector = new MetricsCollector.Builder()
.withScraperPodName(scraperPodName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

import static io.strimzi.systemtest.TestConstants.REGRESSION;
import static io.strimzi.systemtest.enums.ConditionStatus.False;
Expand Down Expand Up @@ -124,16 +126,15 @@ void testCreateDeleteCreate() throws InterruptedException {
assertThat(adminClient.listTopics(), containsString(testStorage.getTopicName()));

for (int i = 0; i < 10; i++) {
Thread.sleep(2_000);
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));

LOGGER.info("Iteration {}: Deleting {}", i, testStorage.getTopicName());
cmdKubeClient(Environment.TEST_SUITE_NAMESPACE).deleteByName(KafkaTopic.RESOURCE_KIND, testStorage.getTopicName());
KafkaTopicUtils.waitForKafkaTopicDeletion(Environment.TEST_SUITE_NAMESPACE, testStorage.getTopicName());

assertThat(adminClient.listTopics(), not(containsString(testStorage.getTopicName())));

Thread.sleep(2_000);
long t0 = System.currentTimeMillis();
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));

LOGGER.info("Iteration {}: Recreating {}", i, testStorage.getTopicName());
resourceManager.createResourceWithWait(KafkaTopicTemplates.topic(Environment.TEST_SUITE_NAMESPACE, testStorage.getTopicName(), sharedTestStorage.getClusterName())
Expand Down Expand Up @@ -297,7 +298,7 @@ void testCreateTopicAfterUnsupportedOperation() {
* - kafkatopic-not-ready
*/
@IsolatedTest
void testKafkaTopicDifferentStatesInUTOMode() throws InterruptedException {
void testKafkaTopicDifferentStatesInUTOMode() {
final TestStorage testStorage = new TestStorage(ResourceManager.getTestContext());
int initialReplicas = 1;
int initialPartitions = 5;
Expand Down Expand Up @@ -369,7 +370,8 @@ void testKafkaTopicDifferentStatesInUTOMode() throws InterruptedException {

// Wait some time to check if error is still present in KafkaTopic status
LOGGER.info("Waiting {} ms for next reconciliation", topicOperatorReconciliationIntervalMs);
Thread.sleep(topicOperatorReconciliationIntervalMs);

LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(topicOperatorReconciliationIntervalMs));
assertKafkaTopicStatus(testStorage.getTopicName(), Environment.TEST_SUITE_NAMESPACE, Ready, False, reason, reasonMessage, expectedObservedGeneration);

LOGGER.info("Changing KafkaTopic's spec to correct state");
Expand All @@ -384,7 +386,7 @@ void testKafkaTopicDifferentStatesInUTOMode() throws InterruptedException {
}

@ParallelTest
void testKafkaTopicChangingMinInSyncReplicas() throws InterruptedException {
void testKafkaTopicChangingMinInSyncReplicas() {
final TestStorage testStorage = new TestStorage(ResourceManager.getTestContext());

resourceManager.createResourceWithWait(KafkaTopicTemplates.topic(Environment.TEST_SUITE_NAMESPACE, testStorage.getTopicName(), sharedTestStorage.getClusterName(), 5).build());
Expand All @@ -406,7 +408,7 @@ void testKafkaTopicChangingMinInSyncReplicas() throws InterruptedException {

// Wait some time to check if error is still present in KafkaTopic status
LOGGER.info("Waiting {} ms for next reconciliation", topicOperatorReconciliationIntervalMs);
Thread.sleep(topicOperatorReconciliationIntervalMs);
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(topicOperatorReconciliationIntervalMs));
assertKafkaTopicStatus(testStorage.getTopicName(), Environment.TEST_SUITE_NAMESPACE, resourceStatus, conditionStatus, reason, reasonMessage, 2);
}

Expand Down
7 changes: 2 additions & 5 deletions test/src/main/java/io/strimzi/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.LockSupport;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

Expand Down Expand Up @@ -187,11 +188,7 @@ public static long waitFor(String description, long pollIntervalMs, long timeout
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("{} not ready, will try again in {} ms ({}ms till timeout)", description, sleepTime, timeLeft);
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
return deadline - System.currentTimeMillis();
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(sleepTime));
}
}

Expand Down
Loading