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

Fixing flaky testConfigReloading by adding a proper waits instead of Thread.sleep #181

Merged
merged 1 commit into from
May 6, 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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dependencies {
testImplementation "org.openjdk.jmh:jmh-core:1.37"
testImplementation "org.openjdk.jmh:jmh-generator-annprocess:1.37"
testImplementation "org.assertj:assertj-core:3.25.3"
testImplementation 'org.awaitility:awaitility:4.2.1'

jmh "org.apache.kafka:kafka_2.12:${kafkaVersion}"
}
Expand Down
48 changes: 35 additions & 13 deletions src/test/java/io/aiven/kafka/auth/AivenAclAuthorizerV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,6 +56,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -340,28 +342,48 @@ public void testConfigReloading() throws IOException, InterruptedException {

// check that config is reloaded after file modification
Files.copy(this.getClass().getResourceAsStream("/acls_full.json"), configFilePath);
Thread.sleep(100);

checkSingleAction(requestCtx("User", "pass-1"), action(READ_OPERATION, TOPIC_RESOURCE), true);

await().atMost(Duration.ofSeconds(1)).pollDelay(Duration.ofMillis(100))
.untilAsserted(() -> checkSingleAction(
requestCtx("User", "pass-1"),
action(READ_OPERATION, TOPIC_RESOURCE),
true));
// check that config is reloaded after file deletion
assertThat(configFilePath.toFile().delete()).isTrue();
Thread.sleep(100);

checkSingleAction(requestCtx("User", "pass-1"), action(READ_OPERATION, TOPIC_RESOURCE), false);
await().atMost(Duration.ofSeconds(1)).pollDelay(Duration.ofMillis(100))
.untilAsserted(() -> checkSingleAction(
requestCtx("User", "pass-1"),
action(READ_OPERATION, TOPIC_RESOURCE),
false));

// check that config is reloaded after directory deletion
assertThat(Files.deleteIfExists(configFilePath.getParent().toAbsolutePath())).isTrue();
Thread.sleep(100);
// restoring the config back to check that it's again properly loaded
Files.copy(this.getClass().getResourceAsStream("/acls_full.json"), configFilePath);
await().atMost(Duration.ofSeconds(2)).pollDelay(Duration.ofMillis(100)).pollInterval(Duration.ofMillis(100))
.untilAsserted(() -> checkSingleAction(
requestCtx("User", "pass-1"),
action(READ_OPERATION, TOPIC_RESOURCE),
true));

checkSingleAction(requestCtx("User", "pass-1"), action(READ_OPERATION, TOPIC_RESOURCE), false);
// check that config is reloaded after directory and file deletion
// needed because WatchService is used under the hood,
// and we are subscribing to parent directory changes not file itself
assertThat(Files.deleteIfExists(configFilePath)).isTrue();
assertThat(Files.deleteIfExists(configFilePath.getParent().toAbsolutePath())).isTrue();
await().atMost(Duration.ofSeconds(1)).pollDelay(Duration.ofMillis(100))
.untilAsserted(() -> checkSingleAction(
requestCtx("User", "pass-1"),
action(READ_OPERATION, TOPIC_RESOURCE),
false));

// check that config reloaded after file and directory re-creation
assertThat(tmpDir.toFile().mkdir()).isTrue();
Thread.sleep(100);
await().atMost(Duration.ofSeconds(1)).until(() -> Files.exists(tmpDir));
Files.copy(this.getClass().getResourceAsStream("/acls_plain.json"), configFilePath);
Thread.sleep(100);
checkSingleAction(requestCtx("User", "pass"), action(READ_OPERATION, TOPIC_RESOURCE), true);
await().atMost(Duration.ofSeconds(1)).pollDelay(Duration.ofMillis(100))
.untilAsserted(() -> checkSingleAction(
requestCtx("User", "pass"),
action(READ_OPERATION, TOPIC_RESOURCE),
true));
}

@Test
Expand Down
Loading