Skip to content

Commit

Permalink
Fix and test case for #306
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Feb 28, 2024
1 parent 8930e0f commit d27f52d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.sf.dz3r.common;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Duration;
import java.time.format.DateTimeParseException;

public class DurationParser {

private final Logger logger = LogManager.getLogger();

public Duration parse(String source) {

try {
return Duration.parse(source);
} catch (DateTimeParseException ex) {
try {
logger.warn("'{}' doesn't parse, trying PT{}", source, source);
return Duration.parse("PT" + source);
} catch (DateTimeParseException ex2) {
try {
logger.warn("'PT{}' doesn't parse, trying {} hours", source, source);
return Duration.ofHours(Long.parseLong(source));
} catch (NumberFormatException ex3) {

// Enough is enough
logger.warn("Failed to parse '{}' as a) Duration, b) PT$duration, c) hours; giving up and returning 0", source);
return Duration.ZERO;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sf.dz3r.counter;

import net.sf.dz3r.common.DurationParser;
import net.sf.dz3r.common.HCCObjects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -27,6 +28,7 @@
public class FileTimeUsageCounter implements ResourceUsageCounter<Duration>, AutoCloseable {

private final Logger logger = LogManager.getLogger();
private final DurationParser durationParser = new DurationParser();

private static final String CF_THRESHOLD = "threshold";
private static final String CF_CURRENT = "current";
Expand Down Expand Up @@ -123,7 +125,7 @@ private TimeUsageCounter load(File source) throws IOException {
}

var threshold = Optional.ofNullable(thresholdString).map(Duration::parse).orElse(Duration.ZERO);
var current = Duration.parse(currentString);
var current = durationParser.parse(currentString);

return new TimeUsageCounter(current, threshold);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.sf.dz3r.common;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Duration;
import java.util.stream.Stream;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class DurationParserTest {

private final DurationParser dp = new DurationParser();

@ParameterizedTest
@MethodSource("durationProvider")
void parse(String2Duration source) {

var actual = dp.parse(source.s);

assertThat(actual).isEqualTo(source.d);
}

private static Stream<String2Duration> durationProvider() {
return Stream.of(
new String2Duration("PT200H", Duration.ofHours(200)),
new String2Duration("PT200", Duration.ofHours(0)),
new String2Duration("200H", Duration.ofHours(200)),
new String2Duration("200M", Duration.ofMinutes(200)),
new String2Duration("200S", Duration.ofSeconds(200)),
new String2Duration("40H3M4S", Duration.parse("PT40H3M4S")),
new String2Duration("200", Duration.ofHours(200)),
new String2Duration("0", Duration.ofHours(0)),
new String2Duration("huh?", Duration.ofHours(0))
);
}

private record String2Duration(
String s,
Duration d
) {}
}

0 comments on commit d27f52d

Please sign in to comment.