@@ -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:
708708def 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 ])
736721def 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 ])
806754def test_stock_policy_restored_state_increments_without_fresh_entropy (
0 commit comments