@@ -16,6 +16,7 @@ use rand::{thread_rng, Rng};
1616
1717use std:: panic:: RefUnwindSafe ;
1818use std:: path:: PathBuf ;
19+ use std:: sync:: RwLock ;
1920
2021pub fn random_storage_path ( ) -> PathBuf {
2122 let mut temp_path = std:: env:: temp_dir ( ) ;
@@ -139,26 +140,69 @@ pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
139140
140141// A `KVStore` impl for testing purposes that wraps all our `KVStore`s and asserts their synchronicity.
141142pub ( crate ) struct TestSyncStore {
143+ serializer : RwLock < ( ) > ,
142144 test_store : TestStore ,
143145 fs_store : FilesystemStore ,
144146 sqlite_store : SqliteStore ,
145147}
146148
147149impl TestSyncStore {
148150 pub ( crate ) fn new ( dest_dir : PathBuf ) -> Self {
149- let fs_store = FilesystemStore :: new ( dest_dir. clone ( ) ) ;
150- let sqlite_store = SqliteStore :: new ( dest_dir,
151- Some ( "test_sync_db" . to_string ( ) ) , Some ( "test_sync_table" . to_string ( ) ) ) . unwrap ( ) ;
151+ let serializer = RwLock :: new ( ( ) ) ;
152+ let mut fs_dir = dest_dir. clone ( ) ;
153+ fs_dir. push ( "fs_store" ) ;
154+ let fs_store = FilesystemStore :: new ( fs_dir) ;
155+ let mut sql_dir = dest_dir. clone ( ) ;
156+ sql_dir. push ( "sqlite_store" ) ;
157+ let sqlite_store = SqliteStore :: new (
158+ sql_dir,
159+ Some ( "test_sync_db" . to_string ( ) ) ,
160+ Some ( "test_sync_table" . to_string ( ) ) ,
161+ )
162+ . unwrap ( ) ;
152163 let test_store = TestStore :: new ( false ) ;
153- Self { fs_store, sqlite_store, test_store }
164+ Self { serializer, fs_store, sqlite_store, test_store }
165+ }
166+
167+ fn do_list (
168+ & self , primary_namespace : & str , secondary_namespace : & str ,
169+ ) -> std:: io:: Result < Vec < String > > {
170+ let fs_res = self . fs_store . list ( primary_namespace, secondary_namespace) ;
171+ let sqlite_res = self . sqlite_store . list ( primary_namespace, secondary_namespace) ;
172+ let test_res = self . test_store . list ( primary_namespace, secondary_namespace) ;
173+
174+ match fs_res {
175+ Ok ( mut list) => {
176+ list. sort ( ) ;
177+
178+ let mut sqlite_list = sqlite_res. unwrap ( ) ;
179+ sqlite_list. sort ( ) ;
180+ assert_eq ! ( list, sqlite_list) ;
181+
182+ let mut test_list = test_res. unwrap ( ) ;
183+ test_list. sort ( ) ;
184+ assert_eq ! ( list, test_list) ;
185+
186+ Ok ( list)
187+ }
188+ Err ( e) => {
189+ assert ! ( sqlite_res. is_err( ) ) ;
190+ assert ! ( test_res. is_err( ) ) ;
191+ Err ( e)
192+ }
193+ }
154194 }
155195}
156196
157197impl KVStore for TestSyncStore {
158- fn read ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> std:: io:: Result < Vec < u8 > > {
159- let fs_res = self . fs_store . read ( namespace, sub_namespace, key) ;
160- let sqlite_res = self . sqlite_store . read ( namespace, sub_namespace, key) ;
161- let test_res = self . test_store . read ( namespace, sub_namespace, key) ;
198+ fn read (
199+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
200+ ) -> std:: io:: Result < Vec < u8 > > {
201+ let _guard = self . serializer . read ( ) . unwrap ( ) ;
202+
203+ let fs_res = self . fs_store . read ( primary_namespace, secondary_namespace, key) ;
204+ let sqlite_res = self . sqlite_store . read ( primary_namespace, secondary_namespace, key) ;
205+ let test_res = self . test_store . read ( primary_namespace, secondary_namespace, key) ;
162206
163207 match fs_res {
164208 Ok ( read) => {
@@ -176,12 +220,18 @@ impl KVStore for TestSyncStore {
176220 }
177221 }
178222
179- fn write ( & self , namespace : & str , sub_namespace : & str , key : & str , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
180- let fs_res = self . fs_store . write ( namespace, sub_namespace, key, buf) ;
181- let sqlite_res = self . sqlite_store . write ( namespace, sub_namespace, key, buf) ;
182- let test_res = self . test_store . write ( namespace, sub_namespace, key, buf) ;
223+ fn write (
224+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ,
225+ ) -> std:: io:: Result < ( ) > {
226+ let _guard = self . serializer . write ( ) . unwrap ( ) ;
227+ let fs_res = self . fs_store . write ( primary_namespace, secondary_namespace, key, buf) ;
228+ let sqlite_res = self . sqlite_store . write ( primary_namespace, secondary_namespace, key, buf) ;
229+ let test_res = self . test_store . write ( primary_namespace, secondary_namespace, key, buf) ;
183230
184- assert ! ( self . list( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
231+ assert ! ( self
232+ . do_list( primary_namespace, secondary_namespace)
233+ . unwrap( )
234+ . contains( & key. to_string( ) ) ) ;
185235
186236 match fs_res {
187237 Ok ( ( ) ) => {
@@ -197,12 +247,19 @@ impl KVStore for TestSyncStore {
197247 }
198248 }
199249
200- fn remove ( & self , namespace : & str , sub_namespace : & str , key : & str , lazy : bool ) -> std:: io:: Result < ( ) > {
201- let fs_res = self . fs_store . remove ( namespace, sub_namespace, key, lazy) ;
202- let sqlite_res = self . sqlite_store . remove ( namespace, sub_namespace, key, lazy) ;
203- let test_res = self . test_store . remove ( namespace, sub_namespace, key, lazy) ;
250+ fn remove (
251+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
252+ ) -> std:: io:: Result < ( ) > {
253+ let _guard = self . serializer . write ( ) . unwrap ( ) ;
254+ let fs_res = self . fs_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
255+ let sqlite_res =
256+ self . sqlite_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
257+ let test_res = self . test_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
204258
205- assert ! ( !self . list( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
259+ assert ! ( !self
260+ . do_list( primary_namespace, secondary_namespace)
261+ . unwrap( )
262+ . contains( & key. to_string( ) ) ) ;
206263
207264 match fs_res {
208265 Ok ( ( ) ) => {
@@ -218,30 +275,10 @@ impl KVStore for TestSyncStore {
218275 }
219276 }
220277
221- fn list ( & self , namespace : & str , sub_namespace : & str ) -> std:: io:: Result < Vec < String > > {
222- let fs_res = self . fs_store . list ( namespace, sub_namespace) ;
223- let sqlite_res = self . sqlite_store . list ( namespace, sub_namespace) ;
224- let test_res = self . test_store . list ( namespace, sub_namespace) ;
225-
226- match fs_res {
227- Ok ( mut list) => {
228- list. sort ( ) ;
229-
230- let mut sqlite_list = sqlite_res. unwrap ( ) ;
231- sqlite_list. sort ( ) ;
232- assert_eq ! ( list, sqlite_list) ;
233-
234- let mut test_list = test_res. unwrap ( ) ;
235- test_list. sort ( ) ;
236- assert_eq ! ( list, test_list) ;
237-
238- Ok ( list)
239- }
240- Err ( e) => {
241- assert ! ( sqlite_res. is_err( ) ) ;
242- assert ! ( test_res. is_err( ) ) ;
243- Err ( e)
244- }
245- }
278+ fn list (
279+ & self , primary_namespace : & str , secondary_namespace : & str ,
280+ ) -> std:: io:: Result < Vec < String > > {
281+ let _guard = self . serializer . read ( ) . unwrap ( ) ;
282+ self . do_list ( primary_namespace, secondary_namespace)
246283 }
247284}
0 commit comments