Exemple #1
0
def _get_task_to_run(now, request_key_id, slice_index, **kwargs):
    """Creates a TaskToRun."""
    request_key = ndb.Key('TaskRequest', request_key_id)
    try_number = 1
    to_run_key = ndb.Key('TaskToRun',
                         try_number | (slice_index << 4),
                         parent=request_key)
    args = {
        'key': to_run_key,
        'created_ts': now,
        'queue_number': None,
    }
    args.update(kwargs)
    return task_to_run.TaskToRun(**args)
 def test_gen_queue_number(self):
     # tuples of (input, expected).
     # 0x3fc00000 is the priority mask.
     # pylint: disable=bad-whitespace
     data = [
         # Priorities.
         ((1, '1970-01-01 00:00:00.000', 0), (0x92cc0300, 75)),
         ((1, '1970-01-01 00:00:00.000', 1), (0x930c0300, 76)),
         ((1, '1970-01-01 00:00:00.000', 2), (0x934c0300, 77)),
         ((1, '1970-01-01 00:00:00.000', 3), (0x938c0300, 78)),
         ((1, '1970-01-01 00:00:00.000', 255), (0xd28c0300, 330)),
         # Largest hash.
         ((0xffffffff, '1970-01-01 00:00:00.000', 255), (0x7fffffffd28c0300,
                                                         330)),
         # Time resolution.
         ((1, '1970-01-01 00:00:00.040', 0), (0x92cc0300, 75)),
         ((1, '1970-01-01 00:00:00.050', 0), (0x92cc0300, 75)),
         ((1, '1970-01-01 00:00:00.100', 0), (0x92cc02ff, 75)),
         ((1, '1970-01-01 00:00:00.900', 0), (0x92cc02f7, 75)),
         ((1, '1970-01-01 00:00:01.000', 0), (0x92cc02f6, 75)),  # 10
         ((1, '2010-01-02 03:04:05.060', 0), (0x92bd248d, 74)),
         ((1, '2010-01-02 03:04:05.060', 1), (0x92fd248d, 75)),
         ((1, '2010-12-31 23:59:59.999', 0), (0x80000000, 0)),
         ((1, '2010-12-31 23:59:59.999', 1), (0x80400000, 1)),
         ((1, '2010-12-31 23:59:59.999', 2), (0x80800000, 2)),
         ((1, '2010-12-31 23:59:59.999', 255), (0xbfc00000, 255)),
         # It's the end of the world as we know it...
         ((1, '9998-12-31 23:59:59.999', 0), (0x80000000, 0)),
         ((1, '9998-12-31 23:59:59.999', 1), (0x80400000, 1)),
         ((1, '9998-12-31 23:59:59.999', 255), (0xbfc00000, 255)),
     ]
     # pylint: enable=bad-whitespace
     for i, ((dimensions_hash, timestamp, priority),
             (expected_v, expected_p)) in enumerate(data):
         d = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
         actual = task_to_run._gen_queue_number(dimensions_hash, d,
                                                priority)
         self.assertEqual((i, '0x%016x' % expected_v),
                          (i, '0x%016x' % actual))
         # Ensure we can extract the priority back. That said, it is corrupted by
         # time.
         v = task_to_run.TaskToRun(queue_number=actual)
         self.assertEqual((i, expected_p),
                          (i, task_to_run._queue_number_priority(v)))