Skip to content
Open
Show file tree
Hide file tree
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 @@ -213,7 +213,11 @@ private static int integer(String field) {

private static double rate(String field) {
try {
return Double.parseDouble(field);
double parsed = Double.parseDouble(field);
if (!Double.isFinite(parsed) || parsed < 0.0 || parsed > 1.0) {
throw invalidLine();
}
return parsed;
} catch (NumberFormatException ex) {
throw invalidLine(ex);
}
Expand All @@ -224,7 +228,11 @@ private static Long nullableLong(String field) {
return null;
}
try {
return Long.parseLong(field);
long parsed = Long.parseLong(field);
if (parsed < 0L) {
throw invalidLine();
}
return parsed;
} catch (NumberFormatException ex) {
throw invalidLine(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ void rejectsInvalidPersistedLedgerLines() throws Exception {
assertInvalidLedger(snapshotLine().replace(encoded("tenant-a"), "!"));
}

@Test
void rejectsNonFiniteOrOutOfRangePersistedKpiEvidence() throws Exception {
assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\tNaN\t"));
assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\tInfinity\t"));
assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t-Infinity\t"));
assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t-0.01\t"));
assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t1.01\t"));
assertInvalidLedger(snapshotLine().replace("\t123", "\t-1"));
}

@Test
void acceptsInclusiveKpiNumericBoundaries() throws Exception {
assertValidLedger(snapshotLine().replace("\t0.5\t", "\t0.0\t"), 0.0, 123L);
assertValidLedger(snapshotLine().replace("\t0.5\t", "\t1.0\t"), 1.0, 123L);
assertValidLedger(snapshotLine().replace("\t123", "\t0"), 0.5, 0L);
}

@Test
void reportsLoadAndWriteFailures() throws Exception {
Path directory = tempDir.resolve("directory-ledger");
Expand All @@ -107,7 +124,24 @@ private void assertInvalidLedger(String line) throws Exception {
StandardCharsets.UTF_8
);

assertThrows(IllegalStateException.class, () -> new KpiSnapshotLedger(ledgerPath, CLOCK));
IllegalStateException error = assertThrows(
IllegalStateException.class,
() -> new KpiSnapshotLedger(ledgerPath, CLOCK)
);
assertEquals("kpi snapshot ledger contains an invalid line", error.getMessage());
}

private void assertValidLedger(String line, double expectedRate, Long expectedP95) throws Exception {
Path ledgerPath = Files.writeString(
tempDir.resolve(UUID.randomUUID() + ".log"),
line + System.lineSeparator(),
StandardCharsets.UTF_8
);

KpiSnapshotLedger ledger = new KpiSnapshotLedger(ledgerPath, CLOCK);
var snapshot = ledger.snapshotsFor("tenant-a").getFirst();
assertEquals(expectedRate, snapshot.conversionSuccessRate());
assertEquals(expectedP95, snapshot.p95TimeToPreviewMs());
}

private static TenantContext context(String tenantId) {
Expand Down
Loading