Beispiel #1
0
class test_AsyncMessage(AWSCase):

    def setup(self):
        self.queue = Mock(name='queue')
        self.callback = PromiseMock(name='callback')
        self.x = AsyncMessage(self.queue, 'body')
        self.x.receipt_handle = uuid()

    def test_delete(self):
        self.assertTrue(self.x.delete(callback=self.callback))
        self.x.queue.delete_message.assert_called_with(
            self.x, self.callback,
        )

        self.x.queue = None
        self.assertIsNone(self.x.delete(callback=self.callback))

    def test_change_visibility(self):
        self.assertTrue(self.x.change_visibility(303, callback=self.callback))
        self.x.queue.connection.change_message_visibility.assert_called_with(
            self.x.queue, self.x.receipt_handle, 303, self.callback,
        )
        self.x.queue = None
        self.assertIsNone(self.x.change_visibility(
            303, callback=self.callback,
        ))
Beispiel #2
0
class test_AsyncMessage(AWSCase):
    def setup(self):
        self.queue = Mock(name='queue')
        self.callback = PromiseMock(name='callback')
        self.x = AsyncMessage(self.queue, 'body')
        self.x.receipt_handle = uuid()

    def test_delete(self):
        self.assertTrue(self.x.delete(callback=self.callback))
        self.x.queue.delete_message.assert_called_with(
            self.x,
            self.callback,
        )

        self.x.queue = None
        self.assertIsNone(self.x.delete(callback=self.callback))

    def test_change_visibility(self):
        self.assertTrue(self.x.change_visibility(303, callback=self.callback))
        self.x.queue.connection.change_message_visibility.assert_called_with(
            self.x.queue,
            self.x.receipt_handle,
            303,
            self.callback,
        )
        self.x.queue = None
        self.assertIsNone(
            self.x.change_visibility(
                303,
                callback=self.callback,
            ))
Beispiel #3
0
    def _get(self, queue):
        """Try to retrieve a single message off ``queue``."""
        q_url = self._new_queue(queue)
        resp = self.sqs.receive_message(q_url)

        if resp['Messages']:
            body = AsyncMessage(body=resp['Messages'][0]['Body']).decode()
            resp['Messages'][0]['Body'] = body
            return self._messages_to_python(resp['Messages'], queue)[0]
        raise Empty()
Beispiel #4
0
 def _get(self, queue):
     """Try to retrieve a single message off ``queue``."""
     q_url = self._new_queue(queue)
     resp = self.sqs.receive_message(QueueUrl=q_url,
                                     MaxNumberOfMessages=1,
                                     WaitTimeSeconds=self.wait_time_seconds)
     if resp.get('Messages'):
         body = AsyncMessage(body=resp['Messages'][0]['Body']).decode()
         resp['Messages'][0]['Body'] = body
         return self._messages_to_python(resp['Messages'], queue)[0]
     raise Empty()
Beispiel #5
0
 def _put(self, queue, message, **kwargs):
     """Put message onto queue."""
     q_url = self._new_queue(queue)
     kwargs = {'QueueUrl': q_url,
               'MessageBody': AsyncMessage().encode(dumps(message))}
     if queue.endswith('.fifo'):
         if 'MessageGroupId' in message['properties']:
             kwargs['MessageGroupId'] = \
                 message['properties']['MessageGroupId']
         else:
             kwargs['MessageGroupId'] = 'default'
         if 'MessageDeduplicationId' in message['properties']:
             kwargs['MessageDeduplicationId'] = \
                 message['properties']['MessageDeduplicationId']
         else:
             kwargs['MessageDeduplicationId'] = str(uuid.uuid4())
     self.sqs.send_message(**kwargs)
Beispiel #6
0
    def _get_bulk(self,
                  queue,
                  max_if_unlimited=SQS_MAX_MESSAGES,
                  callback=None):
        """Try to retrieve multiple messages off ``queue``.

        Where :meth:`_get` returns a single Payload object, this method
        returns a list of Payload objects.  The number of objects returned
        is determined by the total number of messages available in the queue
        and the number of messages the QoS object allows (based on the
        prefetch_count).

        Note:
            Ignores QoS limits so caller is responsible for checking
            that we are allowed to consume at least one message from the
            queue.  get_bulk will then ask QoS for an estimate of
            the number of extra messages that we can consume.

        Arguments:
            queue (str): The queue name to pull from.

        Returns:
            List[Message]
        """
        # drain_events calls `can_consume` first, consuming
        # a token, so we know that we are allowed to consume at least
        # one message.

        # Note: ignoring max_messages for SQS with boto3
        max_count = self._get_message_estimate()
        if max_count:
            q_url = self._new_queue(queue)
            resp = self.sqs.receive_message(
                QueueUrl=q_url,
                MaxNumberOfMessages=max_count,
                WaitTimeSeconds=self.wait_time_seconds)
            if resp.get('Messages'):
                for m in resp['Messages']:
                    m['Body'] = AsyncMessage(body=m['Body']).decode()
                for msg in self._messages_to_python(resp['Messages'], queue):
                    self.connection._deliver(msg, queue)
                return
        raise Empty()
Beispiel #7
0
 def setup(self):
     self.queue = Mock(name='queue')
     self.callback = PromiseMock(name='callback')
     self.x = AsyncMessage(self.queue, 'body')
     self.x.receipt_handle = uuid()
Beispiel #8
0
 def setup(self):
     self.queue = Mock(name='queue')
     self.callback = PromiseMock(name='callback')
     self.x = AsyncMessage(self.queue, 'body')
     self.x.receipt_handle = uuid()
Beispiel #9
0
 def setup(self):
     self.queue = Mock(name="queue")
     self.callback = PromiseMock(name="callback")
     self.x = AsyncMessage(self.queue, "body")
     self.x.receipt_handle = uuid()