@@ -11,12 +11,15 @@ use linera_base::{
1111} ;
1212use linera_chain:: types:: Timeout ;
1313
14- use super :: { ValueCache , DEFAULT_VALUE_CACHE_SIZE } ;
14+ use super :: ValueCache ;
15+
16+ /// Test cache size for unit tests.
17+ const TEST_CACHE_SIZE : usize = 1000 ;
1518
1619/// Tests attempt to retrieve non-existent value.
1720#[ test]
1821fn test_retrieve_missing_value ( ) {
19- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
22+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
2023 let hash = CryptoHash :: test_hash ( "Missing value" ) ;
2124
2225 assert ! ( cache. get( & hash) . is_none( ) ) ;
@@ -26,7 +29,7 @@ fn test_retrieve_missing_value() {
2629/// Tests inserting a certificate value in the cache.
2730#[ test]
2831fn test_insert_single_certificate_value ( ) {
29- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
32+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
3033 let value = create_dummy_certificate_value ( 0 ) ;
3134 let hash = value. hash ( ) ;
3235
@@ -39,9 +42,8 @@ fn test_insert_single_certificate_value() {
3942/// Tests inserting many certificate values in the cache, one-by-one.
4043#[ test]
4144fn test_insert_many_certificate_values_individually ( ) {
42- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
43- let values =
44- create_dummy_certificate_values ( 0 ..( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
45+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
46+ let values = create_dummy_certificate_values ( 0 ..( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
4547
4648 for value in & values {
4749 assert ! ( cache. insert( Cow :: Borrowed ( value) ) ) ;
@@ -61,9 +63,8 @@ fn test_insert_many_certificate_values_individually() {
6163/// Tests inserting many values in the cache, all-at-once.
6264#[ test]
6365fn test_insert_many_values_together ( ) {
64- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
65- let values =
66- create_dummy_certificate_values ( 0 ..( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
66+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
67+ let values = create_dummy_certificate_values ( 0 ..( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
6768
6869 cache. insert_all ( values. iter ( ) . map ( Cow :: Borrowed ) ) ;
6970
@@ -81,9 +82,8 @@ fn test_insert_many_values_together() {
8182/// Tests re-inserting many values in the cache, all-at-once.
8283#[ test]
8384fn test_reinsertion_of_values ( ) {
84- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
85- let values =
86- create_dummy_certificate_values ( 0 ..( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
85+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
86+ let values = create_dummy_certificate_values ( 0 ..( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
8787
8888 cache. insert_all ( values. iter ( ) . map ( Cow :: Borrowed ) ) ;
8989
@@ -105,9 +105,8 @@ fn test_reinsertion_of_values() {
105105/// Tests eviction of one entry.
106106#[ test]
107107fn test_one_eviction ( ) {
108- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
109- let values =
110- create_dummy_certificate_values ( 0 ..=( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
108+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
109+ let values = create_dummy_certificate_values ( 0 ..=( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
111110
112111 cache. insert_all ( values. iter ( ) . map ( Cow :: Borrowed ) ) ;
113112
@@ -128,18 +127,12 @@ fn test_one_eviction() {
128127/// Tests eviction of the second entry.
129128#[ test]
130129fn test_eviction_of_second_entry ( ) {
131- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
132- let values =
133- create_dummy_certificate_values ( 0 ..=( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
134-
135- cache. insert_all (
136- values
137- . iter ( )
138- . take ( DEFAULT_VALUE_CACHE_SIZE )
139- . map ( Cow :: Borrowed ) ,
140- ) ;
130+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
131+ let values = create_dummy_certificate_values ( 0 ..=( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
132+
133+ cache. insert_all ( values. iter ( ) . take ( TEST_CACHE_SIZE ) . map ( Cow :: Borrowed ) ) ;
141134 cache. get ( & values[ 0 ] . hash ( ) ) ;
142- assert ! ( cache. insert( Cow :: Borrowed ( & values[ DEFAULT_VALUE_CACHE_SIZE ] ) ) ) ;
135+ assert ! ( cache. insert( Cow :: Borrowed ( & values[ TEST_CACHE_SIZE ] ) ) ) ;
143136
144137 assert ! ( cache. contains( & values[ 0 ] . hash( ) ) ) ;
145138 assert_eq ! ( cache. get( & values[ 0 ] . hash( ) ) . as_ref( ) , Some ( & values[ 0 ] ) ) ;
@@ -167,18 +160,12 @@ fn test_eviction_of_second_entry() {
167160/// Tests if reinsertion of the first entry promotes it so that it's not evicted so soon.
168161#[ test]
169162fn test_promotion_of_reinsertion ( ) {
170- let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: default ( ) ;
171- let values =
172- create_dummy_certificate_values ( 0 ..=( DEFAULT_VALUE_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
173-
174- cache. insert_all (
175- values
176- . iter ( )
177- . take ( DEFAULT_VALUE_CACHE_SIZE )
178- . map ( Cow :: Borrowed ) ,
179- ) ;
163+ let cache = ValueCache :: < CryptoHash , Hashed < Timeout > > :: new ( TEST_CACHE_SIZE ) ;
164+ let values = create_dummy_certificate_values ( 0 ..=( TEST_CACHE_SIZE as u64 ) ) . collect :: < Vec < _ > > ( ) ;
165+
166+ cache. insert_all ( values. iter ( ) . take ( TEST_CACHE_SIZE ) . map ( Cow :: Borrowed ) ) ;
180167 assert ! ( !cache. insert( Cow :: Borrowed ( & values[ 0 ] ) ) ) ;
181- assert ! ( cache. insert( Cow :: Borrowed ( & values[ DEFAULT_VALUE_CACHE_SIZE ] ) ) ) ;
168+ assert ! ( cache. insert( Cow :: Borrowed ( & values[ TEST_CACHE_SIZE ] ) ) ) ;
182169
183170 assert ! ( cache. contains( & values[ 0 ] . hash( ) ) ) ;
184171 assert_eq ! ( cache. get( & values[ 0 ] . hash( ) ) . as_ref( ) , Some ( & values[ 0 ] ) ) ;
0 commit comments