moto
moto copied to clipboard
SQS receive_messages fails to get newly visible messages
Calls to the SQS queue receive_messages method, with a non-zero WaitTimeSeconds, fails when there is a message that becomes visible after the call was made. The following test code fails for moto >= 4.0.10.dev9:
import moto
import boto3
@moto.mock_sqs
def test_visibility():
sqs = boto3.resource("sqs")
queue = sqs.create_queue(QueueName="test-queue")
queue.send_message(MessageBody="test-message")
message_1 = queue.receive_messages()
message_1[0].change_visibility(VisibilityTimeout=1)
message_2 = queue.receive_messages(WaitTimeSeconds=2)
assert len(message_2) > 0
test_visibility()
I think the issue was introduced in this commit. The code used to loop every ~0.01 seconds, and in each loop it checked whether messages had become visible. I don't think the change to use a conditional works because there is no call to notify_all() when a message becomes visible (in a separate thread). The notify_all() is only called when messages are added in add_message().
@philipnbbc Good catch. I'll get a fix out for this shortly.