Skip to content

Commit

Permalink
Clean up timestamp file name
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 15, 2024
1 parent 3c17786 commit 6e402d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,18 @@ private void createDirectory(
}
}

@VisibleForTesting
String formatOptionalTimestamp(final Optional<UInt64> maybeTimestamp) {
return maybeTimestamp
.map(this::formatTimestamp)
.orElse(generateTimestamp(new SystemTimeProvider()));
}

@VisibleForTesting
String formatTimestamp(final UInt64 arrivalTimestamp) {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss.SS");
final Date date = new Date(arrivalTimestamp.longValue());
return df.format(date);
private String formatOptionalTimestamp(final Optional<UInt64> maybeTimestamp) {
return formatOptionalTimestamp(maybeTimestamp, new SystemTimeProvider());
}

@VisibleForTesting
String generateTimestamp(final TimeProvider timeProvider) {
String formatOptionalTimestamp(
final Optional<UInt64> maybeTimestamp, final TimeProvider timeProvider) {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss.SS");
final Date date = new Date(timeProvider.getTimeInMillis().longValue());
final Date date =
maybeTimestamp
.map(timestamp -> new Date(timestamp.longValue()))
.orElse(new Date(timeProvider.getTimeInMillis().longValue()));
return df.format(date);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
"/eth/test/topic", arrivalTimestamp, messageBytes, new Throwable());

final String fileName =
String.format("%s.ssz", manager.formatTimestamp(timeProvider.getTimeInMillis()));
String.format("%s.ssz", formatTimestamp(timeProvider.getTimeInMillis().longValue()));
final Path expectedFile =
tempDir
.resolve("gossip_messages")
Expand All @@ -71,7 +71,7 @@ void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Pat
assertThat(manager.isEnabled()).isFalse();

final String fileName =
String.format("%s.ssz", manager.formatTimestamp(timeProvider.getTimeInMillis()));
String.format("%s.ssz", formatTimestamp(timeProvider.getTimeInMillis().longValue()));
final Path expectedFile =
tempDir
.resolve("gossip_messages")
Expand All @@ -90,7 +90,7 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
"/eth/test/topic", arrivalTimestamp, messageBytes, Optional.of("reason"));

final String fileName =
String.format("%s.ssz", manager.formatTimestamp(timeProvider.getTimeInMillis()));
String.format("%s.ssz", formatTimestamp(timeProvider.getTimeInMillis().longValue()));
final Path expectedFile =
tempDir
.resolve("gossip_messages")
Expand Down Expand Up @@ -190,21 +190,18 @@ void constructionOfDirectories_shouldDisableWhenFailedToCreate(@TempDir Path tem
void formatTimestamp_shouldFormatDate() {
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
final String formattedTimestamp =
manager.formatOptionalTimestamp(Optional.of(timeProvider.getTimeInMillis()));

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss.SS");
final Date date = new Date(timeProvider.getTimeInMillis().longValue());
assertThat(formattedTimestamp).isEqualTo(df.format(date));
manager.formatOptionalTimestamp(Optional.of(timeProvider.getTimeInMillis()), timeProvider);
assertThat(formattedTimestamp)
.isEqualTo(formatTimestamp(timeProvider.getTimeInMillis().longValue()));
}

@Test
void generateTimestamp_shouldGenerateTimestamp() {
final DebugDataDumper manager = new DebugDataDumper(Path.of("."), true);
final String formattedTimestamp = manager.generateTimestamp(timeProvider);

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss.SS");
final Date date = new Date(timeProvider.getTimeInMillis().longValue());
assertThat(formattedTimestamp).isEqualTo(df.format(date));
final String formattedTimestamp =
manager.formatOptionalTimestamp(Optional.empty(), timeProvider);
assertThat(formattedTimestamp)
.isEqualTo(formatTimestamp(timeProvider.getTimeInMillis().longValue()));
}

private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {
Expand All @@ -226,4 +223,10 @@ private void checkFileNotExist(final Path path) {
}
fail("File was found and bytes read");
}

private String formatTimestamp(final long timeInMillis) {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss.SS");
final Date date = new Date(timeInMillis);
return df.format(date);
}
}

0 comments on commit 6e402d6

Please sign in to comment.