def test_stream(self):
        messages = {'links': [],
                    'messages': [{
                        'href': '/v1/queues/mine/messages/123123423',
                        'ttl': 800,
                        'age': 790,
                        'body': {'event': 'ActivateAccount',
                                'mode': 'active'}
                    }]
                    }

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, json.dumps(messages))
            send_method.return_value = resp

            # NOTE(flaper87): The first iteration will return 1 message
            # and then call `_next_page` which will use the rel-next link
            # to get a new set of messages.
            link = {'rel': 'next',
                    'href': "/v1/queues/mine/messages?marker=6244-244224-783"}
            messages['links'].append(link)

            iterator = iterate._Iterator(self.queue.client,
                                         messages,
                                         'messages',
                                         message.create_object(self.queue))
            iterated = [msg for msg in iterator.stream()]
            self.assertEqual(len(iterated), 2)
    def test_iterator_respect_paging(self):
        messages = {'links': [],
                    'messages': [{
                        'href': '/v1/queues/mine/messages/123123423',
                        'ttl': 800,
                        'age': 790,
                        'body': {'event': 'ActivateAccount',
                                'mode': 'active'}
                    }]
                    }

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, json.dumps(messages))
            send_method.return_value = resp

            link = {'rel': 'next',
                    'href': "/v1/queues/mine/messages?marker=6244-244224-783"}
            messages['links'].append(link)

            iterator = iterate._Iterator(self.queue.client,
                                         messages,
                                         'messages',
                                         message.create_object(self.queue))
            iterated = [msg for msg in iterator]
            self.assertEqual(len(iterated), 1)
示例#3
0
 def _create(self):
     req, trans = self._queue.client._request_and_transport()
     msgs = core.claim_create(trans, req, self._queue._name, ttl=self._ttl, grace=self._grace, limit=self._limit)
     # extract the id from the first message
     if msgs is not None:
         self.id = msgs[0]["href"].split("=")[-1]
     self._message_iter = iterate._Iterator(
         self._queue.client, msgs or [], "messages", message.create_object(self._queue)
     )
示例#4
0
    def _get(self):
        req, trans = self._queue.client._request_and_transport()

        claim_res = core.claim_get(trans, req, self._queue._name, self.id)
        self._age = claim_res["age"]
        self._ttl = claim_res["ttl"]
        self._grace = claim_res.get("grace")
        msgs = claim_res.get("messages", [])
        self._message_iter = iterate._Iterator(self._queue.client, msgs, "messages", message.create_object(self._queue))
示例#5
0
    def pop(self, count=1):
        """Pop `count` messages from the server

        :param count: Number of messages to pop.
        :type count: int

        :returns: List of messages
        :rtype: `list`
        """

        req, trans = self.client._request_and_transport()
        msgs = core.message_pop(trans, req, self._name, count=count)
        return iterator._Iterator(self.client,
                                  msgs,
                                  'messages',
                                  message.create_object(self))
    def test_no_next_iteration(self):
        messages = {'links': [],
                    'messages': [{
                        'href': '/v1/queues/mine/messages/123123423',
                        'ttl': 800,
                        'age': 790,
                        'body': {'event': 'ActivateAccount',
                                'mode': 'active'}
                    }]
                    }

        iterator = iterate._Iterator(self.queue.client,
                                     messages,
                                     'messages',
                                     message.create_object(self.queue))
        iterated = [msg for msg in iterator]
        self.assertEqual(len(iterated), 1)
示例#7
0
    def messages(self, *messages, **params):
        """Gets a list of messages from the server

        This method returns a list of messages, it can be
        used to retrieve a set of messages by id or to
        walk through the active messages by using the
        collection endpoint.

        The `messages` and `params` params are mutually exclusive
        and the former has the priority.

        :param messages: List of messages' ids to retrieve.
        :type messages: *args of `six.string_type`

        :param params: Filters to use for getting messages
        :type params: **kwargs dict.

        :returns: List of messages
        :rtype: `list`
        """
        req, trans = self.client._request_and_transport()

        # TODO(flaper87): Return a MessageIterator.
        # This iterator should handle limits, pagination
        # and messages deserialization.

        if messages:
            msgs = core.message_get_many(trans, req,
                                         self._name, messages)
        else:
            # NOTE(flaper87): It's safe to access messages
            # directly. If something wrong happens, the core
            # API will raise the right exceptions.
            msgs = core.message_list(trans, req,
                                     self._name,
                                     **params)

        return iterator._Iterator(self.client,
                                  msgs,
                                  'messages',
                                  message.create_object(self))