@@ -695,6 +695,7 @@ mod tests {
695695 use super :: * ;
696696 use crate :: { payload:: ArrayDecoder , repo, Options } ;
697697 use itertools:: Itertools ;
698+ use pretty_assertions:: assert_matches;
698699 use proptest:: prelude:: * ;
699700 use spacetimedb_paths:: server:: CommitLogDir ;
700701 use tempfile:: tempdir;
@@ -924,16 +925,28 @@ mod tests {
924925
925926 // Truncating to any offset in the written range or larger
926927 // retains that offset - 1, or the max offset written.
927- let truncate_to: TxOffset = rand:: random_range ( 1 ..=32 ) ;
928- let retained_key = truncate_to. saturating_sub ( 1 ) . min ( 10 ) ;
929- let retained_val = retained_key * 128 ;
930- let retained = ( retained_key, retained_val) ;
931-
932- writer. ftruncate ( truncate_to, rand:: random ( ) ) . unwrap ( ) ;
933- assert_eq ! ( writer. head. key_lookup( truncate_to) . unwrap( ) , retained) ;
934- // Make sure this also holds after reopen.
935- drop ( writer) ;
936- let index = TxOffsetIndex :: open_index_file ( & index_path) . unwrap ( ) ;
937- assert_eq ! ( index. key_lookup( truncate_to) . unwrap( ) , retained) ;
928+ for truncate_to in ( 2 ..=10u64 ) . rev ( ) {
929+ let retained_key = truncate_to. saturating_sub ( 1 ) . min ( 10 ) ;
930+ let retained_val = retained_key * 128 ;
931+ let retained = ( retained_key, retained_val) ;
932+
933+ writer. ftruncate ( truncate_to, rand:: random ( ) ) . unwrap ( ) ;
934+ assert_matches ! (
935+ writer. head. key_lookup( truncate_to) ,
936+ Ok ( x) if x == retained,
937+ "truncate to {truncate_to} should retain {retained:?}"
938+ ) ;
939+ // Make sure this also holds after reopen.
940+ let index = TxOffsetIndex :: open_index_file ( & index_path) . unwrap ( ) ;
941+ assert_matches ! (
942+ index. key_lookup( truncate_to) ,
943+ Ok ( x) if x == retained,
944+ "truncate to {truncate_to} should retain {retained:?} after reopen"
945+ ) ;
946+ }
947+
948+ // Truncating to 1 leaves no entries in the index
949+ writer. ftruncate ( 1 , rand:: random ( ) ) . unwrap ( ) ;
950+ assert_matches ! ( writer. head. key_lookup( 1 ) , Err ( IndexError :: KeyNotFound ) ) ;
938951 }
939952}
0 commit comments