mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(lambda): support ScalingConfig.MaximumConcurrency on SQS event source mappings (#490)
* feat(lambda): support ScalingConfig.MaximumConcurrency on SQS event source mappings
Wire-level support for ScalingConfig on CreateEventSourceMapping,
UpdateEventSourceMapping, GetEventSourceMapping, and
ListEventSourceMappings:
- New ScalingConfig model carrying MaximumConcurrency (Integer, nullable).
- Create and Update parse ScalingConfig from the request, validate range
(2-1000), reject non-SQS event sources with InvalidParameterValueException.
- Response includes ScalingConfig only when a cap is set; omitted when
null, matching AWS behavior.
- Update with an empty ScalingConfig clears the cap (AWS reset semantics).
The value is persisted on the EventSourceMapping but not yet enforced at
invocation time — the SQS poller today serializes to one concurrent
invocation per ESM regardless. Real parallel dispatch capped by
MaximumConcurrency is a follow-up (Scope B).
Tests:
- EsmIntegrationTest: 6 new cases covering round-trip, below/above range
rejection, non-SQS rejection, omission when unset, add-then-clear via
update.
- LambdaEsmScalingConfigTest (SDK compat): 5 cases verifying SDK typed
ScalingConfig round-trips, InvalidParameterValueException on range
violations, and add/clear lifecycle via update.
* fix(lambda): reject non-integer MaximumConcurrency and ScalingConfig on non-SQS sources
- parseScalingConfig now detects fractional values (e.g. 2.5) and
returns InvalidParameterValueException instead of silently truncating.
- Empty ScalingConfig ({}) on a non-SQS event source is now rejected.
Previously the non-SQS check only fired when MaximumConcurrency was
non-null, so an empty ScalingConfig on Kinesis / DynamoDB Streams
slipped through.
- Two new integration tests cover both cases.
* fix(lambda): tighten ScalingConfig input validation
- Reject non-object ScalingConfig (e.g. "foo", []) with 400 instead of
silently treating it as absent. In update paths this previously cleared
an existing cap unintentionally.
- Reject non-numeric MaximumConcurrency (e.g. string "7"). AWS expects a
JSON number on the wire; the previous toString + parseInt path accepted
strings that real AWS would reject.
- Use longValue() before range-checking to avoid intValue() overflow on
large inputs (e.g. 5000000000) that would produce a negative value and
a misleading error message.
- Two new integration tests: string MaximumConcurrency and non-object
ScalingConfig.
* fix(lambda): add NaN/Infinity guard and expand ScalingConfig test coverage
- parseScalingConfig now explicitly rejects NaN and Infinity rather than
letting them fall through to longValue() with a misleading error.
- The post-range non-SQS guard was flagged as dead code by the reviewer
but is in fact reachable (non-SQS + valid numeric MaximumConcurrency).
Retained and confirmed by the existing @Order(43) test.
- New tests: update range validation (below/above on PUT), and list
with mixed ScalingConfig presence across ESMs.
This commit is contained in:
1 parent
ca2e5d4153
commit
d8243b8803
7 files changed
+644
-1
No files matched your search
+172
@@ -0,0 +1,172 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.services.lambda.LambdaClient;
|
||||
import software.amazon.awssdk.services.lambda.model.*;
|
||||
import software.amazon.awssdk.services.lambda.model.Runtime;
|
||||
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
|
||||
import software.amazon.awssdk.services.sqs.model.DeleteQueueRequest;
|
||||
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest;
|
||||
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@DisplayName("Lambda - ESM ScalingConfig")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class LambdaEsmScalingConfigTest {
|
||||
|
||||
private static final String FUNCTION_NAME = "sdk-test-esm-scaling-fn";
|
||||
private static final String SQS_QUEUE_NAME = "sdk-test-esm-scaling-queue";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role";
|
||||
|
||||
private static LambdaClient lambda;
|
||||
private static SqsClient sqs;
|
||||
private static String queueArn;
|
||||
private static String queueUrl;
|
||||
private static final Set<String> createdEsmUuids = new HashSet<>();
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
lambda = TestFixtures.lambdaClient();
|
||||
sqs = TestFixtures.sqsClient();
|
||||
|
||||
lambda.createFunction(CreateFunctionRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.runtime(Runtime.NODEJS20_X)
|
||||
.role(ROLE)
|
||||
.handler("index.handler")
|
||||
.code(FunctionCode.builder()
|
||||
.zipFile(SdkBytes.fromByteArray(LambdaUtils.minimalZip()))
|
||||
.build())
|
||||
.build());
|
||||
|
||||
queueUrl = sqs.createQueue(CreateQueueRequest.builder()
|
||||
.queueName(SQS_QUEUE_NAME)
|
||||
.build())
|
||||
.queueUrl();
|
||||
queueArn = sqs.getQueueAttributes(GetQueueAttributesRequest.builder()
|
||||
.queueUrl(queueUrl)
|
||||
.attributeNames(QueueAttributeName.QUEUE_ARN)
|
||||
.build())
|
||||
.attributes()
|
||||
.get(QueueAttributeName.QUEUE_ARN);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (lambda != null) {
|
||||
for (String uuid : createdEsmUuids) {
|
||||
try {
|
||||
lambda.deleteEventSourceMapping(DeleteEventSourceMappingRequest.builder()
|
||||
.uuid(uuid).build());
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
try {
|
||||
lambda.deleteFunction(DeleteFunctionRequest.builder()
|
||||
.functionName(FUNCTION_NAME).build());
|
||||
} catch (Exception ignored) {}
|
||||
lambda.close();
|
||||
}
|
||||
if (sqs != null) {
|
||||
try {
|
||||
sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build());
|
||||
} catch (Exception ignored) {}
|
||||
sqs.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createEsm_withScalingConfig_roundTrips() {
|
||||
CreateEventSourceMappingResponse created = lambda.createEventSourceMapping(
|
||||
CreateEventSourceMappingRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.eventSourceArn(queueArn)
|
||||
.batchSize(5)
|
||||
.scalingConfig(ScalingConfig.builder().maximumConcurrency(7).build())
|
||||
.build());
|
||||
createdEsmUuids.add(created.uuid());
|
||||
|
||||
assertThat(created.scalingConfig()).isNotNull();
|
||||
assertThat(created.scalingConfig().maximumConcurrency()).isEqualTo(7);
|
||||
|
||||
GetEventSourceMappingResponse fetched = lambda.getEventSourceMapping(
|
||||
GetEventSourceMappingRequest.builder().uuid(created.uuid()).build());
|
||||
assertThat(fetched.scalingConfig()).isNotNull();
|
||||
assertThat(fetched.scalingConfig().maximumConcurrency()).isEqualTo(7);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createEsm_withoutScalingConfig_doesNotExposeIt() {
|
||||
CreateEventSourceMappingResponse created = lambda.createEventSourceMapping(
|
||||
CreateEventSourceMappingRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.eventSourceArn(queueArn)
|
||||
.batchSize(2)
|
||||
.build());
|
||||
createdEsmUuids.add(created.uuid());
|
||||
|
||||
// SDK models an omitted object as null (or an unset builder); both are acceptable
|
||||
assertThat(created.scalingConfig() == null
|
||||
|| created.scalingConfig().maximumConcurrency() == null).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void createEsm_withMaximumConcurrencyBelowTwo_throwsInvalidParameter() {
|
||||
assertThatThrownBy(() -> lambda.createEventSourceMapping(
|
||||
CreateEventSourceMappingRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.eventSourceArn(queueArn)
|
||||
.scalingConfig(ScalingConfig.builder().maximumConcurrency(1).build())
|
||||
.build()))
|
||||
.isInstanceOf(InvalidParameterValueException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void createEsm_withMaximumConcurrencyAboveThousand_throwsInvalidParameter() {
|
||||
assertThatThrownBy(() -> lambda.createEventSourceMapping(
|
||||
CreateEventSourceMappingRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.eventSourceArn(queueArn)
|
||||
.scalingConfig(ScalingConfig.builder().maximumConcurrency(1001).build())
|
||||
.build()))
|
||||
.isInstanceOf(InvalidParameterValueException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void updateEsm_addsThenClearsScalingConfig() {
|
||||
CreateEventSourceMappingResponse created = lambda.createEventSourceMapping(
|
||||
CreateEventSourceMappingRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.eventSourceArn(queueArn)
|
||||
.batchSize(4)
|
||||
.build());
|
||||
createdEsmUuids.add(created.uuid());
|
||||
|
||||
UpdateEventSourceMappingResponse added = lambda.updateEventSourceMapping(
|
||||
UpdateEventSourceMappingRequest.builder()
|
||||
.uuid(created.uuid())
|
||||
.scalingConfig(ScalingConfig.builder().maximumConcurrency(3).build())
|
||||
.build());
|
||||
assertThat(added.scalingConfig()).isNotNull();
|
||||
assertThat(added.scalingConfig().maximumConcurrency()).isEqualTo(3);
|
||||
|
||||
// Clear by sending an empty ScalingConfig — AWS treats this as "reset".
|
||||
UpdateEventSourceMappingResponse cleared = lambda.updateEventSourceMapping(
|
||||
UpdateEventSourceMappingRequest.builder()
|
||||
.uuid(created.uuid())
|
||||
.scalingConfig(ScalingConfig.builder().build())
|
||||
.build());
|
||||
assertThat(cleared.scalingConfig() == null
|
||||
|| cleared.scalingConfig().maximumConcurrency() == null).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user