MCPcopy Index your code
hub / github.com/python/cpython / test_uuid7_timestamp_backwards

Method test_uuid7_timestamp_backwards

Lib/test/test_uuid.py:1006–1044  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1004 self.assertLess(u1, u2)
1005
1006 def test_uuid7_timestamp_backwards(self):
1007 equal = self.assertEqual
1008 # 1 Jan 2023 12:34:56.123_456_789
1009 timestamp_ns = 1672533296_123_456_789 # ns precision
1010 timestamp_ms, _ = divmod(timestamp_ns, 1_000_000)
1011 fake_last_timestamp_v7 = timestamp_ms + 1
1012
1013 # counter_{hi,lo} are chosen so that "counter + 1" does not overflow
1014 counter_hi = random.getrandbits(11)
1015 counter_lo = random.getrandbits(29)
1016 counter = (counter_hi << 30) | counter_lo
1017 self.assertLess(counter + 1, 0x3ff_ffff_ffff)
1018
1019 tail_bytes = os.urandom(4)
1020 tail = int.from_bytes(tail_bytes)
1021
1022 with (
1023 mock.patch.multiple(
1024 self.uuid,
1025 _last_timestamp_v7=fake_last_timestamp_v7,
1026 _last_counter_v7=counter,
1027 ),
1028 mock.patch('time.time_ns', return_value=timestamp_ns),
1029 mock.patch('os.urandom', return_value=tail_bytes) as urand
1030 ):
1031 u = self.uuid.uuid7()
1032 urand.assert_called_once_with(4)
1033 equal(u.variant, self.uuid.RFC_4122)
1034 equal(u.version, 7)
1035 equal(self.uuid._last_timestamp_v7, fake_last_timestamp_v7 + 1)
1036 unix_ts_ms = (fake_last_timestamp_v7 + 1) & 0xffff_ffff_ffff
1037 equal(u.time, unix_ts_ms)
1038 equal((u.int >> 80) & 0xffff_ffff_ffff, unix_ts_ms)
1039 # 42-bit counter advanced by 1
1040 equal(self.uuid._last_counter_v7, counter + 1)
1041 equal((u.int >> 64) & 0xfff, counter_hi)
1042 # 42-bit counter advanced by 1 (counter_hi is untouched)
1043 equal((u.int >> 32) & 0x3fff_ffff, counter_lo + 1)
1044 equal(u.int & 0xffff_ffff, tail)
1045
1046 def test_uuid7_overflow_counter(self):
1047 equal = self.assertEqual

Callers

nothing calls this directly

Calls 3

getrandbitsMethod · 0.45
assertLessMethod · 0.45

Tested by

no test coverage detected