Skip to content

Fix flaky ProfileFileSupplier test #4127

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

Merged
merged 1 commit into from
Jun 21, 2023
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 @@ -433,8 +433,9 @@ void aggregate_duplicateOptionsGivenFixedProfileFirst_preservesPrecedence() {
}

@Test
void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() {
AdjustableClock clock = new AdjustableClock();
void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() throws IOException {
Instant startTime = Instant.now();
AdjustableClock clock = new AdjustableClock(startTime);

ProfileFile configFile1 = configFile("profile default", Pair.of("aws_access_key_id", "config-key"));
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");
Expand All @@ -452,7 +453,14 @@ void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence()

generateTestCredentialsFile("defaultAccessKey2", "defaultSecretAccessKey2");

clock.tickForward(Duration.ofMillis(1_000));
Duration tick = Duration.ofMillis(1_000);

// The refresh logic uses the last modified attribute of the profile file to determine if it's changed and should be
// reloaded; unfortunately that means that if things happen quickly enough, the last modified time of the first version
// of the file, and the new version will be the same. Ensure that there is a change in the last modified time for the
// test file.
Files.setLastModifiedTime(getTestCredentialsFilePath(), FileTime.from(startTime.plus(tick)));
clock.tickForward(tick);

profileFile = supplier.get();
accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();
Expand Down Expand Up @@ -505,10 +513,10 @@ void get_givenOnLoadAction_callsActionOncePerNewProfileFile() {
assertThat(blockCount.get()).isEqualTo(actualProfilesCount);
}

private Path generateTestFile(String contents, String filename) {
private Path writeTestFile(String contents, Path path) {
try {
Files.createDirectories(testDirectory);
return Files.write(testDirectory.resolve(filename), contents.getBytes(StandardCharsets.UTF_8));
return Files.write(path, contents.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -517,7 +525,11 @@ private Path generateTestFile(String contents, String filename) {
private Path generateTestCredentialsFile(String accessKeyId, String secretAccessKey) {
String contents = String.format("[default]\naws_access_key_id = %s\naws_secret_access_key = %s\n",
accessKeyId, secretAccessKey);
return generateTestFile(contents, "credentials.txt");
return writeTestFile(contents, getTestCredentialsFilePath());
}

private Path getTestCredentialsFilePath() {
return testDirectory.resolve("credentials.txt");
}

private Path generateTestConfigFile(Pair<Object, Object>... pairs) {
Expand All @@ -526,7 +538,7 @@ private Path generateTestConfigFile(Pair<Object, Object>... pairs) {
.collect(Collectors.joining(System.lineSeparator()));
String contents = String.format("[default]\n%s", values);

return generateTestFile(contents, "config.txt");
return writeTestFile(contents, testDirectory.resolve("config.txt"));
}

private void updateModificationTime(Path path, Instant instant) {
Expand Down Expand Up @@ -597,6 +609,10 @@ private AdjustableClock() {
this.time = Instant.now();
}

private AdjustableClock(Instant time) {
this.time = time;
}

@Override
public ZoneId getZone() {
return ZoneOffset.UTC;
Expand Down