def test_ack_no_time():
    policy = create_policy()
    policy._consumer._stopped.clear()
    with mock.patch.object(policy._consumer, 'send_request') as send_request:
        policy.ack(
            [base.AckRequest('ack_id_string', time_to_ack=None, byte_size=0)])
        send_request.assert_called_once_with(
            types.StreamingPullRequest(ack_ids=['ack_id_string'], ))
    assert len(policy.histogram) == 0
def test_ack_paused():
    policy = create_policy()
    consumer = policy._consumer
    consumer._stopped.set()
    assert consumer.paused is True

    policy.ack([base.AckRequest('ack_id_string', 0, 0)])

    assert consumer.paused is False
    assert 'ack_id_string' in policy._ack_on_resume
Ejemplo n.º 3
0
def test_ack():
    msg = create_message(b'foo', ack_id='bogus_ack_id')
    with mock.patch.object(msg._request_queue, 'put') as put:
        msg.ack()
        put.assert_called_once_with(
            base.AckRequest(
                ack_id='bogus_ack_id',
                byte_size=25,
                time_to_ack=mock.ANY,
            ))
        check_call_types(put, base.AckRequest)
Ejemplo n.º 4
0
    def ack(self):
        """Acknowledge the given message.

        Acknowledging a message in Pub/Sub means that you are done
        with it, and it will not be delivered to this subscription again.
        You should avoid acknowledging messages until you have
        *finished* processing them, so that in the event of a failure,
        you receive the message again.

        .. warning::
            Acks in Pub/Sub are best effort. You should always
            ensure that your processing code is idempotent, as you may
            receive any given message more than once.
        """
        time_to_ack = math.ceil(time.time() - self._received_timestamp)
        self._request_queue.put(
            base_policy.AckRequest(ack_id=self._ack_id,
                                   byte_size=self.size,
                                   time_to_ack=time_to_ack))
    assert policy._leases_thread is threads[2]
    threads[2].start.assert_called_once_with()


def test_open_already_open():
    policy = create_policy()
    policy._future = mock.sentinel.future

    with pytest.raises(ValueError) as exc_info:
        policy.open(None)

    assert exc_info.value.args == ('This policy has already been opened.', )


@pytest.mark.parametrize('item,method',
                         [(base.AckRequest(0, 0, 0), 'ack'),
                          (base.DropRequest(0, 0), 'drop'),
                          (base.LeaseRequest(0, 0), 'lease'),
                          (base.ModAckRequest(0, 0), 'modify_ack_deadline'),
                          (base.NackRequest(0, 0), 'nack')])
def test_dispatch_callback_valid(item, method):
    policy = create_policy()
    with mock.patch.object(policy, method) as mocked:
        items = [item]
        policy.dispatch_callback(items)
        mocked.assert_called_once_with([item])


def test_on_exception_deadline_exceeded():
    policy = create_policy()