From 7033d142a006256dbfabcc6a0737e2011fd98d5e Mon Sep 17 00:00:00 2001 From: brett55 Date: Thu, 1 Jun 2017 09:51:33 -0600 Subject: [PATCH] - Bug fix for infinite loop when WaitTimeSeconds was set to 0 - Added 2 unit tests --- moto/sqs/models.py | 6 ++++++ tests/test_sqs/test_sqs.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/moto/sqs/models.py b/moto/sqs/models.py index d2c538ecb1c4..43a633c42619 100644 --- a/moto/sqs/models.py +++ b/moto/sqs/models.py @@ -334,6 +334,8 @@ def receive_messages(self, queue_name, count, wait_seconds_timeout, visibility_t :param string queue_name: The name of the queue to read from. :param int count: The maximum amount of messages to retrieve. :param int visibility_timeout: The number of seconds the message should remain invisible to other queue readers. + :param int wait_seconds_timeout: The duration (in seconds) for which the call waits for a message to arrive in + the queue before returning. If a message is available, the call returns sooner than WaitTimeSeconds """ queue = self.get_queue(queue_name) result = [] @@ -347,6 +349,10 @@ def receive_messages(self, queue_name, count, wait_seconds_timeout, visibility_t break if len(queue.messages) == 0: + # we want to break here, otherwise it will be an infinite loop + if wait_seconds_timeout == 0: + break + import time time.sleep(0.001) continue diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index bc1f5d7bda05..b01a5540633c 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -208,6 +208,35 @@ def test_send_message(): messages[1]['Body'].should.equal(body_two) +@mock_sqs +def test_receive_messages_with_wait_seconds_timeout_of_zero(): + """ + test that zero messages is returned with a wait_seconds_timeout of zero, + previously this created an infinite loop and nothing was returned + :return: + """ + + sqs = boto3.resource('sqs', region_name='us-east-1') + queue = sqs.create_queue(QueueName="blah") + + messages = queue.receive_messages(WaitTimeSeconds=0) + messages.should.equal([]) + + +@mock_sqs +def test_receive_messages_with_wait_seconds_timeout_of_negative_one(): + """ + test that zero messages is returned with a wait_seconds_timeout of negative 1 + :return: + """ + + sqs = boto3.resource('sqs', region_name='us-east-1') + queue = sqs.create_queue(QueueName="blah") + + messages = queue.receive_messages(WaitTimeSeconds=-1) + messages.should.equal([]) + + @mock_sqs_deprecated def test_send_message_with_xml_characters(): conn = boto.connect_sqs('the_key', 'the_secret')