def testTIDUtils(self): """ Tests packTID/unpackTID/addTID. """ min_tid = pack('!LL', 0, 0) min_unpacked_tid = ((1900, 1, 1, 0, 0), 0) max_tid = pack('!LL', 2**32 - 1, 2**32 - 1) # ((((9917 - 1900) * 12 + (10 - 1)) * 31 + (14 - 1)) * 24 + 4) * 60 + # 15 == 2**32 - 1 max_unpacked_tid = ((9917, 10, 14, 4, 15), 2**32 - 1) self.assertEqual(unpackTID(min_tid), min_unpacked_tid) self.assertEqual(unpackTID(max_tid), max_unpacked_tid) self.assertEqual(packTID(*min_unpacked_tid), min_tid) self.assertEqual(packTID(*max_unpacked_tid), max_tid) self.assertEqual(addTID(min_tid, 1), pack('!LL', 0, 1)) self.assertEqual(addTID(pack('!LL', 0, 2**32 - 1), 1), pack('!LL', 1, 0)) self.assertEqual(addTID(pack('!LL', 0, 2**32 - 1), 2**32 + 1), pack('!LL', 2, 0)) # Check impossible dates are avoided (2010/11/31 doesn't exist) self.assertEqual( unpackTID(addTID(packTID((2010, 11, 30, 23, 59), 2**32 - 1), 1)), ((2010, 12, 1, 0, 0), 0))
def testTIDUtils(self): """ Tests packTID/unpackTID/addTID. """ min_tid = pack('!LL', 0, 0) min_unpacked_tid = ((1900, 1, 1, 0, 0), 0) max_tid = pack('!LL', 2**32 - 1, 2 ** 32 - 1) # ((((9917 - 1900) * 12 + (10 - 1)) * 31 + (14 - 1)) * 24 + 4) * 60 + # 15 == 2**32 - 1 max_unpacked_tid = ((9917, 10, 14, 4, 15), 2**32 - 1) self.assertEqual(unpackTID(min_tid), min_unpacked_tid) self.assertEqual(unpackTID(max_tid), max_unpacked_tid) self.assertEqual(packTID(*min_unpacked_tid), min_tid) self.assertEqual(packTID(*max_unpacked_tid), max_tid) self.assertEqual(addTID(min_tid, 1), pack('!LL', 0, 1)) self.assertEqual(addTID(pack('!LL', 0, 2**32 - 1), 1), pack('!LL', 1, 0)) self.assertEqual(addTID(pack('!LL', 0, 2**32 - 1), 2**32 + 1), pack('!LL', 2, 0)) # Check impossible dates are avoided (2010/11/31 doesn't exist) self.assertEqual( unpackTID(addTID(packTID((2010, 11, 30, 23, 59), 2**32 - 1), 1)), ((2010, 12, 1, 0, 0), 0))
def _nextTID(self, ttid=None, divisor=None): """ Compute the next TID based on the current time and check collisions. Also, if ttid is not None, divisor is mandatory adjust it so that tid % divisor == ttid % divisor while preserving min_tid < tid If ttid is None, divisor is ignored. When constraints allow, prefer decreasing generated TID, to avoid fast-forwarding to future dates. """ tid = tidFromTime(time()) min_tid = self._last_tid if tid <= min_tid: tid = addTID(min_tid, 1) # We know we won't have room to adjust by decreasing. try_decrease = False else: try_decrease = True if ttid is not None: assert isinstance(ttid, basestring), repr(ttid) assert isinstance(divisor, (int, long)), repr(divisor) ref_remainder = u64(ttid) % divisor remainder = u64(tid) % divisor if ref_remainder != remainder: if try_decrease: new_tid = addTID(tid, ref_remainder - divisor - remainder) assert u64(new_tid) % divisor == ref_remainder, (dump(new_tid), ref_remainder) if new_tid <= min_tid: new_tid = addTID(new_tid, divisor) else: if ref_remainder > remainder: ref_remainder += divisor new_tid = addTID(tid, ref_remainder - remainder) assert min_tid < new_tid, (dump(min_tid), dump(tid), dump(new_tid)) tid = new_tid self._last_tid = tid return self._last_tid