Skip to content

Commit 4e4c24f

Browse files
committed
chore: tidy up after epoch randomness fix
Note the changed initial prev_timestamp value in the changelog, mark -1 as a literal in the BaseMonotonicPolicy docstring, drop two tests that targeted the superseded flag-based approach, and use constants.RANDOMNESS_LEN instead of a hard-coded 10 in the epoch tests.
1 parent 40e5bc3 commit 4e4c24f

3 files changed

Lines changed: 13 additions & 64 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Fixed
1212
~~~~~
1313
* Draw fresh randomness for the first identifier at timestamp zero with
1414
:class:`.StrictMonotonicPolicy` and :class:`.LaxMonotonicPolicy`, instead of treating the initial
15-
state as an existing epoch identifier.
15+
state as an existing epoch identifier. The ``prev_timestamp`` attribute of a fresh
16+
:class:`.BaseMonotonicPolicy` is now ``-1`` instead of ``0``.
1617

1718
`4.2.0`_ - 2026-11-17
1819
---------------------

‎tests/test_ulid.py‎

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def randomness(timestamp: int) -> bytes:
699699
assert value.bytes[constants.TIMESTAMP_LEN :] == entropy
700700
second = generator.generate(0.0) if explicit else generator.generate()
701701
assert second.bytes[constants.TIMESTAMP_LEN :] == (int.from_bytes(entropy, "big") + 1).to_bytes(
702-
10, "big"
702+
constants.RANDOMNESS_LEN, "big"
703703
)
704704
assert calls == [0]
705705

@@ -708,30 +708,15 @@ def randomness(timestamp: int) -> bytes:
708708
def test_independent_epoch_generators_do_not_discard_distinct_entropy(
709709
policy_type: type[BaseMonotonicPolicy],
710710
) -> None:
711-
first = ULIDGenerator(clock=lambda: 0, randomness=lambda _: b"a" * 10, policy=policy_type())
712-
second = ULIDGenerator(clock=lambda: 0, randomness=lambda _: b"b" * 10, policy=policy_type())
711+
first = ULIDGenerator(
712+
clock=lambda: 0, randomness=lambda _: b"a" * constants.RANDOMNESS_LEN, policy=policy_type()
713+
)
714+
second = ULIDGenerator(
715+
clock=lambda: 0, randomness=lambda _: b"b" * constants.RANDOMNESS_LEN, policy=policy_type()
716+
)
713717
assert first.generate() != second.generate()
714718

715719

716-
@pytest.mark.parametrize("policy_type", [StrictMonotonicPolicy, LaxMonotonicPolicy])
717-
def test_failed_first_epoch_entropy_attempt_does_not_initialize_policy(
718-
policy_type: type[BaseMonotonicPolicy],
719-
) -> None:
720-
calls: list[int] = []
721-
722-
def randomness(timestamp: int) -> bytes:
723-
calls.append(timestamp)
724-
if len(calls) == 1:
725-
raise RuntimeError("synthetic entropy failure")
726-
return b"c" * 10
727-
728-
generator = ULIDGenerator(clock=lambda: 0, randomness=randomness, policy=policy_type())
729-
with pytest.raises(RuntimeError, match="synthetic entropy failure"):
730-
generator.generate()
731-
assert generator.generate().bytes[constants.TIMESTAMP_LEN :] == b"c" * 10
732-
assert calls == [0, 0]
733-
734-
735720
@pytest.mark.parametrize("policy_type", [StrictMonotonicPolicy, LaxMonotonicPolicy])
736721
def test_epoch_entropy_maximum_is_used_before_overflow_policy(
737722
policy_type: type[BaseMonotonicPolicy],
@@ -752,55 +737,18 @@ def test_return_to_epoch_after_another_timestamp_draws_fresh_entropy(
752737
policy_type: type[BaseMonotonicPolicy],
753738
) -> None:
754739
calls: list[int] = []
740+
entropy = b"d" * constants.RANDOMNESS_LEN
755741

756742
def randomness(timestamp: int) -> bytes:
757743
calls.append(timestamp)
758-
return b"d" * 10
744+
return entropy
759745

760746
generator = ULIDGenerator(randomness=randomness, policy=policy_type())
761747
assert generator.generate(1.0).milliseconds == constants.MILLISECS_IN_SECS
762-
assert generator.generate(0.0).bytes[constants.TIMESTAMP_LEN :] == b"d" * 10
748+
assert generator.generate(0.0).bytes[constants.TIMESTAMP_LEN :] == entropy
763749
assert calls == [constants.MILLISECS_IN_SECS, 0]
764750

765751

766-
@pytest.mark.parametrize("timestamp", [0, 123456789])
767-
@pytest.mark.parametrize("same_timestamp", [False, True])
768-
def test_custom_policy_initializer_preserves_seeded_state(
769-
timestamp: int, *, same_timestamp: bool
770-
) -> None:
771-
class SeededPolicy(BaseMonotonicPolicy):
772-
def __init__(self) -> None:
773-
# A public subclass may manage the two existing state attributes itself.
774-
self.prev_timestamp = timestamp
775-
self.prev_randomness = b"s" * constants.RANDOMNESS_LEN
776-
777-
def _on_overflow(self, timestamp: int, randomness_source: Callable[[int], bytes]) -> bytes:
778-
return randomness_source(timestamp)
779-
780-
calls: list[int] = []
781-
782-
def randomness(timestamp: int) -> bytes:
783-
calls.append(timestamp)
784-
return b"f" * constants.RANDOMNESS_LEN
785-
786-
policy = SeededPolicy()
787-
next_timestamp = timestamp if same_timestamp else timestamp + 1
788-
value = policy.resolve_randomness(next_timestamp, randomness)
789-
if same_timestamp:
790-
expected = (int.from_bytes(b"s" * constants.RANDOMNESS_LEN, "big") + 1).to_bytes(
791-
constants.RANDOMNESS_LEN, "big"
792-
)
793-
assert value == expected
794-
assert calls == []
795-
else:
796-
assert value == b"f" * constants.RANDOMNESS_LEN
797-
assert calls == [next_timestamp]
798-
assert policy.prev_timestamp == next_timestamp
799-
assert policy.prev_randomness == value
800-
again = policy.resolve_randomness(next_timestamp, randomness)
801-
assert int.from_bytes(again, "big") == int.from_bytes(value, "big") + 1
802-
803-
804752
@pytest.mark.parametrize("policy_type", [StrictMonotonicPolicy, LaxMonotonicPolicy])
805753
@pytest.mark.parametrize("timestamp", [0, 123456789])
806754
def test_stock_policy_restored_state_increments_without_fresh_entropy(

‎ulid/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def resolve_randomness(
6464
class BaseMonotonicPolicy(abc.ABC):
6565
"""Base class for stateful monotonic policies.
6666
67-
``prev_timestamp`` is -1 until the first successful randomness resolution.
67+
``prev_timestamp`` is ``-1`` until the first successful randomness resolution.
6868
Restoring ``prev_timestamp`` and ``prev_randomness`` seeds the previous ULID state.
6969
"""
7070

0 commit comments

Comments
 (0)