Files
ai-security-xdr/haobang-security-dm/syslog-consumer-rule/src/test/java/com/common/service/IngressStatsProjectionWriterTest.java
T
王绍宏 0e9e6656db feat(syslog): 优化大日志通道并增加异常连续消费兜底
- 128 KiB 及以上日志默认进入大日志 Topic
- InfluxDB 原文及达梦大字段超过 256 KiB 时截断保存
- 截断内容追加 HB_XDR_TRUNCATED 标记
- InfluxDB 写入失败时继续执行规则解析和达梦写入
- 未知或系统性异常优先写入普通/大日志 DLT
- DLT 写入失败时记录特殊告警并提交 offset,避免阻塞后续消息
- DLT 失败等待时间默认限制为 1 秒
- 增加连续性放行、DLT 成功及失败监控指标
- 更新 Docker Compose、v3 升级文档及相关测试
2026-09-12 19:22:52 +08:00

79 lines
3.7 KiB
Java

package com.common.service;
import com.common.entity.IngressStatsEvent;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.SimpleTransactionStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class IngressStatsProjectionWriterTest {
@Test
void aggregatesBatchByDeviceDateAndCollectorUsingStableMinMax() {
LocalDateTime t1 = LocalDateTime.of(2026, 8, 13, 10, 0);
LocalDateTime t2 = t1.plusMinutes(2);
LocalDateTime previousDay = t1.minusDays(1);
IngressStatsProjectionWriter.Aggregation result = IngressStatsProjectionWriter.aggregate(Arrays.asList(
new IngressStatsEvent("a", 7L, 3L, t2),
new IngressStatsEvent("b", 7L, 3L, t1),
new IngressStatsEvent("a", 7L, 3L, t2),
new IngressStatsEvent("c", 7L, 3L, previousDay),
new IngressStatsEvent("d", 8L, 4L, t1)));
assertEquals(3, result.devices.size());
assertEquals(2, result.collects.size());
assertEquals(previousDay, result.collects.get(3L).min);
assertEquals(t2, result.collects.get(3L).max);
}
@Test
void usesSameLockOrderRegardlessOfKafkaRecordOrder() {
LocalDateTime time = LocalDateTime.of(2026, 8, 13, 10, 0);
IngressStatsProjectionWriter.Aggregation result = IngressStatsProjectionWriter.aggregate(Arrays.asList(
new IngressStatsEvent("z", 9L, 7L, time),
new IngressStatsEvent("a", 2L, 3L, time),
new IngressStatsEvent("b", 8L, 4L, time.minusDays(1))));
List<Long> devices = new ArrayList<>();
for (IngressStatsProjectionWriter.DeviceKey key : result.devices.keySet()) devices.add(key.deviceId);
assertEquals(Arrays.asList(8L, 2L, 9L), devices);
assertEquals(Arrays.asList(3L, 4L, 7L), new ArrayList<>(result.collects.keySet()));
}
@Test
void directAsyncModeWritesOnlyOneOutboxEvent() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
PlatformTransactionManager manager = mock(PlatformTransactionManager.class);
when(manager.getTransaction(any(TransactionDefinition.class)))
.thenReturn(new SimpleTransactionStatus());
StatsProjectionOutboxWriter outbox = mock(StatsProjectionOutboxWriter.class);
when(outbox.isEnabled()).thenReturn(true);
IngressStatsProjectionWriter writer = new IngressStatsProjectionWriter(
jdbc, manager, new StatsWriteModeProvider("DIRECT"),
mock(StatsMaterializationPublisher.class), outbox);
ReflectionTestUtils.setField(writer, "enabled", true);
ReflectionTestUtils.setField(writer, "transactionTimeoutSeconds", 45);
int groups = writer.writeBatch(Arrays.asList(
new IngressStatsEvent("a", 7L, 3L, LocalDateTime.of(2026, 9, 12, 8, 0)),
new IngressStatsEvent("b", 7L, 3L, LocalDateTime.of(2026, 9, 12, 8, 1))));
assertEquals(2, groups);
verify(outbox).append(org.mockito.ArgumentMatchers.any(StatsProjectionOutboxEvent.class));
verifyNoInteractions(jdbc);
verify(manager).commit(any());
}
}