Skip to content

Commit ad65f95

Browse files
authored
Fix flaky ProfileFileSupplier test (#4127)
This fixes aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() by ensuring that that file refresher is always able to see that the file has a new modified time.
1 parent 305af51 commit ad65f95

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

core/profiles/src/test/java/software/amazon/awssdk/profiles/ProfileFileSupplierTest.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,9 @@ void aggregate_duplicateOptionsGivenFixedProfileFirst_preservesPrecedence() {
433433
}
434434

435435
@Test
436-
void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() {
437-
AdjustableClock clock = new AdjustableClock();
436+
void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() throws IOException {
437+
Instant startTime = Instant.now();
438+
AdjustableClock clock = new AdjustableClock(startTime);
438439

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

453454
generateTestCredentialsFile("defaultAccessKey2", "defaultSecretAccessKey2");
454455

455-
clock.tickForward(Duration.ofMillis(1_000));
456+
Duration tick = Duration.ofMillis(1_000);
457+
458+
// The refresh logic uses the last modified attribute of the profile file to determine if it's changed and should be
459+
// reloaded; unfortunately that means that if things happen quickly enough, the last modified time of the first version
460+
// of the file, and the new version will be the same. Ensure that there is a change in the last modified time for the
461+
// test file.
462+
Files.setLastModifiedTime(getTestCredentialsFilePath(), FileTime.from(startTime.plus(tick)));
463+
clock.tickForward(tick);
456464

457465
profileFile = supplier.get();
458466
accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();
@@ -505,10 +513,10 @@ void get_givenOnLoadAction_callsActionOncePerNewProfileFile() {
505513
assertThat(blockCount.get()).isEqualTo(actualProfilesCount);
506514
}
507515

508-
private Path generateTestFile(String contents, String filename) {
516+
private Path writeTestFile(String contents, Path path) {
509517
try {
510518
Files.createDirectories(testDirectory);
511-
return Files.write(testDirectory.resolve(filename), contents.getBytes(StandardCharsets.UTF_8));
519+
return Files.write(path, contents.getBytes(StandardCharsets.UTF_8));
512520
} catch (IOException e) {
513521
throw new RuntimeException(e);
514522
}
@@ -517,7 +525,11 @@ private Path generateTestFile(String contents, String filename) {
517525
private Path generateTestCredentialsFile(String accessKeyId, String secretAccessKey) {
518526
String contents = String.format("[default]\naws_access_key_id = %s\naws_secret_access_key = %s\n",
519527
accessKeyId, secretAccessKey);
520-
return generateTestFile(contents, "credentials.txt");
528+
return writeTestFile(contents, getTestCredentialsFilePath());
529+
}
530+
531+
private Path getTestCredentialsFilePath() {
532+
return testDirectory.resolve("credentials.txt");
521533
}
522534

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

529-
return generateTestFile(contents, "config.txt");
541+
return writeTestFile(contents, testDirectory.resolve("config.txt"));
530542
}
531543

532544
private void updateModificationTime(Path path, Instant instant) {
@@ -597,6 +609,10 @@ private AdjustableClock() {
597609
this.time = Instant.now();
598610
}
599611

612+
private AdjustableClock(Instant time) {
613+
this.time = time;
614+
}
615+
600616
@Override
601617
public ZoneId getZone() {
602618
return ZoneOffset.UTC;

0 commit comments

Comments
 (0)