'Check that HandleMessageBundles() requeues messages if it failed.
This test makes sure that when messages are pending for a client, and which
we have no certificate for, the messages are requeued when sending fails.'

def testHandleMessageBundle(self):

    (client_id,) = self.SetupClients(1)
    class MockCommunicator(object, ):
        'A    fake    that    simulates    an    unenrolled    client.'
        def DecodeMessages(self, *unused_args):
            'For    simplicity    client    sends    an    empty    request.'
            return ([], client_id, 100)
        def EncodeMessages(self, *unused_args, **unused_kw):
            'Raise    because    the    server    has    no    certificates    for    this    client.'
            raise communicator.UnknownClientCert()
    self.server._communicator = MockCommunicator()
    request_comms = rdf_flows.ClientCommunication()
    self.assertRaises(communicator.UnknownClientCert, self.server.HandleMessageBundles, request_comms, 2)
    flow.GRRFlow.StartFlow(client_id=client_id, flow_name=flow_test_lib.SendingFlow.__name__, message_count=1, token=self.token)
    manager = queue_manager.QueueManager(token=self.token)
    tasks = manager.Query(client_id, limit=100)
    self.assertRaises(communicator.UnknownClientCert, self.server.HandleMessageBundles, request_comms, 2)
    new_tasks = manager.Query(client_id, limit=100)
    lease_time = ((new_tasks[0].eta - tasks[0].eta) / 1000000.0)
    self.assertLess(lease_time, 1)
    self.assertEqual((tasks[0].task_ttl - new_tasks[0].task_ttl), 1)
