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

Method test_uuid7_overflow_counter

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

Source from the content-addressed store, hash-verified

1044 equal(u.int & 0xffff_ffff, tail)
1045
1046 def test_uuid7_overflow_counter(self):
1047 equal = self.assertEqual
1048 # 1 Jan 2023 12:34:56.123_456_789
1049 timestamp_ns = 1672533296_123_456_789 # ns precision
1050 timestamp_ms, _ = divmod(timestamp_ns, 1_000_000)
1051
1052 new_counter_hi = random.getrandbits(11)
1053 new_counter_lo = random.getrandbits(30)
1054 new_counter = (new_counter_hi << 30) | new_counter_lo
1055
1056 tail = random.getrandbits(32)
1057 random_bits = (new_counter << 32) | tail
1058 random_data = random_bits.to_bytes(10)
1059
1060 with (
1061 mock.patch.multiple(
1062 self.uuid,
1063 _last_timestamp_v7=timestamp_ms,
1064 # same timestamp, but force an overflow on the counter
1065 _last_counter_v7=0x3ff_ffff_ffff,
1066 ),
1067 mock.patch('time.time_ns', return_value=timestamp_ns),
1068 mock.patch('os.urandom', return_value=random_data) as urand
1069 ):
1070 u = self.uuid.uuid7()
1071 urand.assert_called_with(10)
1072 equal(u.variant, self.uuid.RFC_4122)
1073 equal(u.version, 7)
1074 # timestamp advanced due to overflow
1075 equal(self.uuid._last_timestamp_v7, timestamp_ms + 1)
1076 unix_ts_ms = (timestamp_ms + 1) & 0xffff_ffff_ffff
1077 equal(u.time, unix_ts_ms)
1078 equal((u.int >> 80) & 0xffff_ffff_ffff, unix_ts_ms)
1079 # counter overflowed, so we picked a new one
1080 equal(self.uuid._last_counter_v7, new_counter)
1081 equal((u.int >> 64) & 0xfff, new_counter_hi)
1082 equal((u.int >> 32) & 0x3fff_ffff, new_counter_lo)
1083 equal(u.int & 0xffff_ffff, tail)
1084
1085 def test_uuid8(self):
1086 equal = self.assertEqual

Callers

nothing calls this directly

Calls 2

assert_called_withMethod · 0.80
getrandbitsMethod · 0.45

Tested by

no test coverage detected