mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): honor MaximumRetryAttempts and DestinationConfig.OnFailure for Kinesis ESM (#6133)
This commit is contained in:
1 parent
023cb8341b
commit
bb32ba06eb
8 files changed
+1112
-110
No files matched your search
@@ -216,12 +216,13 @@ class EmulatorLifecycleTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopsDynamoDbStreamConsumersBeforeTheFlushAndTheBackendBeforeStorageShutdown() {
|
||||
void stopsStreamConsumersBeforeTheFlushAndTheBackendBeforeStorageShutdown() {
|
||||
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
|
||||
|
||||
InOrder inOrder = Mockito.inOrder(dynamodbStreamsPoller, redshiftZeroEtlConsumer, storageFactory,
|
||||
flociUiManager, dynamoDbRuntime);
|
||||
InOrder inOrder = Mockito.inOrder(dynamodbStreamsPoller, kinesisPoller, redshiftZeroEtlConsumer,
|
||||
storageFactory, flociUiManager, dynamoDbRuntime);
|
||||
inOrder.verify(dynamodbStreamsPoller).shutdown();
|
||||
inOrder.verify(kinesisPoller).shutdown();
|
||||
inOrder.verify(redshiftZeroEtlConsumer).shutdown();
|
||||
inOrder.verify(storageFactory).flushAll();
|
||||
inOrder.verify(flociUiManager).shutdown();
|
||||
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.testing.DockerAvailability;
|
||||
import io.github.hectorvent.floci.testing.SqsTestQueue;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Runs a real function that throws on a poison record behind a Kinesis mapping with
|
||||
* {@code MaximumRetryAttempts = 1}: the batch is invoked twice, its invocation record reaches the
|
||||
* SQS OnFailure destination, and the shard goes on with the next record.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class KinesisEsmOnFailureDockerIntegrationTest {
|
||||
|
||||
private static final String BASE_PATH = "/2015-03-31";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String ROLE = "arn:aws:iam::" + ACCOUNT + ":role/lambda-role";
|
||||
private static final Duration DEADLINE = Duration.ofSeconds(90);
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
SqsService sqsService;
|
||||
|
||||
@Inject
|
||||
KinesisService kinesisService;
|
||||
|
||||
@Inject
|
||||
EsmStore esmStore;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
Assumptions.assumeTrue(DockerAvailability.answers(dockerClient),
|
||||
"Docker daemon must be available for the Kinesis OnFailure integration test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void exhaustedBatchReachesTheSqsDestinationAndTheShardGoesOn() throws Exception {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String streamName = "esm-onfailure-" + suffix;
|
||||
String functionName = "esm-onfailure-" + suffix;
|
||||
String streamArn = RequestScopes.callAs(ACCOUNT, () ->
|
||||
kinesisService.createStream(streamName, 1, REGION).getStreamArn());
|
||||
SqsTestQueue queue = SqsTestQueue.create(sqsService, ACCOUNT, REGION, "esm-onfailure-");
|
||||
createFunction(functionName);
|
||||
String uuid = null;
|
||||
try {
|
||||
String poisonSequence = putRecord(streamName, "poison");
|
||||
uuid = given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"FunctionName": "%s",
|
||||
"EventSourceArn": "%s",
|
||||
"StartingPosition": "TRIM_HORIZON",
|
||||
"MaximumRetryAttempts": 1,
|
||||
"DestinationConfig": {"OnFailure": {"Destination": "%s"}}
|
||||
}
|
||||
""".formatted(functionName, streamArn, queue.arn()))
|
||||
.when()
|
||||
.post(BASE_PATH + "/event-source-mappings/")
|
||||
.then()
|
||||
.statusCode(202)
|
||||
.extract().path("UUID");
|
||||
|
||||
JsonNode record = queue.awaitJson(DEADLINE);
|
||||
|
||||
assertEquals("RetryAttemptsExhausted", record.at("/requestContext/condition").asText(), record.toString());
|
||||
assertEquals(2, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
assertEquals("Unhandled", record.at("/responseContext/functionError").asText());
|
||||
assertEquals(poisonSequence, record.at("/KinesisBatchInfo/startSequenceNumber").asText());
|
||||
assertEquals(poisonSequence, record.at("/KinesisBatchInfo/endSequenceNumber").asText());
|
||||
assertEquals(1, record.at("/KinesisBatchInfo/batchSize").asInt());
|
||||
assertEquals(streamArn, record.at("/KinesisBatchInfo/streamArn").asText());
|
||||
|
||||
String goodSequence = putRecord(streamName, "good");
|
||||
awaitCheckpoint(uuid, goodSequence);
|
||||
assertTrue(queue.receive().isEmpty(), "the good record must not reach the OnFailure destination");
|
||||
} finally {
|
||||
if (uuid != null) {
|
||||
given().when().delete(BASE_PATH + "/event-source-mappings/" + uuid);
|
||||
}
|
||||
given().when().delete(BASE_PATH + "/functions/" + functionName);
|
||||
RequestScopes.runAs(ACCOUNT, () -> kinesisService.deleteStream(streamName, REGION));
|
||||
queue.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private String putRecord(String streamName, String data) {
|
||||
return RequestScopes.callAs(ACCOUNT, () ->
|
||||
kinesisService.putRecord(streamName, data.getBytes(StandardCharsets.UTF_8), "pk", REGION));
|
||||
}
|
||||
|
||||
private void createFunction(String functionName) throws Exception {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"FunctionName": "%s",
|
||||
"Runtime": "nodejs20.x",
|
||||
"Role": "%s",
|
||||
"Handler": "index.handler",
|
||||
"Timeout": 10,
|
||||
"Code": {"ZipFile": "%s"}
|
||||
}
|
||||
""".formatted(functionName, ROLE, poisonRaisingZipBase64()))
|
||||
.when()
|
||||
.post(BASE_PATH + "/functions")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
private void awaitCheckpoint(String uuid, String sequenceNumber) throws InterruptedException {
|
||||
long deadline = System.nanoTime() + DEADLINE.toNanos();
|
||||
Map<String, String> checkpoints = Map.of();
|
||||
while (System.nanoTime() < deadline) {
|
||||
checkpoints = esmStore.get(uuid).orElseThrow().getShardSequenceNumbers();
|
||||
if (checkpoints.containsValue(sequenceNumber)) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(250);
|
||||
}
|
||||
fail("the shard was not checkpointed at " + sequenceNumber + " within " + DEADLINE.toSeconds()
|
||||
+ " s, checkpoints: " + checkpoints);
|
||||
}
|
||||
|
||||
private static String poisonRaisingZipBase64() throws Exception {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(bytes)) {
|
||||
zip.putNextEntry(new ZipEntry("index.js"));
|
||||
zip.write("""
|
||||
exports.handler = async (event) => {
|
||||
for (const record of event.Records) {
|
||||
if (Buffer.from(record.kinesis.data, 'base64').toString() === 'poison') {
|
||||
throw new Error('poison record');
|
||||
}
|
||||
}
|
||||
};
|
||||
""".getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(bytes.toByteArray());
|
||||
}
|
||||
}
|
||||
+645
-25
@@ -14,30 +14,47 @@ import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.pipes.PipesFilterMatcher;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -49,6 +66,8 @@ import static org.mockito.Mockito.when;
|
||||
* signal via Mockito {@code timeout(...)}: {@code esmStore.saveForAccount} for a checkpoint advance, or the
|
||||
* invoke itself. {@code never()} assertions target actions the exercised code path never performs on any thread
|
||||
* (invoke in a fully-filtered path; save in an invoke-error path), so they are timing-independent.
|
||||
* Tests that drive several polls in a row wait for each one with {@link #pollOnce}, and move the injected
|
||||
* clock past the retry backoff themselves.
|
||||
*/
|
||||
class KinesisEventSourcePollerTest {
|
||||
|
||||
@@ -57,6 +76,13 @@ class KinesisEventSourcePollerTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String STREAM_ARN = "arn:aws:kinesis:us-east-1:000000000000:stream/s";
|
||||
private static final String SHARD = "shardId-000000000000";
|
||||
private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:000000000000:function:fn";
|
||||
private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:k-dlq";
|
||||
private static final String QUEUE_URL = "http://localhost:4566/000000000000/k-dlq";
|
||||
private static final String TOPIC_ARN = "arn:aws:sns:us-east-1:000000000000:k-failures";
|
||||
private static final String BUCKET_ARN = "arn:aws:s3:::k-failures";
|
||||
/** The first record of {@link #stubShard}; each later record arrives one second after the one before. */
|
||||
private static final Instant FIRST_ARRIVAL = Instant.parse("2026-10-20T12:07:26.012Z");
|
||||
|
||||
private KinesisEventSourcePoller poller;
|
||||
private KinesisService kinesisService;
|
||||
@@ -64,24 +90,39 @@ class KinesisEventSourcePollerTest {
|
||||
private LambdaFunctionStore functionStore;
|
||||
private LambdaAliasStore aliasStore;
|
||||
private EsmStore esmStore;
|
||||
private SqsService sqsService;
|
||||
private SnsService snsService;
|
||||
private S3Service s3Service;
|
||||
private final AtomicLong clock = new AtomicLong();
|
||||
/** What the store holds for the mapping while a poll runs; a test changes it to delete or disable it. */
|
||||
private final AtomicReference<Optional<EventSourceMapping>> storedMapping = new AtomicReference<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
clock.set(FIRST_ARRIVAL.plusSeconds(30).toEpochMilli());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.LambdaServiceConfig lambdaConfig = mock(EmulatorConfig.LambdaServiceConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.lambda()).thenReturn(lambdaConfig);
|
||||
when(lambdaConfig.pollIntervalMs()).thenReturn(1000L);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
kinesisService = mock(KinesisService.class);
|
||||
executorService = mock(LambdaExecutorService.class);
|
||||
functionStore = mock(LambdaFunctionStore.class);
|
||||
aliasStore = mock(LambdaAliasStore.class);
|
||||
esmStore = mock(EsmStore.class);
|
||||
storedMapping.set(Optional.of(storedAs(true)));
|
||||
when(esmStore.getForAccount(anyString(), anyString())).thenAnswer(inv -> storedMapping.get());
|
||||
sqsService = mock(SqsService.class);
|
||||
snsService = mock(SnsService.class);
|
||||
s3Service = mock(S3Service.class);
|
||||
|
||||
poller = new KinesisEventSourcePoller(mock(Vertx.class), kinesisService, executorService,
|
||||
new LambdaTargetResolver(functionStore, aliasStore), esmStore, config, MAPPER, new PipesFilterMatcher(MAPPER));
|
||||
new LambdaTargetResolver(functionStore, aliasStore), esmStore, config, MAPPER,
|
||||
new PipesFilterMatcher(MAPPER), new StreamBatchFailures(sqsService, snsService, s3Service, MAPPER, config),
|
||||
clock::get);
|
||||
}
|
||||
|
||||
private EventSourceMapping esm(String... patterns) {
|
||||
@@ -308,7 +349,7 @@ class KinesisEventSourcePollerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeErrorDoesNotAdvanceCheckpointAndRetriesSameWindow() {
|
||||
void invokeErrorDoesNotAdvanceCheckpointAndRetriesSameWindow() throws Exception {
|
||||
stubFunction();
|
||||
stubStreamWith(List.of(record("s1", "p1", "{\"type\":\"order\"}")));
|
||||
InvokeResult err = new InvokeResult();
|
||||
@@ -316,20 +357,18 @@ class KinesisEventSourcePollerTest {
|
||||
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(err);
|
||||
EventSourceMapping esm = esm("{\"data\":{\"type\":[\"order\"]}}");
|
||||
|
||||
poller.pollAndInvoke(esm);
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
|
||||
// Barrier: a 2nd poll ran (so poll 1 finished). Since the checkpoint never advanced, both polls
|
||||
// re-derive TRIM_HORIZON and fetch the same window.
|
||||
awaitPollCompletedViaSecondFetch(esm);
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(poller.retryBackoffMs(1));
|
||||
pollOnce(esm);
|
||||
|
||||
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
|
||||
assertNull(esm.getShardSequenceNumbers().get(SHARD));
|
||||
// retry-from-old-checkpoint: both invokes carried the same fetched window.
|
||||
ArgumentCaptor<byte[]> cap = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(executorService, timeout(2000).atLeast(2)).invoke(any(), cap.capture(), eq(InvocationType.RequestResponse));
|
||||
verify(executorService, times(2)).invoke(any(), cap.capture(), eq(InvocationType.RequestResponse));
|
||||
List<byte[]> payloads = cap.getAllValues();
|
||||
assertEquals(new String(payloads.get(0), StandardCharsets.UTF_8),
|
||||
new String(payloads.get(payloads.size() - 1), StandardCharsets.UTF_8));
|
||||
new String(payloads.get(1), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -377,33 +416,33 @@ class KinesisEventSourcePollerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportedFailureOnTheFirstRecordRetriesTheWholeBatch() {
|
||||
stubFunction();
|
||||
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
|
||||
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
|
||||
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"s1\"}]}"));
|
||||
void reportedFailureOnTheFirstRecordRetriesTheWholeBatch() throws Exception {
|
||||
stubShard("s1", "s2");
|
||||
List<byte[]> events = recordInvocations(seqs -> batchItemFailures(
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"s1\"}]}"));
|
||||
EventSourceMapping esm = reportingEsm();
|
||||
|
||||
poller.pollAndInvoke(esm);
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
|
||||
awaitPollCompletedViaSecondFetch(esm);
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(poller.retryBackoffMs(1));
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(List.of(List.of("s1", "s2"), List.of("s1", "s2")), invokedSequences(events));
|
||||
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
|
||||
assertNull(esm.getShardSequenceNumbers().get(SHARD));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownItemIdentifierRetriesTheWholeBatch() {
|
||||
stubFunction();
|
||||
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
|
||||
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
|
||||
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"nope\"}]}"));
|
||||
void unknownItemIdentifierRetriesTheWholeBatch() throws Exception {
|
||||
stubShard("s1", "s2");
|
||||
List<byte[]> events = recordInvocations(seqs -> batchItemFailures(
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"nope\"}]}"));
|
||||
EventSourceMapping esm = reportingEsm();
|
||||
|
||||
poller.pollAndInvoke(esm);
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
|
||||
awaitPollCompletedViaSecondFetch(esm);
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(poller.retryBackoffMs(1));
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(List.of(List.of("s1", "s2"), List.of("s1", "s2")), invokedSequences(events));
|
||||
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
|
||||
assertNull(esm.getShardSequenceNumbers().get(SHARD));
|
||||
}
|
||||
@@ -479,4 +518,585 @@ class KinesisEventSourcePollerTest {
|
||||
assertEquals("orders/not-a-consumer", KinesisEventSourcePoller.streamNameFromArn(
|
||||
"arn:aws:kinesis:us-east-1:000000000000:stream/orders/not-a-consumer"));
|
||||
}
|
||||
|
||||
// ─────────── MaximumRetryAttempts, MaximumRecordAgeInSeconds and DestinationConfig.OnFailure ───────────
|
||||
|
||||
@Test
|
||||
void maximumRetryAttemptsAllowsExactlyNPlusOneInvocationsThenDiscardsOnceAndMovesOn() throws Exception {
|
||||
List<KinesisRecord> shard = stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(10, QUEUE_ARN);
|
||||
|
||||
for (int invocation = 1; invocation <= 10; invocation++) {
|
||||
pollOnce(esm);
|
||||
assertEquals(invocation, events.size(), "invocations after poll " + invocation);
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
assertNull(checkpoint(esm), "a batch with retries left must not be checkpointed");
|
||||
clock.addAndGet(StreamBatchFailures.MAX_RETRY_BACKOFF_MS);
|
||||
}
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(11, events.size());
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), anyString(), eq(0), eq(REGION));
|
||||
assertEquals("s1", checkpoint(esm), "the discarded batch must be checkpointed");
|
||||
|
||||
shard.add(record("s2", FIRST_ARRIVAL.plusSeconds(1)));
|
||||
// No clock move: the discarded batch's backoff must not hold back the next records.
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(12, events.size());
|
||||
assertEquals(List.of("s2"), sequences(events.get(11)));
|
||||
assertEquals("s2", checkpoint(esm));
|
||||
verify(sqsService, times(1)).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void zeroMaximumRetryAttemptsDiscardsAfterOneInvocationAndSendsTheAwsRecordToSqs() throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(1, events.size());
|
||||
assertEquals("s1", checkpoint(esm));
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals(List.of("requestContext", "responseContext", "version", "timestamp", "KinesisBatchInfo"),
|
||||
fieldNames(record), "SQS gets the metadata only, no payload: " + record);
|
||||
assertEquals("req-1", record.at("/requestContext/requestId").asText());
|
||||
assertEquals(FUNCTION_ARN, record.at("/requestContext/functionArn").asText());
|
||||
assertEquals("RetryAttemptsExhausted", record.at("/requestContext/condition").asText());
|
||||
assertEquals(1, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
assertEquals(200, record.at("/responseContext/statusCode").asInt());
|
||||
assertEquals("$LATEST", record.at("/responseContext/executedVersion").asText());
|
||||
assertEquals("Unhandled", record.at("/responseContext/functionError").asText());
|
||||
assertEquals("1.0", record.path("version").asText());
|
||||
assertTrue(record.path("timestamp").asText().matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"),
|
||||
"timestamp must be ISO-8601 with milliseconds: " + record.path("timestamp"));
|
||||
assertBatchInfo(record, "s1", "s1", "2026-10-20T12:07:26.012Z", "2026-10-20T12:07:26.012Z", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void snsOnFailureDestinationGetsTheRecordWithoutPayload() throws Exception {
|
||||
stubShard("s1");
|
||||
failInvocationsContaining("s1");
|
||||
|
||||
pollOnce(esmWithOnFailure(0, TOPIC_ARN));
|
||||
|
||||
ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
|
||||
verify(snsService, times(1)).publish(eq(TOPIC_ARN), isNull(), message.capture(), eq("ESM OnFailure"), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(message.getValue());
|
||||
assertFalse(record.has("payload"), "SNS gets the metadata only: " + record);
|
||||
assertEquals("RetryAttemptsExhausted", record.at("/requestContext/condition").asText());
|
||||
assertBatchInfo(record, "s1", "s1", "2026-10-20T12:07:26.012Z", "2026-10-20T12:07:26.012Z", 1);
|
||||
verifyNoInteractions(sqsService, s3Service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void enhancedFanOutMappingReportsTheStreamArnNotTheConsumerArn() throws Exception {
|
||||
stubShard("s1");
|
||||
failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
esm.setEventSourceArn(STREAM_ARN + "/consumer/reader:1700000000");
|
||||
|
||||
pollOnce(esm);
|
||||
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
assertEquals(STREAM_ARN, MAPPER.readTree(body.getValue()).at("/KinesisBatchInfo/streamArn").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3OnFailureDestinationGetsTheExactEventAsAPayloadString() throws Exception {
|
||||
stubShard("s1", "s2");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(1, BUCKET_ARN);
|
||||
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(StreamBatchFailures.MAX_RETRY_BACKOFF_MS);
|
||||
pollOnce(esm);
|
||||
|
||||
ArgumentCaptor<String> key = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<byte[]> body = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service, times(1)).putObject(eq("k-failures"), key.capture(), body.capture(),
|
||||
eq("application/json"), eq(Map.of()));
|
||||
assertTrue(key.getValue().matches("aws/lambda/k-esm/shardId-000000000000/\\d{4}/\\d{2}/\\d{2}/"
|
||||
+ "\\d{4}-\\d{2}-\\d{2}T\\d{2}\\.\\d{2}\\.\\d{2}-[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}"),
|
||||
"unexpected S3 key " + key.getValue());
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals(List.of("requestContext", "responseContext", "version", "payload", "timestamp", "KinesisBatchInfo"),
|
||||
fieldNames(record));
|
||||
assertEquals(2, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
assertTrue(record.path("payload").isTextual(), "payload must be a JSON string, not an object: " + record);
|
||||
assertEquals(MAPPER.readTree(events.get(1)), MAPPER.readTree(record.path("payload").asText()));
|
||||
assertBatchInfo(record, "s1", "s2", "2026-10-20T12:07:26.012Z", "2026-10-20T12:07:27.012Z", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportedBatchItemFailuresUseUpTheRetryAttempts() throws Exception {
|
||||
stubShard("s1", "s2", "s3");
|
||||
List<byte[]> events = recordInvocations(seqs -> batchItemFailures(
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"s2\"}]}"));
|
||||
EventSourceMapping esm = esmWithOnFailure(1, QUEUE_ARN);
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
|
||||
pollOnce(esm);
|
||||
assertEquals("s1", checkpoint(esm), "the records before the lowest failure are done");
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
clock.addAndGet(StreamBatchFailures.MAX_RETRY_BACKOFF_MS);
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(List.of(List.of("s1", "s2", "s3"), List.of("s2", "s3")), invokedSequences(events));
|
||||
assertEquals("s3", checkpoint(esm));
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals("RetryAttemptsExhausted", record.at("/requestContext/condition").asText());
|
||||
assertEquals(2, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
assertEquals(200, record.at("/responseContext/statusCode").asInt());
|
||||
assertFalse(record.path("responseContext").has("functionError"),
|
||||
"a partial batch response is not a function error: " + record);
|
||||
assertBatchInfo(record, "s2", "s3", "2026-10-20T12:07:27.012Z", "2026-10-20T12:07:28.012Z", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportedBatchItemFailureWithZeroRetryAttemptsDiscardsTheRestOfTheBatchAtOnce() throws Exception {
|
||||
stubShard("s1", "s2", "s3");
|
||||
List<byte[]> events = recordInvocations(seqs -> batchItemFailures(
|
||||
"{\"batchItemFailures\":[{\"itemIdentifier\":\"s2\"}]}"));
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(1, events.size());
|
||||
assertEquals("s3", checkpoint(esm));
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals(1, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
assertBatchInfo(record, "s2", "s3", "2026-10-20T12:07:27.012Z", "2026-10-20T12:07:28.012Z", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchOlderThanMaximumRecordAgeIsDiscardedWithTheAgeCondition() throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(null, QUEUE_ARN);
|
||||
esm.setMaximumRecordAgeInSeconds(60);
|
||||
|
||||
clock.set(FIRST_ARRIVAL.plusSeconds(58).toEpochMilli());
|
||||
pollOnce(esm);
|
||||
assertNull(checkpoint(esm), "a 58 s old batch is within the 60 s maximum age");
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
clock.set(FIRST_ARRIVAL.plusSeconds(61).toEpochMilli());
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(2, events.size());
|
||||
assertEquals("s1", checkpoint(esm));
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals("MaximumRecordAgeExceeded", record.at("/requestContext/condition").asText());
|
||||
assertEquals(2, record.at("/requestContext/approximateInvokeCount").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedBatchIsNotRetriedBeforeItsDoublingBackoffElapses() throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(null, null);
|
||||
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(1_999);
|
||||
pollOnce(esm);
|
||||
assertEquals(1, events.size(), "retried before the first backoff (2 x the 1 s poll interval) elapsed");
|
||||
verify(kinesisService, times(1)).getRecordsForAccount(eq(ACCOUNT), anyString(), anyInt(), eq(REGION));
|
||||
clock.addAndGet(1);
|
||||
pollOnce(esm);
|
||||
assertEquals(2, events.size(), "not retried once the first backoff elapsed");
|
||||
|
||||
clock.addAndGet(3_999);
|
||||
pollOnce(esm);
|
||||
assertEquals(2, events.size(), "retried before the second backoff (4 s) elapsed");
|
||||
clock.addAndGet(1);
|
||||
pollOnce(esm);
|
||||
assertEquals(3, events.size(), "not retried once the second backoff elapsed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void backoffStartsWhenTheFailedInvocationEnds() throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = recordInvocations(seqs -> {
|
||||
clock.addAndGet(10_000);
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
EventSourceMapping esm = esmWithOnFailure(null, null);
|
||||
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(1_999);
|
||||
pollOnce(esm);
|
||||
assertEquals(1, events.size(), "a function that took 10 s to fail was retried before the 2 s backoff");
|
||||
clock.addAndGet(1);
|
||||
pollOnce(esm);
|
||||
assertEquals(2, events.size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(ints = -1)
|
||||
void unlimitedRetryAttemptsAndRecordAgeNeverDiscardAFailingBatch(Integer unlimited) throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(unlimited, QUEUE_ARN);
|
||||
esm.setMaximumRecordAgeInSeconds(unlimited);
|
||||
clock.set(FIRST_ARRIVAL.plus(Duration.ofDays(8)).toEpochMilli());
|
||||
|
||||
for (int poll = 0; poll < 20; poll++) {
|
||||
pollOnce(esm);
|
||||
clock.addAndGet(StreamBatchFailures.MAX_RETRY_BACKOFF_MS);
|
||||
}
|
||||
|
||||
assertEquals(20, events.size());
|
||||
assertNull(checkpoint(esm));
|
||||
verify(esmStore, never()).saveForAccount(anyString(), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void exhaustedBatchWithoutOnFailureDestinationIsStillDiscarded() throws Exception {
|
||||
List<KinesisRecord> shard = stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(0, null);
|
||||
|
||||
pollOnce(esm);
|
||||
assertEquals("s1", checkpoint(esm));
|
||||
shard.add(record("s2", FIRST_ARRIVAL.plusSeconds(1)));
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(List.of(List.of("s1"), List.of("s2")), invokedSequences(events));
|
||||
assertEquals("s2", checkpoint(esm));
|
||||
verifyNoInteractions(sqsService, snsService, s3Service);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void mappingDeletedOrDisabledDuringAFailedInvokeSendsNothingAndSavesNothing(boolean deleted) throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = recordInvocations(seqs -> {
|
||||
storedMapping.set(deleted ? Optional.empty() : Optional.of(storedAs(false)));
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(1, events.size());
|
||||
verifyNoInteractions(sqsService);
|
||||
verify(esmStore, never()).saveForAccount(anyString(), any());
|
||||
assertNull(checkpoint(esm));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mappingDisabledDuringAFailedInvokeKeepsNoAttemptOrBackoffForItsReEnable() throws Exception {
|
||||
stubShard("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(1, QUEUE_ARN);
|
||||
AtomicBoolean disableDuringInvoke = new AtomicBoolean(true);
|
||||
List<byte[]> events = recordInvocations(seqs -> {
|
||||
if (disableDuringInvoke.getAndSet(false)) {
|
||||
storedMapping.set(Optional.of(storedAs(false)));
|
||||
poller.stopPolling(esm.getUuid());
|
||||
}
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
|
||||
pollOnce(esm);
|
||||
storedMapping.set(Optional.of(storedAs(true)));
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(2, events.size(), "the re-enabled mapping must not wait out a backoff of the disabled one");
|
||||
verifyNoInteractions(sqsService);
|
||||
assertNull(checkpoint(esm), "the re-enabled mapping must not carry the disabled one's attempt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownDuringAFailedInvokeKeepsTheBatchForTheNextStart() throws Exception {
|
||||
stubShard("s1");
|
||||
recordInvocations(seqs -> {
|
||||
poller.shutdown();
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
|
||||
pollOnce(esmWithOnFailure(0, QUEUE_ARN));
|
||||
|
||||
verifyNoInteractions(sqsService);
|
||||
verify(esmStore, never()).saveForAccount(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetDuringAFailedInvokeDropsItsResultAndPollingResumesAfterIt() throws Exception {
|
||||
stubShard("s1");
|
||||
AtomicBoolean resetDuringInvoke = new AtomicBoolean(true);
|
||||
List<byte[]> events = recordInvocations(seqs -> {
|
||||
if (resetDuringInvoke.getAndSet(false)) {
|
||||
poller.beforeReset();
|
||||
}
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
|
||||
pollOnce(esm);
|
||||
verifyNoInteractions(sqsService);
|
||||
verify(esmStore, never()).saveForAccount(anyString(), any());
|
||||
|
||||
poller.afterReset();
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(2, events.size());
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), anyString(), eq(0), eq(REGION));
|
||||
assertEquals("s1", checkpoint(esm));
|
||||
}
|
||||
|
||||
@Test
|
||||
void throttledBatchOlderThanMaximumRecordAgeIsDiscardedWithTheAgeCondition() throws Exception {
|
||||
stubShard("s1");
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenThrow(new AwsException("TooManyRequestsException", "Rate exceeded", 429));
|
||||
EventSourceMapping esm = esmWithOnFailure(null, QUEUE_ARN);
|
||||
esm.setMaximumRecordAgeInSeconds(60);
|
||||
|
||||
clock.set(FIRST_ARRIVAL.plusSeconds(58).toEpochMilli());
|
||||
pollOnce(esm);
|
||||
assertNull(checkpoint(esm), "a throttled batch within the 60 s maximum age is kept");
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
clock.set(FIRST_ARRIVAL.plusSeconds(61).toEpochMilli());
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals("s1", checkpoint(esm), "a throttled batch past the maximum age must not block the shard");
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
|
||||
JsonNode record = MAPPER.readTree(body.getValue());
|
||||
assertEquals("MaximumRecordAgeExceeded", record.at("/requestContext/condition").asText(), record.toString());
|
||||
assertEquals(0, record.at("/requestContext/approximateInvokeCount").asInt(), "the function never ran");
|
||||
assertEquals(429, record.at("/responseContext/statusCode").asInt());
|
||||
assertFalse(record.path("responseContext").has("functionError"), record.toString());
|
||||
assertBatchInfo(record, "s1", "s1", "2026-10-20T12:07:26.012Z", "2026-10-20T12:07:26.012Z", 1);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"disabled", "shutdown"})
|
||||
void recordsTheFunctionHandledStayCheckpointedWhenTheMappingStopsDuringAPartialResponse(String stop)
|
||||
throws Exception {
|
||||
stubShard("s1", "s2", "s3");
|
||||
recordInvocations(seqs -> {
|
||||
if ("disabled".equals(stop)) {
|
||||
storedMapping.set(Optional.of(storedAs(false)));
|
||||
} else {
|
||||
poller.shutdown();
|
||||
}
|
||||
return batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"s2\"}]}");
|
||||
});
|
||||
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals("s1", checkpoint(esm), "s1 was handled by the function and must not be sent to it again");
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"deleted", "reset"})
|
||||
void mappingStoppedDuringTheOnFailureSendIsNotWrittenBack(String stop) throws Exception {
|
||||
stubShard("s1");
|
||||
failInvocationsContaining("s1");
|
||||
when(sqsService.sendMessage(anyString(), anyString(), anyInt(), anyString())).thenAnswer(inv -> {
|
||||
if ("deleted".equals(stop)) {
|
||||
storedMapping.set(Optional.empty());
|
||||
} else {
|
||||
poller.beforeReset();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
pollOnce(esmWithOnFailure(0, QUEUE_ARN));
|
||||
|
||||
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), anyString(), eq(0), eq(REGION));
|
||||
verify(esmStore, never()).saveForAccount(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopPollingDuringAFailedInvokeKeepsNoAttemptBeforeTheStoreDeleteRuns() throws Exception {
|
||||
stubShard("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(1, QUEUE_ARN);
|
||||
AtomicBoolean stopDuringInvoke = new AtomicBoolean(true);
|
||||
List<byte[]> events = recordInvocations(seqs -> {
|
||||
if (stopDuringInvoke.getAndSet(false)) {
|
||||
// DeleteEventSourceMapping stops polling first; the store still holds the mapping.
|
||||
poller.stopPolling(esm.getUuid());
|
||||
}
|
||||
return new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
|
||||
});
|
||||
|
||||
pollOnce(esm);
|
||||
poller.startPolling(esm);
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(2, events.size(), "a mapping stopped during its invoke must not keep a backoff");
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mappingDeletedReleasesItsStopTombstone() {
|
||||
poller.stopPolling("k-esm-gone");
|
||||
|
||||
poller.mappingDeleted("k-esm-gone");
|
||||
|
||||
assertTrue(poller.stopped.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopPollingForgetsTheAttemptsAndBackoffOfItsMapping() throws Exception {
|
||||
stubShard("s1");
|
||||
List<byte[]> events = failInvocationsContaining("s1");
|
||||
EventSourceMapping esm = esmWithOnFailure(1, QUEUE_ARN);
|
||||
|
||||
pollOnce(esm);
|
||||
poller.stopPolling(esm.getUuid());
|
||||
pollOnce(esm);
|
||||
|
||||
assertEquals(2, events.size(), "a stopped mapping must not keep its backoff");
|
||||
assertNull(checkpoint(esm), "a stopped mapping must not keep its attempt count");
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
private void pollOnce(EventSourceMapping esm) throws InterruptedException {
|
||||
poller.pollAndInvoke(esm);
|
||||
long deadline = System.currentTimeMillis() + 3_000;
|
||||
while (!poller.activePolls.isEmpty() && System.currentTimeMillis() < deadline) {
|
||||
Thread.sleep(5);
|
||||
}
|
||||
assertTrue(poller.activePolls.isEmpty(), "poll did not complete before the test timeout");
|
||||
}
|
||||
|
||||
private static KinesisRecord record(String seq, Instant arrival) {
|
||||
return new KinesisRecord("{}".getBytes(StandardCharsets.UTF_8), "pk-" + seq, seq, arrival);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serves one shard as Kinesis does: TRIM_HORIZON reads from the first record, AFTER_SEQUENCE_NUMBER
|
||||
* after the checkpoint, at most the batch size. Adding to the returned list writes to the shard.
|
||||
*/
|
||||
private List<KinesisRecord> stubShard(String... sequences) {
|
||||
List<KinesisRecord> shard = new CopyOnWriteArrayList<>();
|
||||
for (int i = 0; i < sequences.length; i++) {
|
||||
shard.add(record(sequences[i], FIRST_ARRIVAL.plusSeconds(i)));
|
||||
}
|
||||
KinesisStream stream = new KinesisStream("s", STREAM_ARN);
|
||||
stream.setShards(List.of(new KinesisShard(SHARD, "0", "1", "0")));
|
||||
when(kinesisService.describeStreamForAccount(eq(ACCOUNT), anyString(), eq(REGION))).thenReturn(stream);
|
||||
when(kinesisService.getShardIteratorForAccount(eq(ACCOUNT), anyString(), eq(SHARD), anyString(), any(), eq(REGION)))
|
||||
.thenAnswer(inv -> "after:" + (inv.getArgument(4) == null ? "" : inv.getArgument(4)));
|
||||
when(kinesisService.getRecordsForAccount(eq(ACCOUNT), anyString(), anyInt(), eq(REGION))).thenAnswer(inv -> {
|
||||
int from = indexAfter(shard, inv.<String>getArgument(1).substring("after:".length()));
|
||||
int to = Math.min(shard.size(), from + inv.<Integer>getArgument(2));
|
||||
return Map.of("Records", new ArrayList<>(shard.subList(from, to)));
|
||||
});
|
||||
stubFunction();
|
||||
return shard;
|
||||
}
|
||||
|
||||
/** The index of the record after {@code checkpoint}, or 0 (the trim horizon) when no record has it. */
|
||||
private static int indexAfter(List<KinesisRecord> shard, String checkpoint) {
|
||||
for (int i = 0; i < shard.size(); i++) {
|
||||
if (shard.get(i).getSequenceNumber().equals(checkpoint)) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Answers every invoke with {@code respond}, given the delivered sequences, and keeps each event. */
|
||||
private List<byte[]> recordInvocations(Function<List<String>, InvokeResult> respond) {
|
||||
List<byte[]> events = new CopyOnWriteArrayList<>();
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse))).thenAnswer(inv -> {
|
||||
byte[] event = inv.getArgument(1);
|
||||
events.add(event);
|
||||
return respond.apply(sequences(event));
|
||||
});
|
||||
return events;
|
||||
}
|
||||
|
||||
/** The function raises, as a handler that throws does: status 200 with an Unhandled function error. */
|
||||
private List<byte[]> failInvocationsContaining(String poison) {
|
||||
return recordInvocations(seqs -> {
|
||||
if (!seqs.contains(poison)) {
|
||||
return new InvokeResult();
|
||||
}
|
||||
InvokeResult error = new InvokeResult(200, "Unhandled",
|
||||
"{\"errorMessage\":\"boom\"}".getBytes(StandardCharsets.UTF_8), null, "req-1");
|
||||
error.setExecutedVersion("$LATEST");
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
private List<String> sequences(byte[] event) {
|
||||
List<String> sequences = new ArrayList<>();
|
||||
deliveredRecords(event).forEach(r -> sequences.add(r.path("kinesis").path("sequenceNumber").asText()));
|
||||
return sequences;
|
||||
}
|
||||
|
||||
private List<List<String>> invokedSequences(List<byte[]> events) {
|
||||
return events.stream().map(this::sequences).toList();
|
||||
}
|
||||
|
||||
private EventSourceMapping esmWithOnFailure(Integer maximumRetryAttempts, String destinationArn) {
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setFunctionArn(FUNCTION_ARN);
|
||||
esm.setMaximumRetryAttempts(maximumRetryAttempts);
|
||||
if (destinationArn != null) {
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(destinationArn);
|
||||
EventSourceMapping.DestinationConfig destinationConfig = new EventSourceMapping.DestinationConfig();
|
||||
destinationConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destinationConfig);
|
||||
}
|
||||
return esm;
|
||||
}
|
||||
|
||||
private static EventSourceMapping storedAs(boolean enabled) {
|
||||
EventSourceMapping stored = new EventSourceMapping();
|
||||
stored.setEnabled(enabled);
|
||||
return stored;
|
||||
}
|
||||
|
||||
private static String checkpoint(EventSourceMapping esm) {
|
||||
return esm.getShardSequenceNumbers().get(SHARD);
|
||||
}
|
||||
|
||||
private static List<String> fieldNames(JsonNode node) {
|
||||
List<String> names = new ArrayList<>();
|
||||
node.fieldNames().forEachRemaining(names::add);
|
||||
return names;
|
||||
}
|
||||
|
||||
private static void assertBatchInfo(JsonNode record, String startSequence, String endSequence,
|
||||
String firstArrival, String lastArrival, int batchSize) {
|
||||
JsonNode batchInfo = record.path("KinesisBatchInfo");
|
||||
assertEquals(List.of("shardId", "startSequenceNumber", "endSequenceNumber", "approximateArrivalOfFirstRecord",
|
||||
"approximateArrivalOfLastRecord", "batchSize", "streamArn"), fieldNames(batchInfo), record.toString());
|
||||
assertEquals(SHARD, batchInfo.path("shardId").asText());
|
||||
assertEquals(startSequence, batchInfo.path("startSequenceNumber").asText());
|
||||
assertEquals(endSequence, batchInfo.path("endSequenceNumber").asText());
|
||||
assertEquals(firstArrival, batchInfo.path("approximateArrivalOfFirstRecord").asText());
|
||||
assertEquals(lastArrival, batchInfo.path("approximateArrivalOfLastRecord").asText());
|
||||
assertTrue(batchInfo.path("batchSize").isInt(), "batchSize must be a number: " + batchInfo);
|
||||
assertEquals(batchSize, batchInfo.path("batchSize").asInt());
|
||||
assertEquals(STREAM_ARN, batchInfo.path("streamArn").asText());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -29,4 +31,15 @@ public final class DockerAvailability {
|
||||
}
|
||||
return process.exitValue() == 0;
|
||||
}
|
||||
|
||||
/** True when the daemon Floci itself talks to, through {@code dockerClient}, answers a ping. */
|
||||
public static boolean answers(DockerClient dockerClient) {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception expected) {
|
||||
// An unreachable daemon only means the caller skips its test rather than failing it.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user