'Test the ability to schedule a task.'

def testSchedule(self):

    test_queue = rdfvalue.RDFURN('fooSchedule')
    task = rdf_flows.GrrMessage(queue=test_queue, task_ttl=5, session_id='aff4:/Test', generate_task_id=True)
    manager = queue_manager.QueueManager(token=self.token)
    manager.Schedule([task])
    self.assertGreater(task.task_id, 0)
    self.assertGreater((task.task_id & 4294967295), 0)
    self.assertEqual(((long((self._current_mock_time * 1000)) & 4294967295) << 32), (task.task_id & 2305843004918726656))
    self.assertEqual(task.task_ttl, 5)
    (value, ts) = data_store.DB.Resolve(test_queue, manager._TaskIdToColumn(task.task_id), token=self.token)
    decoded = rdf_flows.GrrMessage.FromSerializedString(value)
    self.assertRDFValuesEqual(decoded, task)
    self.assertGreater(ts, 0)
    tasks = manager.QueryAndOwn(test_queue, lease_seconds=100, limit=100)
    self.assertEqual(len(tasks), 1)
    self.assertEqual(tasks[0].task_ttl, 4)
    self.assertEqual(tasks[0].session_id, 'aff4:/Test')
    self._current_mock_time += 10
    tasks = manager.QueryAndOwn(test_queue, lease_seconds=100, limit=100)
    self.assertEqual(len(tasks), 0)
    self._current_mock_time += 110
    tasks = manager.QueryAndOwn(test_queue, lease_seconds=100, limit=100)
    self.assertEqual(len(tasks), 1)
    self.assertEqual(tasks[0].task_ttl, 3)
    for i in range(2, 0, (-1)):
        self._current_mock_time += 110
        tasks = manager.QueryAndOwn(test_queue, lease_seconds=100)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0].task_ttl, i)
    self._current_mock_time += 110
    tasks = manager.QueryAndOwn(test_queue, lease_seconds=100)
    self.assertEqual(len(tasks), 0)
