Skip to content

Commit 43e4a6d

Browse files
SQS: fix ReceiveMessageWaitTimeSeconds & KmsDataKeyReusePeriodSeconds wrong attribute names (getmoto#9759)
Co-authored-by: Brian Pandola <bpandola@gmail.com>
1 parent 0517668 commit 43e4a6d

3 files changed

Lines changed: 70 additions & 4 deletions

File tree

moto/sqs/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ def _set_attributes(
326326

327327
integer_fields = (
328328
"DelaySeconds",
329-
"KmsDataKeyreusePeriodSeconds",
329+
"KmsDataKeyReusePeriodSeconds",
330330
"MaximumMessageSize",
331331
"MessageRetentionPeriod",
332-
"ReceiveMessageWaitTime",
332+
"ReceiveMessageWaitTimeSeconds",
333333
"VisibilityTimeout",
334334
)
335335
bool_fields = ("ContentBasedDeduplication", "FifoQueue")

tests/test_sqs/test_sqs.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
from moto import mock_aws, settings
1717
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
18+
from moto.core.utils import camelcase_to_underscores
1819
from moto.sqs.models import (
1920
MAXIMUM_MESSAGE_LENGTH,
2021
MAXIMUM_MESSAGE_SIZE_ATTR_LOWER_BOUND,
2122
MAXIMUM_MESSAGE_SIZE_ATTR_UPPER_BOUND,
2223
Queue,
24+
sqs_backends,
2325
)
2426
from moto.utilities.distutils_version import LooseVersion
2527
from tests import aws_verified
@@ -320,6 +322,64 @@ def test_is_empty_redrive_policy_returns_false_for_valid_policy_format():
320322
assert not Queue._is_empty_redrive_policy(json.dumps({"maxReceiveCount": 5}))
321323

322324

325+
@mock_aws
326+
@pytest.mark.parametrize(
327+
"attribute_name,attribute_value",
328+
[
329+
("DelaySeconds", "5"),
330+
("KmsDataKeyReusePeriodSeconds", "300"),
331+
("MaximumMessageSize", "262144"),
332+
("MessageRetentionPeriod", "345600"),
333+
("ReceiveMessageWaitTimeSeconds", "10"),
334+
("VisibilityTimeout", "30"),
335+
],
336+
)
337+
def test_set_attributes_integer_fields_conversion(attribute_name, attribute_value):
338+
"""Test that integer fields are properly converted from strings to integers."""
339+
if settings.TEST_SERVER_MODE:
340+
raise SkipTest("Cannot access backend directly in server mode")
341+
342+
client = boto3.client("sqs", region_name=REGION)
343+
queue_name = str(uuid4())[0:6]
344+
345+
# KMS attributes require KmsMasterKeyId to be set
346+
attributes = {attribute_name: attribute_value}
347+
if attribute_name == "KmsDataKeyReusePeriodSeconds":
348+
attributes["KmsMasterKeyId"] = "alias/MyKey"
349+
350+
q_resp = client.create_queue(QueueName=queue_name, Attributes=attributes)
351+
queue_url = q_resp["QueueUrl"]
352+
353+
attrs = client.get_queue_attributes(
354+
QueueUrl=queue_url, AttributeNames=[attribute_name]
355+
)
356+
assert attrs["Attributes"][attribute_name] == attribute_value
357+
358+
# Verify internal value is stored as integer
359+
backend = sqs_backends[ACCOUNT_ID][REGION]
360+
queue = backend.get_queue(queue_name)
361+
internal_attr_name = camelcase_to_underscores(attribute_name)
362+
internal_value = getattr(queue, internal_attr_name)
363+
assert isinstance(internal_value, int)
364+
assert internal_value == int(attribute_value)
365+
366+
# Update attribute
367+
new_value = str(int(attribute_value) + 1)
368+
client.set_queue_attributes(
369+
QueueUrl=queue_url, Attributes={attribute_name: new_value}
370+
)
371+
372+
attrs = client.get_queue_attributes(
373+
QueueUrl=queue_url, AttributeNames=[attribute_name]
374+
)
375+
assert attrs["Attributes"][attribute_name] == new_value
376+
377+
# Verify updated value is still integer internally
378+
internal_value = getattr(queue, internal_attr_name)
379+
assert isinstance(internal_value, int)
380+
assert internal_value == int(new_value)
381+
382+
323383
@mock_aws
324384
def test_set_queue_attribute_empty_redrive_removes_attr():
325385
client = boto3.client("sqs", region_name=REGION)

tests/test_sqs/test_sqs_integration.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def test_invoke_fake_function_from_sqs_queue():
8080
sqs = boto3.resource("sqs", region_name="us-east-1")
8181
queue_name = str(uuid.uuid4())[0:6]
8282
queue = sqs.create_queue(QueueName=queue_name)
83-
83+
queue.set_attributes(
84+
Attributes={"ReceiveMessageWaitTimeSeconds": "2"} # Coverage for Moto #9759
85+
)
8486
fn_name = str(uuid.uuid4())[0:6]
8587
conn = boto3.client("lambda", region_name="us-east-1")
8688
func = conn.create_function(
@@ -119,7 +121,11 @@ def test_invoke_function_from_sqs_fifo_queue():
119121
queue_name = str(uuid.uuid4())[0:6] + ".fifo"
120122
queue = sqs.create_queue(
121123
QueueName=queue_name,
122-
Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"},
124+
Attributes={
125+
"FifoQueue": "true",
126+
"ContentBasedDeduplication": "true",
127+
"ReceiveMessageWaitTimeSeconds": "2", # Coverage for Moto #9759
128+
},
123129
)
124130

125131
fn_name = str(uuid.uuid4())[0:6]

0 commit comments

Comments
 (0)