2017-03-19 15:03:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-08-27 15:17:06 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-02-12 00:41:04 +00:00
|
|
|
|
2013-02-24 03:26:46 +00:00
|
|
|
import boto
|
2015-08-02 13:45:40 +00:00
|
|
|
import boto3
|
2016-06-01 14:39:06 +00:00
|
|
|
import botocore.exceptions
|
2013-02-24 03:26:46 +00:00
|
|
|
from boto.exception import SQSError
|
2015-05-30 03:26:43 +00:00
|
|
|
from boto.sqs.message import RawMessage, Message
|
2014-06-20 21:29:40 +00:00
|
|
|
|
2013-02-28 02:56:54 +00:00
|
|
|
import requests
|
2013-08-03 21:21:25 +00:00
|
|
|
import sure # noqa
|
2014-06-20 20:00:36 +00:00
|
|
|
import time
|
2013-02-24 03:26:46 +00:00
|
|
|
|
2017-03-05 15:09:19 +00:00
|
|
|
from moto import settings, mock_sqs, mock_sqs_deprecated
|
2014-09-28 19:59:14 +00:00
|
|
|
from tests.helpers import requires_boto_gte
|
2017-02-12 00:41:04 +00:00
|
|
|
import tests.backport_assert_raises # noqa
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2013-02-24 03:26:46 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_create_queue():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:41:04 +00:00
|
|
|
new_queue = sqs.create_queue(QueueName='test-queue')
|
|
|
|
new_queue.should_not.be.none
|
|
|
|
new_queue.should.have.property('url').should.contain('test-queue')
|
|
|
|
|
|
|
|
queue = sqs.get_queue_by_name(QueueName='test-queue')
|
|
|
|
queue.attributes.get('QueueArn').should_not.be.none
|
|
|
|
queue.attributes.get('QueueArn').split(':')[-1].should.equal('test-queue')
|
|
|
|
queue.attributes.get('QueueArn').split(':')[3].should.equal('us-east-1')
|
|
|
|
queue.attributes.get('VisibilityTimeout').should_not.be.none
|
|
|
|
queue.attributes.get('VisibilityTimeout').should.equal('30')
|
2013-02-24 03:26:46 +00:00
|
|
|
|
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_get_inexistent_queue():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-24 02:37:43 +00:00
|
|
|
sqs.get_queue_by_name.when.called_with(
|
|
|
|
QueueName='nonexisting-queue').should.throw(botocore.exceptions.ClientError)
|
2013-02-24 03:26:46 +00:00
|
|
|
|
|
|
|
|
2014-11-16 22:57:46 +00:00
|
|
|
@mock_sqs
|
2017-02-12 00:41:04 +00:00
|
|
|
def test_message_send():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:41:04 +00:00
|
|
|
queue = sqs.create_queue(QueueName="blah")
|
|
|
|
msg = queue.send_message(MessageBody="derp")
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
msg.get('MD5OfMessageBody').should.equal(
|
|
|
|
'58fd9edd83341c29f1aebba81c31e257')
|
|
|
|
msg.get('ResponseMetadata', {}).get('RequestId').should.equal(
|
|
|
|
'27daac76-34dd-47df-bd01-1f6e873584a0')
|
2017-02-12 00:41:04 +00:00
|
|
|
msg.get('MessageId').should_not.contain(' \n')
|
|
|
|
|
|
|
|
messages = queue.receive_messages()
|
|
|
|
messages.should.have.length_of(1)
|
|
|
|
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2017-03-19 15:03:22 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_send_message_with_unicode_characters():
|
|
|
|
body_one = 'Héllo!😀'
|
|
|
|
|
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
|
|
|
queue = sqs.create_queue(QueueName="blah")
|
|
|
|
msg = queue.send_message(MessageBody=body_one)
|
|
|
|
|
|
|
|
messages = queue.receive_messages()
|
|
|
|
message_body = messages[0].body
|
|
|
|
|
|
|
|
message_body.should.equal(body_one)
|
|
|
|
|
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_set_queue_attributes():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:41:04 +00:00
|
|
|
queue = sqs.create_queue(QueueName="blah")
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
queue.attributes['VisibilityTimeout'].should.equal("30")
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
queue.set_attributes(Attributes={"VisibilityTimeout": "45"})
|
|
|
|
queue.attributes['VisibilityTimeout'].should.equal("45")
|
2015-09-19 13:18:16 +00:00
|
|
|
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2013-04-19 03:07:24 +00:00
|
|
|
@mock_sqs
|
2017-02-12 00:41:04 +00:00
|
|
|
def test_create_queues_in_multiple_region():
|
|
|
|
west1_conn = boto3.client('sqs', region_name='us-west-1')
|
|
|
|
west1_conn.create_queue(QueueName="blah")
|
|
|
|
|
|
|
|
west2_conn = boto3.client('sqs', region_name='us-west-2')
|
|
|
|
west2_conn.create_queue(QueueName="test-queue")
|
2013-04-19 03:07:24 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
list(west1_conn.list_queues()['QueueUrls']).should.have.length_of(1)
|
|
|
|
list(west2_conn.list_queues()['QueueUrls']).should.have.length_of(1)
|
2013-04-19 03:07:24 +00:00
|
|
|
|
2017-03-05 15:09:19 +00:00
|
|
|
if settings.TEST_SERVER_MODE:
|
2017-03-13 01:04:19 +00:00
|
|
|
base_url = 'http://localhost:5000'
|
2017-03-05 15:09:19 +00:00
|
|
|
else:
|
|
|
|
base_url = 'https://us-west-1.queue.amazonaws.com'
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
west1_conn.list_queues()['QueueUrls'][0].should.equal(
|
2017-03-05 15:09:19 +00:00
|
|
|
'{base_url}/123456789012/blah'.format(base_url=base_url))
|
2013-04-24 07:41:45 +00:00
|
|
|
|
2013-04-19 03:07:24 +00:00
|
|
|
|
2014-02-20 19:00:08 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_get_queue_with_prefix():
|
2017-02-12 00:41:04 +00:00
|
|
|
conn = boto3.client("sqs", region_name='us-west-1')
|
|
|
|
conn.create_queue(QueueName="prefixa-queue")
|
|
|
|
conn.create_queue(QueueName="prefixb-queue")
|
|
|
|
conn.create_queue(QueueName="test-queue")
|
2014-02-20 19:00:08 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
conn.list_queues()['QueueUrls'].should.have.length_of(3)
|
2014-02-20 19:00:08 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
queue = conn.list_queues(QueueNamePrefix="test-")['QueueUrls']
|
2014-02-20 19:00:08 +00:00
|
|
|
queue.should.have.length_of(1)
|
2017-03-05 15:09:19 +00:00
|
|
|
|
|
|
|
if settings.TEST_SERVER_MODE:
|
2017-03-13 01:04:19 +00:00
|
|
|
base_url = 'http://localhost:5000'
|
2017-03-05 15:09:19 +00:00
|
|
|
else:
|
|
|
|
base_url = 'https://us-west-1.queue.amazonaws.com'
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
queue[0].should.equal(
|
2017-03-05 15:09:19 +00:00
|
|
|
"{base_url}/123456789012/test-queue".format(base_url=base_url))
|
2014-02-20 19:00:08 +00:00
|
|
|
|
|
|
|
|
2013-02-24 03:26:46 +00:00
|
|
|
@mock_sqs
|
|
|
|
def test_delete_queue():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:50:26 +00:00
|
|
|
conn = boto3.client("sqs", region_name='us-east-1')
|
2017-02-24 02:37:43 +00:00
|
|
|
conn.create_queue(QueueName="test-queue",
|
|
|
|
Attributes={"VisibilityTimeout": "60"})
|
2017-02-12 00:41:04 +00:00
|
|
|
queue = sqs.Queue('test-queue')
|
2013-02-24 03:26:46 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
conn.list_queues()['QueueUrls'].should.have.length_of(1)
|
2013-02-24 03:26:46 +00:00
|
|
|
|
|
|
|
queue.delete()
|
2017-02-12 00:41:04 +00:00
|
|
|
conn.list_queues().get('QueueUrls').should.equal(None)
|
2013-02-24 03:26:46 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
with assert_raises(botocore.exceptions.ClientError):
|
|
|
|
queue.delete()
|
2013-02-24 03:35:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
def test_set_queue_attribute():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:50:26 +00:00
|
|
|
conn = boto3.client("sqs", region_name='us-east-1')
|
2017-02-24 02:37:43 +00:00
|
|
|
conn.create_queue(QueueName="test-queue",
|
|
|
|
Attributes={"VisibilityTimeout": '60'})
|
2013-02-24 03:35:05 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
queue = sqs.Queue("test-queue")
|
|
|
|
queue.attributes['VisibilityTimeout'].should.equal('60')
|
2013-02-24 03:35:05 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
queue.set_attributes(Attributes={"VisibilityTimeout": '45'})
|
|
|
|
queue = sqs.Queue("test-queue")
|
|
|
|
queue.attributes['VisibilityTimeout'].should.equal('45')
|
2013-02-24 16:06:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_sqs
|
|
|
|
def test_send_message():
|
2017-02-16 03:35:45 +00:00
|
|
|
sqs = boto3.resource('sqs', region_name='us-east-1')
|
2017-02-12 00:50:26 +00:00
|
|
|
conn = boto3.client("sqs", region_name='us-east-1')
|
2017-02-12 00:41:04 +00:00
|
|
|
conn.create_queue(QueueName="test-queue")
|
|
|
|
queue = sqs.Queue("test-queue")
|
2014-06-23 18:53:46 +00:00
|
|
|
|
|
|
|
body_one = 'this is a test message'
|
|
|
|
body_two = 'this is another test message'
|
2013-02-24 16:06:42 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
response = queue.send_message(MessageBody=body_one)
|
|
|
|
response = queue.send_message(MessageBody=body_two)
|
2013-02-24 16:06:42 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
messages = conn.receive_message(
|
|
|
|
QueueUrl=queue.url, MaxNumberOfMessages=2)['Messages']
|
2014-06-23 18:53:46 +00:00
|
|
|
|
2017-02-12 00:41:04 +00:00
|
|
|
messages[0]['Body'].should.equal(body_one)
|
|
|
|
messages[1]['Body'].should.equal(body_two)
|
2014-06-20 20:00:36 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-10-22 20:03:42 +00:00
|
|
|
def test_send_message_with_xml_characters():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
body_one = '< & >'
|
|
|
|
|
|
|
|
queue.write(queue.new_message(body_one))
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
|
|
|
|
messages[0].get_body().should.equal(body_one)
|
|
|
|
|
|
|
|
|
2014-09-28 19:59:14 +00:00
|
|
|
@requires_boto_gte("2.28")
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-09-28 19:59:14 +00:00
|
|
|
def test_send_message_with_attributes():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
queue.set_message_class(RawMessage)
|
2014-11-16 22:57:46 +00:00
|
|
|
|
2014-09-28 19:59:14 +00:00
|
|
|
body = 'this is a test message'
|
|
|
|
message = queue.new_message(body)
|
|
|
|
message_attributes = {
|
2014-11-16 22:57:46 +00:00
|
|
|
'test.attribute_name': {'data_type': 'String', 'string_value': 'attribute value'},
|
|
|
|
'test.binary_attribute': {'data_type': 'Binary', 'binary_value': 'binary value'},
|
|
|
|
'test.number_attribute': {'data_type': 'Number', 'string_value': 'string value'}
|
2014-09-28 19:59:14 +00:00
|
|
|
}
|
|
|
|
message.message_attributes = message_attributes
|
|
|
|
|
|
|
|
queue.write(message)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue)
|
|
|
|
|
|
|
|
messages[0].get_body().should.equal(body)
|
|
|
|
|
|
|
|
for name, value in message_attributes.items():
|
|
|
|
dict(messages[0].message_attributes[name]).should.equal(value)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-20 20:00:36 +00:00
|
|
|
def test_send_message_with_delay():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2014-06-20 20:00:36 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
body_one = 'this is a test message'
|
|
|
|
body_two = 'this is another test message'
|
|
|
|
|
|
|
|
queue.write(queue.new_message(body_one), delay_seconds=60)
|
|
|
|
queue.write(queue.new_message(body_two))
|
2014-06-20 20:00:36 +00:00
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=2)
|
|
|
|
assert len(messages) == 1
|
|
|
|
message = messages[0]
|
2014-06-23 18:53:46 +00:00
|
|
|
assert message.get_body().should.equal(body_two)
|
2014-06-20 20:00:36 +00:00
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2015-11-10 16:24:55 +00:00
|
|
|
def test_send_large_message_fails():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
2015-11-10 16:39:00 +00:00
|
|
|
body_one = 'test message' * 200000
|
2015-11-10 16:24:55 +00:00
|
|
|
huge_message = queue.new_message(body_one)
|
|
|
|
|
|
|
|
queue.write.when.called_with(huge_message).should.throw(SQSError)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-22 18:34:32 +00:00
|
|
|
def test_message_becomes_inflight_when_received():
|
2014-06-20 20:00:36 +00:00
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=2)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2014-06-20 20:00:36 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
body_one = 'this is a test message'
|
|
|
|
queue.write(queue.new_message(body_one))
|
2014-06-20 20:00:36 +00:00
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
# Wait
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2016-05-31 21:58:37 +00:00
|
|
|
def test_receive_message_with_explicit_visibility_timeout():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
body_one = 'this is another test message'
|
|
|
|
queue.write(queue.new_message(body_one))
|
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
2017-02-24 02:37:43 +00:00
|
|
|
messages = conn.receive_message(
|
|
|
|
queue, number_messages=1, visibility_timeout=0)
|
2016-05-31 21:58:37 +00:00
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
# Message should remain visible
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-20 20:00:36 +00:00
|
|
|
def test_change_message_visibility():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=2)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
body_one = 'this is another test message'
|
|
|
|
queue.write(queue.new_message(body_one))
|
2014-06-20 20:00:36 +00:00
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
messages[0].change_visibility(2)
|
|
|
|
|
|
|
|
# Wait
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Message is not visible
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
# Message now becomes visible
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
messages[0].delete()
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-20 20:00:36 +00:00
|
|
|
def test_message_attributes():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=2)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
body_one = 'this is another test message'
|
|
|
|
queue.write(queue.new_message(body_one))
|
2014-06-20 20:00:36 +00:00
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
message_attributes = messages[0].attributes
|
|
|
|
|
|
|
|
assert message_attributes.get('ApproximateFirstReceiveTimestamp')
|
|
|
|
assert int(message_attributes.get('ApproximateReceiveCount')) == 1
|
|
|
|
assert message_attributes.get('SentTimestamp')
|
|
|
|
assert message_attributes.get('SenderId')
|
2013-02-24 16:06:42 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-03-23 13:44:53 +00:00
|
|
|
def test_read_message_from_queue():
|
|
|
|
conn = boto.connect_sqs()
|
|
|
|
queue = conn.create_queue('testqueue')
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
body = 'foo bar baz'
|
|
|
|
queue.write(queue.new_message(body))
|
2013-03-23 13:44:53 +00:00
|
|
|
message = queue.read(1)
|
2014-06-23 18:53:46 +00:00
|
|
|
message.get_body().should.equal(body)
|
2013-03-23 13:44:53 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-02-24 16:06:42 +00:00
|
|
|
def test_queue_length():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2013-02-24 16:06:42 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.write(queue.new_message('this is a test message'))
|
|
|
|
queue.write(queue.new_message('this is another test message'))
|
2013-02-24 16:06:42 +00:00
|
|
|
queue.count().should.equal(2)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-02-24 16:06:42 +00:00
|
|
|
def test_delete_message():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2013-02-24 16:06:42 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.write(queue.new_message('this is a test message'))
|
|
|
|
queue.write(queue.new_message('this is another test message'))
|
2014-06-20 20:00:36 +00:00
|
|
|
queue.count().should.equal(2)
|
2013-02-24 16:06:42 +00:00
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
2014-06-20 20:00:36 +00:00
|
|
|
assert len(messages) == 1
|
2013-02-24 16:06:42 +00:00
|
|
|
messages[0].delete()
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
2014-06-20 20:00:36 +00:00
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
assert len(messages) == 1
|
|
|
|
messages[0].delete()
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
2013-02-24 16:06:42 +00:00
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-02-24 16:06:42 +00:00
|
|
|
def test_send_batch_operation():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
|
2013-03-23 13:44:53 +00:00
|
|
|
# See https://github.com/boto/boto/issues/831
|
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
|
|
|
queue.write_batch([
|
2013-02-24 16:06:42 +00:00
|
|
|
("my_first_message", 'test message 1', 0),
|
|
|
|
("my_second_message", 'test message 2', 0),
|
|
|
|
("my_third_message", 'test message 3', 0),
|
|
|
|
])
|
|
|
|
|
|
|
|
messages = queue.get_messages(3)
|
|
|
|
messages[0].get_body().should.equal("test message 1")
|
|
|
|
|
|
|
|
# Test that pulling more messages doesn't break anything
|
|
|
|
messages = queue.get_messages(2)
|
|
|
|
|
|
|
|
|
2014-09-28 19:59:14 +00:00
|
|
|
@requires_boto_gte("2.28")
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-09-28 19:59:14 +00:00
|
|
|
def test_send_batch_operation_with_message_attributes():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
queue.set_message_class(RawMessage)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
message_tuple = ("my_first_message", 'test message 1', 0, {
|
|
|
|
'name1': {'data_type': 'String', 'string_value': 'foo'}})
|
2014-09-28 19:59:14 +00:00
|
|
|
queue.write_batch([message_tuple])
|
|
|
|
|
|
|
|
messages = queue.get_messages()
|
|
|
|
messages[0].get_body().should.equal("test message 1")
|
|
|
|
|
|
|
|
for name, value in message_tuple[3].items():
|
|
|
|
dict(messages[0].message_attributes[name]).should.equal(value)
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-02-24 16:06:42 +00:00
|
|
|
def test_delete_batch_operation():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=60)
|
|
|
|
|
|
|
|
conn.send_message_batch(queue, [
|
|
|
|
("my_first_message", 'test message 1', 0),
|
|
|
|
("my_second_message", 'test message 2', 0),
|
|
|
|
("my_third_message", 'test message 3', 0),
|
|
|
|
])
|
|
|
|
|
|
|
|
messages = queue.get_messages(2)
|
|
|
|
queue.delete_message_batch(messages)
|
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
2013-02-28 02:56:54 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2013-04-24 06:53:30 +00:00
|
|
|
def test_queue_attributes():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
|
|
|
|
queue_name = 'test-queue'
|
|
|
|
visibility_timeout = 60
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
queue = conn.create_queue(
|
|
|
|
queue_name, visibility_timeout=visibility_timeout)
|
2013-04-24 06:53:30 +00:00
|
|
|
|
|
|
|
attributes = queue.get_attributes()
|
|
|
|
|
|
|
|
attributes['QueueArn'].should.look_like(
|
2016-07-19 12:20:33 +00:00
|
|
|
'arn:aws:sqs:us-east-1:123456789012:%s' % queue_name)
|
2013-04-24 06:53:30 +00:00
|
|
|
|
|
|
|
attributes['VisibilityTimeout'].should.look_like(str(visibility_timeout))
|
|
|
|
|
|
|
|
attribute_names = queue.get_attributes().keys()
|
|
|
|
attribute_names.should.contain('ApproximateNumberOfMessagesNotVisible')
|
|
|
|
attribute_names.should.contain('MessageRetentionPeriod')
|
|
|
|
attribute_names.should.contain('ApproximateNumberOfMessagesDelayed')
|
|
|
|
attribute_names.should.contain('MaximumMessageSize')
|
|
|
|
attribute_names.should.contain('CreatedTimestamp')
|
|
|
|
attribute_names.should.contain('ApproximateNumberOfMessages')
|
|
|
|
attribute_names.should.contain('ReceiveMessageWaitTimeSeconds')
|
|
|
|
attribute_names.should.contain('DelaySeconds')
|
|
|
|
attribute_names.should.contain('VisibilityTimeout')
|
|
|
|
attribute_names.should.contain('LastModifiedTimestamp')
|
|
|
|
attribute_names.should.contain('QueueArn')
|
2014-06-20 21:29:40 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-20 21:29:40 +00:00
|
|
|
def test_change_message_visibility_on_invalid_receipt():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=1)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2014-06-20 21:29:40 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.write(queue.new_message('this is another test message'))
|
2014-06-20 21:29:40 +00:00
|
|
|
queue.count().should.equal(1)
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
original_message = messages[0]
|
|
|
|
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
original_message.change_visibility.when.called_with(
|
|
|
|
100).should.throw(SQSError)
|
2014-06-20 21:29:40 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2014-06-20 21:29:40 +00:00
|
|
|
def test_change_message_visibility_on_visible_message():
|
|
|
|
conn = boto.connect_sqs('the_key', 'the_secret')
|
|
|
|
queue = conn.create_queue("test-queue", visibility_timeout=1)
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.set_message_class(RawMessage)
|
2014-06-20 21:29:40 +00:00
|
|
|
|
2014-06-23 18:53:46 +00:00
|
|
|
queue.write(queue.new_message('this is another test message'))
|
2014-06-20 21:29:40 +00:00
|
|
|
queue.count().should.equal(1)
|
|
|
|
messages = conn.receive_message(queue, number_messages=1)
|
|
|
|
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
original_message = messages[0]
|
|
|
|
|
|
|
|
queue.count().should.equal(0)
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
original_message.change_visibility.when.called_with(
|
|
|
|
100).should.throw(SQSError)
|
2015-05-30 03:21:14 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2015-05-30 03:21:14 +00:00
|
|
|
def test_purge_action():
|
|
|
|
conn = boto.sqs.connect_to_region("us-east-1")
|
|
|
|
|
|
|
|
queue = conn.create_queue('new-queue')
|
|
|
|
queue.write(queue.new_message('this is another test message'))
|
|
|
|
queue.count().should.equal(1)
|
|
|
|
|
|
|
|
queue.purge()
|
|
|
|
|
|
|
|
queue.count().should.equal(0)
|
2015-05-30 03:26:43 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_sqs_deprecated
|
2015-05-30 03:26:43 +00:00
|
|
|
def test_delete_message_after_visibility_timeout():
|
|
|
|
VISIBILITY_TIMEOUT = 1
|
|
|
|
conn = boto.sqs.connect_to_region("us-east-1")
|
2017-02-24 02:37:43 +00:00
|
|
|
new_queue = conn.create_queue(
|
|
|
|
'new-queue', visibility_timeout=VISIBILITY_TIMEOUT)
|
2015-05-30 03:26:43 +00:00
|
|
|
|
|
|
|
m1 = Message()
|
|
|
|
m1.set_body('Message 1!')
|
|
|
|
new_queue.write(m1)
|
|
|
|
|
|
|
|
assert new_queue.count() == 1
|
|
|
|
|
|
|
|
m1_retrieved = new_queue.read()
|
|
|
|
|
|
|
|
time.sleep(VISIBILITY_TIMEOUT + 1)
|
|
|
|
|
|
|
|
m1_retrieved.delete()
|
|
|
|
|
|
|
|
assert new_queue.count() == 0
|