2023-08-12 07:10:50 +00:00
|
|
|
"""Test different server responses."""
|
2022-11-05 13:09:58 +00:00
|
|
|
import datetime
|
2013-03-05 13:14:43 +00:00
|
|
|
import re
|
2015-08-10 20:23:17 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2013-03-05 13:14:43 +00:00
|
|
|
|
|
|
|
import moto.server as server
|
|
|
|
|
|
|
|
|
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")
|
2023-08-12 07:10:50 +00:00
|
|
|
assert b"ListQueuesResponse" in res.data
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2017-08-05 05:47:40 +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"
|
2023-01-07 11:35:14 +00:00
|
|
|
# See: https://github.com/getmoto/moto/issues/866
|
2017-08-05 05:47:40 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
for queue_name in ("testqueue", "otherqueue.fifo"):
|
2014-02-20 19:00:08 +00:00
|
|
|
|
2022-11-17 22:41:08 +00:00
|
|
|
res = test_client.put(f"/?Action=CreateQueue&QueueName={queue_name}")
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2017-08-05 05:47:40 +00:00
|
|
|
res = test_client.put(
|
2022-11-17 22:41:08 +00:00
|
|
|
f"/123/{queue_name}?MessageBody=test-message&Action=SendMessage"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2017-08-05 05:47:40 +00:00
|
|
|
res = test_client.get(
|
2022-11-17 22:41:08 +00:00
|
|
|
f"/123/{queue_name}?Action=ReceiveMessage&MaxNumberOfMessages=1"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2014-06-20 20:00:36 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
message = re.search("<Body>(.*?)</Body>", res.data.decode("utf-8")).groups()[0]
|
2023-08-12 07:10:50 +00:00
|
|
|
assert message == "test-message"
|
2017-08-05 05:47:40 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/?Action=ListQueues&QueueNamePrefix=other")
|
2023-08-12 07:10:50 +00:00
|
|
|
assert b"otherqueue.fifo" in res.data
|
|
|
|
assert b"testqueue" not in res.data
|
2015-08-10 20:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2015-08-10 20:23:17 +00:00
|
|
|
|
|
|
|
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"
|
2015-08-10 20:23:17 +00:00
|
|
|
)
|
|
|
|
messages_count -= 1
|
2019-10-31 15:44:26 +00:00
|
|
|
time.sleep(0.5)
|
2015-08-10 20:23:17 +00:00
|
|
|
|
|
|
|
def get_messages():
|
2015-08-31 15:54:17 +00:00
|
|
|
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"
|
2015-08-31 15:54:17 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
new_msgs = re.findall("<Body>(.*?)</Body>", msg_res.data.decode("utf-8"))
|
2015-08-31 15:54:17 +00:00
|
|
|
count += len(new_msgs)
|
|
|
|
messages.append(new_msgs)
|
2015-08-10 20:23:17 +00:00
|
|
|
|
|
|
|
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
|
2015-08-10 20:23:17 +00:00
|
|
|
assert len(messages) == 5
|
2022-11-05 13:09:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_messages_polling_timeout():
|
|
|
|
backend = server.create_backend_app("sqs")
|
|
|
|
queue_name = "test-queue"
|
|
|
|
test_client = backend.test_client()
|
|
|
|
test_client.put(f"/?Action=CreateQueue&QueueName={queue_name}")
|
|
|
|
wait_seconds = 5
|
|
|
|
start = datetime.datetime.utcnow()
|
|
|
|
test_client.get(
|
2023-08-12 07:10:50 +00:00
|
|
|
f"/123/{queue_name}?Action=ReceiveMessage&"
|
|
|
|
f"MaxNumberOfMessages=1&WaitTimeSeconds={wait_seconds}"
|
2022-11-05 13:09:58 +00:00
|
|
|
)
|
|
|
|
end = datetime.datetime.utcnow()
|
|
|
|
duration = end - start
|
|
|
|
assert duration.seconds >= wait_seconds
|
|
|
|
assert duration.seconds <= wait_seconds + (wait_seconds / 2)
|