Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudwatch): keep GetLogEvents paging inside the list bounds (#2355)

TiMc163a8678cca
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
index b03fc09001..6ddba64111 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
@@ -428,7 +428,10 @@ public class CloudWatchLogsService {
if (nextToken != null && nextToken.startsWith("f/")) {
int offset = parseTokenIndex(nextToken, 2);
pageStart = Math.min(offset, total);
- pageEnd = Math.min(pageStart + maxEvents, total);
+ // Take the window out of what is left rather than adding to the offset, so a
+ // max-events cap configured near Integer.MAX_VALUE cannot overflow the end
+ // index negative once pagination has moved past the first page.
+ pageEnd = pageStart + Math.min(maxEvents, total - pageStart);
} else if (nextToken != null && nextToken.startsWith("b/")) {
int end = parseTokenIndex(nextToken, 2);
pageEnd = Math.min(end, total);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
index f2f71c3fc2..70ace48154 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
@@ -524,6 +524,39 @@ class CloudWatchLogsServiceTest {
assertEquals("f/3", atEnd.nextForwardToken(), "token must echo back to signal end of stream");
}
+ @Test
+ void getLogEventsPagesForwardWithAnUnboundedMaxEventsPerQuery() {
+ CloudWatchLogsService unboundedService = new CloudWatchLogsService(
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ Integer.MAX_VALUE,
+ new RegionResolver("us-east-1", "000000000000")
+ );
+
+ unboundedService.createLogGroup("/app/logs", null, null, REGION);
+ unboundedService.createLogStream("/app/logs", "stream-1", REGION);
+ long now = System.currentTimeMillis();
+ unboundedService.putLogEvents("/app/logs", "stream-1", List.of(
+ Map.of("timestamp", now, "message", "a"),
+ Map.of("timestamp", now + 1, "message", "b"),
+ Map.of("timestamp", now + 2, "message", "c")
+ ), REGION);
+
+ CloudWatchLogsService.LogEventsResult page =
+ unboundedService.getLogEvents("/app/logs", "stream-1", null, null, 0, true, null, REGION);
+ assertEquals(3, page.events().size());
+ assertEquals("f/3", page.nextForwardToken());
+
+ // GetLogEvents echoes its token at the end of the stream, so a paginator always
+ // spends one more call on the token it was just handed.
+ CloudWatchLogsService.LogEventsResult atEnd = unboundedService.getLogEvents(
+ "/app/logs", "stream-1", null, null, 0, true, page.nextForwardToken(), REGION);
+ assertEquals(0, atEnd.events().size());
+ assertEquals("f/3", atEnd.nextForwardToken());
+ }
+
@Test
void getLogEventsRejectsMalformedNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);