155 lines
6.9 KiB
Java
155 lines
6.9 KiB
Java
|
|
package com.common.service;
|
||
|
|
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
|
||
|
|
import java.time.Clock;
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.time.ZoneId;
|
||
|
|
import java.time.ZoneOffset;
|
||
|
|
import java.util.Arrays;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
|
|
|
||
|
|
class RawLogCaptureServiceTest {
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void keepsFirstMessagesThenReplacesOldestNormalWithLaterAnomalies() {
|
||
|
|
MutableClock clock = new MutableClock(1_700_000_000_000L);
|
||
|
|
RawLogCaptureService service = new RawLogCaptureService(clock);
|
||
|
|
RawLogCaptureService.CaptureSnapshot started = service.start(7L, 60, 3);
|
||
|
|
|
||
|
|
receive(service, clock, 7L, "e1");
|
||
|
|
receive(service, clock, 7L, "e2");
|
||
|
|
receive(service, clock, 7L, "e3");
|
||
|
|
receive(service, clock, 7L, "e4");
|
||
|
|
assertEquals(Arrays.asList("e1", "e2", "e3"), ids(service.get(started.getSessionId())));
|
||
|
|
|
||
|
|
fail(service, clock, 7L, "e4");
|
||
|
|
fail(service, clock, 7L, "e5");
|
||
|
|
fail(service, clock, 7L, "e6");
|
||
|
|
RawLogCaptureService.CaptureSnapshot anomalies = service.get(started.getSessionId());
|
||
|
|
|
||
|
|
assertEquals(Arrays.asList("e4", "e5", "e6"), ids(anomalies));
|
||
|
|
assertEquals(3, anomalies.getRetainedAnomalyCount());
|
||
|
|
assertEquals(3L, anomalies.getReplacementCount());
|
||
|
|
|
||
|
|
fail(service, clock, 7L, "e7");
|
||
|
|
assertEquals(Arrays.asList("e4", "e5", "e6"),
|
||
|
|
ids(service.get(started.getSessionId())));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void upgradesSelectedMessageToAnomalyWithoutReplacement() {
|
||
|
|
MutableClock clock = new MutableClock(1_700_000_000_000L);
|
||
|
|
RawLogCaptureService service = new RawLogCaptureService(clock);
|
||
|
|
RawLogCaptureService.CaptureSnapshot started = service.start(7L, 60, 2);
|
||
|
|
|
||
|
|
receive(service, clock, 7L, "e1");
|
||
|
|
receive(service, clock, 7L, "e2");
|
||
|
|
fail(service, clock, 7L, "e2");
|
||
|
|
|
||
|
|
RawLogCaptureService.CaptureSnapshot snapshot = service.get(started.getSessionId());
|
||
|
|
assertEquals(Arrays.asList("e1", "e2"), ids(snapshot));
|
||
|
|
assertEquals(1, snapshot.getRetainedAnomalyCount());
|
||
|
|
assertEquals(0L, snapshot.getReplacementCount());
|
||
|
|
assertFalse(snapshot.getMessages().get(0).isAnomaly());
|
||
|
|
assertTrue(snapshot.getMessages().get(1).isAnomaly());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void isolatesDevicesAndAcceptsLateOutcomeOnlyDuringSettlingPeriod() {
|
||
|
|
MutableClock clock = new MutableClock(1_700_000_000_000L);
|
||
|
|
RawLogCaptureService service = new RawLogCaptureService(clock);
|
||
|
|
RawLogCaptureService.CaptureSnapshot started = service.start(7L, 2, 1);
|
||
|
|
long observedInsideWindow = clock.millis();
|
||
|
|
|
||
|
|
receive(service, clock, 8L, "other-device");
|
||
|
|
receive(service, clock, 7L, "normal");
|
||
|
|
clock.advanceMillis(3_000L);
|
||
|
|
assertEquals("DRAINING", service.get(started.getSessionId()).getStatus());
|
||
|
|
|
||
|
|
service.observeFailure(observedInsideWindow, 7L, 1L, "late-error", "topic",
|
||
|
|
LocalDateTime.ofInstant(Instant.ofEpochMilli(observedInsideWindow), ZoneOffset.UTC),
|
||
|
|
"raw-late-error", "RULE_PROCESSING", "DLT_PUBLISHED",
|
||
|
|
new IllegalStateException("bad json"));
|
||
|
|
RawLogCaptureService.CaptureSnapshot draining = service.get(started.getSessionId());
|
||
|
|
assertEquals(Arrays.asList("late-error"), ids(draining));
|
||
|
|
assertEquals(1L, draining.getReplacementCount());
|
||
|
|
|
||
|
|
clock.advanceMillis(31_000L);
|
||
|
|
assertEquals("COMPLETED", service.get(started.getSessionId()).getStatus());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void validatesHardApiLimits() {
|
||
|
|
RawLogCaptureService service = new RawLogCaptureService(
|
||
|
|
new MutableClock(1_700_000_000_000L));
|
||
|
|
assertThrows(IllegalArgumentException.class, () -> service.start(0L, 1, 1));
|
||
|
|
assertThrows(IllegalArgumentException.class, () -> service.start(1L, 601, 1));
|
||
|
|
assertThrows(IllegalArgumentException.class, () -> service.start(1L, 1, 51));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void mergesCandidatesAcrossConsumerInstancesBeforeApplyingAnomalyReplacement() {
|
||
|
|
List<RawLogCaptureService.CapturedLogSnapshot> first = Arrays.asList(
|
||
|
|
snapshot(2, "a2", false), snapshot(3, "a3", false), snapshot(4, "a4", false),
|
||
|
|
snapshot(1, "b1", false), snapshot(100, "b100", false),
|
||
|
|
snapshot(101, "b101", false));
|
||
|
|
List<RawLogCaptureService.CapturedLogSnapshot> anomalies =
|
||
|
|
Arrays.asList(snapshot(200, "a200", true));
|
||
|
|
|
||
|
|
List<RawLogCaptureService.CapturedLogSnapshot> selected =
|
||
|
|
RawLogCaptureService.mergeCandidates(first, anomalies, 3);
|
||
|
|
|
||
|
|
assertEquals(Arrays.asList("a2", "a3", "a200"), selected.stream()
|
||
|
|
.map(RawLogCaptureService.CapturedLogSnapshot::getEventId)
|
||
|
|
.collect(Collectors.toList()));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static RawLogCaptureService.CapturedLogSnapshot snapshot(
|
||
|
|
long sequence, String eventId, boolean anomaly) {
|
||
|
|
return new RawLogCaptureService.CapturedLogSnapshot(sequence, eventId, 7L, 1L,
|
||
|
|
"topic", null, "2026-09-14T00:00:00Z",
|
||
|
|
anomaly ? "NON_NORMAL" : "SUCCESS", anomaly,
|
||
|
|
anomaly ? "JSON解析异常" : null, null, null, "raw-" + eventId);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void receive(RawLogCaptureService service, Clock clock,
|
||
|
|
long deviceId, String eventId) {
|
||
|
|
service.observeReceived(clock.millis(), deviceId, 1L, eventId, "topic",
|
||
|
|
LocalDateTime.ofInstant(Instant.ofEpochMilli(clock.millis()), ZoneOffset.UTC),
|
||
|
|
"raw-" + eventId);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void fail(RawLogCaptureService service, Clock clock,
|
||
|
|
long deviceId, String eventId) {
|
||
|
|
service.observeFailure(clock.millis(), deviceId, 1L, eventId, "topic",
|
||
|
|
LocalDateTime.ofInstant(Instant.ofEpochMilli(clock.millis()), ZoneOffset.UTC),
|
||
|
|
"raw-" + eventId, "RULE_PROCESSING", "DLT_PUBLISHED",
|
||
|
|
new IllegalArgumentException("broken"));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static List<String> ids(RawLogCaptureService.CaptureSnapshot snapshot) {
|
||
|
|
return snapshot.getMessages().stream()
|
||
|
|
.map(RawLogCaptureService.CapturedLogSnapshot::getEventId)
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
}
|
||
|
|
|
||
|
|
private static final class MutableClock extends Clock {
|
||
|
|
private long millis;
|
||
|
|
|
||
|
|
private MutableClock(long millis) { this.millis = millis; }
|
||
|
|
private void advanceMillis(long value) { millis += value; }
|
||
|
|
@Override public ZoneId getZone() { return ZoneOffset.UTC; }
|
||
|
|
@Override public Clock withZone(ZoneId zone) { return this; }
|
||
|
|
@Override public Instant instant() { return Instant.ofEpochMilli(millis); }
|
||
|
|
@Override public long millis() { return millis; }
|
||
|
|
}
|
||
|
|
}
|