Skip to content

Commit

Permalink
Merge pull request #181 from Aiven-Open/anatolii/config-reloading-tes…
Browse files Browse the repository at this point in the history
…t-fix

Fixing flaky testConfigReloading by adding a proper waits instead of Thread.sleep
  • Loading branch information
ivanyu authored May 6, 2024
2 parents 2528df1 + 9ab704d commit 7b50558
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
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

0 comments on commit 7b50558

Please sign in to comment.