Skip to content

Commit 3702981

Browse files
kmbottsjamesls
authored andcommitted
Added MaximumBatchingWindowInSeconds aws#1778
Updated awsclient.py to accept argument on create and update of lambda event source.
1 parent 8b13175 commit 3702981

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

chalice/awsclient.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,13 +1660,16 @@ def _remove_lambda_permission_if_needed(self, source_arn, function_arn,
16601660
)
16611661

16621662
def create_lambda_event_source(self, event_source_arn, function_name,
1663-
batch_size, starting_position=None):
1664-
# type: (str, str, int, Optional[str]) -> None
1663+
batch_size, starting_position=None,
1664+
maximum_batching_window_in_seconds=0):
1665+
# type: (str, str, int, Optional[str], Optional[int]) -> None
16651666
lambda_client = self._client('lambda')
1667+
batch_window = maximum_batching_window_in_seconds
16661668
kwargs = {
16671669
'EventSourceArn': event_source_arn,
16681670
'FunctionName': function_name,
16691671
'BatchSize': batch_size,
1672+
'MaximumBatchingWindowInSeconds': batch_window
16701673
}
16711674
if starting_position is not None:
16721675
kwargs['StartingPosition'] = starting_position
@@ -1675,12 +1678,18 @@ def create_lambda_event_source(self, event_source_arn, function_name,
16751678
max_attempts=self.LAMBDA_CREATE_ATTEMPTS
16761679
)['UUID']
16771680

1678-
def update_lambda_event_source(self, event_uuid, batch_size):
1679-
# type: (str, int) -> None
1681+
def update_lambda_event_source(self, event_uuid, batch_size,
1682+
maximum_batching_window_in_seconds=0):
1683+
# type: (str, int, Optional[int]) -> None
16801684
lambda_client = self._client('lambda')
1685+
batch_window = maximum_batching_window_in_seconds
1686+
kwargs = {
1687+
'UUID': event_uuid,
1688+
'BatchSize': batch_size,
1689+
'MaximumBatchingWindowInSeconds': batch_window,
1690+
}
16811691
self._call_client_method_with_retries(
1682-
lambda_client.update_event_source_mapping,
1683-
{'UUID': event_uuid, 'BatchSize': batch_size},
1692+
lambda_client.update_event_source_mapping, kwargs,
16841693
max_attempts=10,
16851694
should_retry=self._is_settling_error,
16861695
)

tests/functional/test_awsclient.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,7 @@ def test_can_create_kinesis_event_source(stubbed_session):
35923592
FunctionName=function_name,
35933593
BatchSize=batch_size,
35943594
StartingPosition=starting_position,
3595+
MaximumBatchingWindowInSeconds=0
35953596
).returns({'UUID': 'my-uuid'})
35963597

35973598
stubbed_session.activate_stubs()
@@ -3603,22 +3604,52 @@ def test_can_create_kinesis_event_source(stubbed_session):
36033604
stubbed_session.verify_stubs()
36043605

36053606

3607+
def test_can_create_kinesis_event_source_batching_window(stubbed_session):
3608+
kinesis_arn = 'arn:aws:kinesis:us-west-2:...:stream/MyStream'
3609+
function_name = 'myfunction'
3610+
batch_size = 100
3611+
starting_position = 'TRIM_HORIZON'
3612+
maximum_batching_window_in_seconds = 60
3613+
3614+
lambda_stub = stubbed_session.stub('lambda')
3615+
lambda_stub.create_event_source_mapping(
3616+
EventSourceArn=kinesis_arn,
3617+
FunctionName=function_name,
3618+
BatchSize=batch_size,
3619+
StartingPosition=starting_position,
3620+
MaximumBatchingWindowInSeconds=maximum_batching_window_in_seconds
3621+
3622+
).returns({'UUID': 'my-uuid'})
3623+
3624+
stubbed_session.activate_stubs()
3625+
client = TypedAWSClient(stubbed_session)
3626+
result = client.create_lambda_event_source(
3627+
kinesis_arn, function_name, batch_size, starting_position,
3628+
maximum_batching_window_in_seconds
3629+
)
3630+
assert result == 'my-uuid'
3631+
stubbed_session.verify_stubs()
3632+
3633+
36063634
def test_can_create_sqs_event_source(stubbed_session):
36073635
queue_arn = 'arn:sqs:queue-name'
36083636
function_name = 'myfunction'
36093637
batch_size = 100
3638+
maximum_batching_window_in_seconds = 60
36103639

36113640
lambda_stub = stubbed_session.stub('lambda')
36123641
lambda_stub.create_event_source_mapping(
36133642
EventSourceArn=queue_arn,
36143643
FunctionName=function_name,
3615-
BatchSize=batch_size
3644+
BatchSize=batch_size,
3645+
MaximumBatchingWindowInSeconds=maximum_batching_window_in_seconds
36163646
).returns({'UUID': 'my-uuid'})
36173647

36183648
stubbed_session.activate_stubs()
36193649
client = TypedAWSClient(stubbed_session)
36203650
result = client.create_lambda_event_source(
3621-
queue_arn, function_name, batch_size
3651+
queue_arn, function_name, batch_size,
3652+
maximum_batching_window_in_seconds=maximum_batching_window_in_seconds
36223653
)
36233654
assert result == 'my-uuid'
36243655
stubbed_session.verify_stubs()
@@ -3633,7 +3664,8 @@ def test_can_retry_create_sqs_event_source(stubbed_session):
36333664
lambda_stub.create_event_source_mapping(
36343665
EventSourceArn=queue_arn,
36353666
FunctionName=function_name,
3636-
BatchSize=batch_size
3667+
BatchSize=batch_size,
3668+
MaximumBatchingWindowInSeconds=0
36373669
).raises_error(
36383670
error_code='InvalidParameterValueException',
36393671
message=('The provided execution role does not '
@@ -3642,7 +3674,8 @@ def test_can_retry_create_sqs_event_source(stubbed_session):
36423674
lambda_stub.create_event_source_mapping(
36433675
EventSourceArn=queue_arn,
36443676
FunctionName=function_name,
3645-
BatchSize=batch_size
3677+
BatchSize=batch_size,
3678+
MaximumBatchingWindowInSeconds=0
36463679
).returns({'UUID': 'my-uuid'})
36473680

36483681
stubbed_session.activate_stubs()
@@ -3710,6 +3743,7 @@ def test_can_retry_update_event_source(stubbed_session):
37103743
lambda_stub.update_event_source_mapping(
37113744
UUID='my-uuid',
37123745
BatchSize=5,
3746+
MaximumBatchingWindowInSeconds=0,
37133747
).returns({})
37143748

37153749
stubbed_session.activate_stubs()
@@ -3720,6 +3754,23 @@ def test_can_retry_update_event_source(stubbed_session):
37203754
stubbed_session.verify_stubs()
37213755

37223756

3757+
def test_can_retry_update_event_source_batching_window(stubbed_session):
3758+
lambda_stub = stubbed_session.stub('lambda')
3759+
lambda_stub.update_event_source_mapping(
3760+
UUID='my-uuid',
3761+
BatchSize=5,
3762+
MaximumBatchingWindowInSeconds=60,
3763+
).returns({})
3764+
3765+
stubbed_session.activate_stubs()
3766+
client = TypedAWSClient(stubbed_session)
3767+
client.update_lambda_event_source(
3768+
event_uuid='my-uuid', batch_size=5,
3769+
maximum_batching_window_in_seconds=60
3770+
)
3771+
stubbed_session.verify_stubs()
3772+
3773+
37233774
@pytest.mark.parametrize('resource_name,service_name,is_verified', [
37243775
('queue-name', 'sqs', True),
37253776
('queue-name', 'not-sqs', False),
@@ -3815,11 +3866,13 @@ def test_can_update_lambda_event_source(stubbed_session):
38153866
lambda_stub.update_event_source_mapping(
38163867
UUID='my-uuid',
38173868
BatchSize=5,
3869+
MaximumBatchingWindowInSeconds=60,
38183870
).returns({})
38193871

38203872
stubbed_session.activate_stubs()
38213873
client = TypedAWSClient(stubbed_session)
38223874
client.update_lambda_event_source(
3823-
event_uuid='my-uuid', batch_size=5
3875+
event_uuid='my-uuid', batch_size=5,
3876+
maximum_batching_window_in_seconds=60
38243877
)
38253878
stubbed_session.verify_stubs()

0 commit comments

Comments
 (0)