moto/tests/test_sqs/test_server.py

85 lines
2.6 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-03-05 13:14:43 +00:00
import re
2013-08-03 21:21:25 +00:00
import sure # noqa
import threading
import time
2013-03-05 13:14:43 +00:00
import moto.server as server
2019-10-31 15:44:26 +00:00
"""
2013-03-05 13:14:43 +00:00
Test the different server responses
2019-10-31 15:44:26 +00:00
"""
2013-03-05 13:14:43 +00:00
2013-03-23 13:44:53 +00:00
def test_sqs_list_identities():
2013-12-29 01:15:37 +00:00
backend = server.create_backend_app("sqs")
test_client = backend.test_client()
2019-10-31 15:44:26 +00:00
res = test_client.get("/?Action=ListQueues")
2014-08-26 17:25:50 +00:00
res.data.should.contain(b"ListQueuesResponse")
2013-03-05 13:14:43 +00:00
# Make sure that we can receive messages from queues whose name contains dots (".")
# The AWS API mandates that the names of FIFO queues use the suffix ".fifo"
# See: https://github.com/spulec/moto/issues/866
2019-10-31 15:44:26 +00:00
for queue_name in ("testqueue", "otherqueue.fifo"):
2019-10-31 15:44:26 +00:00
res = test_client.put("/?Action=CreateQueue&QueueName=%s" % queue_name)
2013-03-05 13:14:43 +00:00
res = test_client.put(
2019-10-31 15:44:26 +00:00
"/123/%s?MessageBody=test-message&Action=SendMessage" % queue_name
)
2013-03-05 13:14:43 +00:00
res = test_client.get(
2019-10-31 15:44:26 +00:00
"/123/%s?Action=ReceiveMessage&MaxNumberOfMessages=1" % queue_name
)
2019-10-31 15:44:26 +00:00
message = re.search("<Body>(.*?)</Body>", res.data.decode("utf-8")).groups()[0]
message.should.equal("test-message")
2019-10-31 15:44:26 +00:00
res = test_client.get("/?Action=ListQueues&QueueNamePrefix=other")
res.data.should.contain(b"otherqueue.fifo")
res.data.should_not.contain(b"testqueue")
def test_messages_polling():
backend = server.create_backend_app("sqs")
test_client = backend.test_client()
messages = []
2019-10-31 15:44:26 +00:00
test_client.put("/?Action=CreateQueue&QueueName=testqueue")
def insert_messages():
messages_count = 5
while messages_count > 0:
test_client.put(
2019-10-31 15:44:26 +00:00
"/123/testqueue?MessageBody=test-message&Action=SendMessage"
"&Attribute.1.Name=WaitTimeSeconds&Attribute.1.Value=10"
)
messages_count -= 1
2019-10-31 15:44:26 +00:00
time.sleep(0.5)
def get_messages():
count = 0
while count < 5:
msg_res = test_client.get(
2019-10-31 15:44:26 +00:00
"/123/testqueue?Action=ReceiveMessage&MaxNumberOfMessages=1&WaitTimeSeconds=5"
)
2019-10-31 15:44:26 +00:00
new_msgs = re.findall("<Body>(.*?)</Body>", msg_res.data.decode("utf-8"))
count += len(new_msgs)
messages.append(new_msgs)
get_messages_thread = threading.Thread(target=get_messages)
insert_messages_thread = threading.Thread(target=insert_messages)
get_messages_thread.start()
insert_messages_thread.start()
get_messages_thread.join()
insert_messages_thread.join()
2017-02-24 02:37:43 +00:00
# got each message in a separate call to ReceiveMessage, despite the long
# WaitTimeSeconds
assert len(messages) == 5