示例#1
0
    def nack(self):
        """Decline to acknowldge the given message.

        This will cause the message to be re-delivered to the subscription.
        """
        self._request_queue.put(
            base_policy.NackRequest(ack_id=self._ack_id, byte_size=self.size))
def test_on_response_nacks_on_error():
    # Create a callback that always errors.
    callback = mock.Mock(spec=(), side_effect=ValueError)
    executor = mock.create_autospec(futures.Executor, instance=True)
    executor.submit.side_effect = _callback_side_effect
    policy = create_and_open_policy(callback, executor=executor)

    # Set up the messages.
    message = types.PubsubMessage(data=b'foo', message_id='1')
    response = types.StreamingPullResponse(received_messages=[
        types.ReceivedMessage(ack_id='fack', message=message),
    ], )

    # Actually run the method and prove that nack is called because the
    # callback errored.
    policy.on_response(response)

    # Make sure the callback was executed.
    callback.assert_called_once_with(mock.ANY)

    # Process outstanding requests, the callback should've queued a nack
    # request.
    nack_patch = mock.patch.object(policy, 'nack', autospec=True)
    with nack_patch as nack:
        policy.dispatch_callback(policy._request_queue.queue)

    nack.assert_called_once_with(
        [base.NackRequest('fack', message.ByteSize())])
def test_nack():
    msg = create_message(b'foo', ack_id='bogus_ack_id')
    with mock.patch.object(msg._request_queue, 'put') as put:
        msg.nack()
        put.assert_called_once_with(
            base.NackRequest(
                ack_id='bogus_ack_id',
                byte_size=25,
            ))
        check_call_types(put, base.NackRequest)
def test_nack():
    policy = create_policy()
    with mock.patch.object(policy, 'modify_ack_deadline') as mad:
        with mock.patch.object(policy, 'drop') as drop:
            items = [base.NackRequest(ack_id='ack_id_string', byte_size=10)]
            policy.nack(items)
            drop.assert_called_once_with(
                [base.DropRequest(ack_id='ack_id_string', byte_size=10)])
        mad.assert_called_once_with(
            [base.ModAckRequest(ack_id='ack_id_string', seconds=0)])
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()

    details = 'Bad thing happened. Time out, go sit in the corner.'
    exc = exceptions.DeadlineExceeded(details)

    assert policy.on_exception(exc) is True