@@ -15,6 +15,7 @@ use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event
15
15
16
16
use std:: panic:: RefUnwindSafe ;
17
17
use std:: path:: PathBuf ;
18
+ use std:: sync:: RwLock ;
18
19
19
20
pub ( crate ) fn do_read_write_remove_list_persist < K : KVStore + RefUnwindSafe > ( kv_store : & K ) {
20
21
let data = [ 42u8 ; 32 ] ;
@@ -174,13 +175,15 @@ pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
174
175
175
176
// A `KVStore` impl for testing purposes that wraps all our `KVStore`s and asserts their synchronicity.
176
177
pub ( crate ) struct TestSyncStore {
178
+ serializer : RwLock < ( ) > ,
177
179
test_store : TestStore ,
178
180
fs_store : FilesystemStore ,
179
181
sqlite_store : SqliteStore ,
180
182
}
181
183
182
184
impl TestSyncStore {
183
185
pub ( crate ) fn new ( dest_dir : PathBuf ) -> Self {
186
+ let serializer = RwLock :: new ( ( ) ) ;
184
187
let mut fs_dir = dest_dir. clone ( ) ;
185
188
fs_dir. push ( "fs_store" ) ;
186
189
let fs_store = FilesystemStore :: new ( fs_dir) ;
@@ -193,12 +196,41 @@ impl TestSyncStore {
193
196
)
194
197
. unwrap ( ) ;
195
198
let test_store = TestStore :: new ( ) ;
196
- Self { fs_store, sqlite_store, test_store }
199
+ Self { serializer, fs_store, sqlite_store, test_store }
200
+ }
201
+
202
+ fn do_list ( & self , namespace : & str , sub_namespace : & str ) -> std:: io:: Result < Vec < String > > {
203
+ let fs_res = self . fs_store . list ( namespace, sub_namespace) ;
204
+ let sqlite_res = self . sqlite_store . list ( namespace, sub_namespace) ;
205
+ let test_res = self . test_store . list ( namespace, sub_namespace) ;
206
+
207
+ match fs_res {
208
+ Ok ( mut list) => {
209
+ list. sort ( ) ;
210
+
211
+ let mut sqlite_list = sqlite_res. unwrap ( ) ;
212
+ sqlite_list. sort ( ) ;
213
+ assert_eq ! ( list, sqlite_list) ;
214
+
215
+ let mut test_list = test_res. unwrap ( ) ;
216
+ test_list. sort ( ) ;
217
+ assert_eq ! ( list, test_list) ;
218
+
219
+ Ok ( list)
220
+ }
221
+ Err ( e) => {
222
+ assert ! ( sqlite_res. is_err( ) ) ;
223
+ assert ! ( test_res. is_err( ) ) ;
224
+ Err ( e)
225
+ }
226
+ }
197
227
}
198
228
}
199
229
200
230
impl KVStore for TestSyncStore {
201
231
fn read ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> std:: io:: Result < Vec < u8 > > {
232
+ let _guard = self . serializer . read ( ) . unwrap ( ) ;
233
+
202
234
let fs_res = self . fs_store . read ( namespace, sub_namespace, key) ;
203
235
let sqlite_res = self . sqlite_store . read ( namespace, sub_namespace, key) ;
204
236
let test_res = self . test_store . read ( namespace, sub_namespace, key) ;
@@ -222,11 +254,12 @@ impl KVStore for TestSyncStore {
222
254
fn write (
223
255
& self , namespace : & str , sub_namespace : & str , key : & str , buf : & [ u8 ] ,
224
256
) -> std:: io:: Result < ( ) > {
257
+ let _guard = self . serializer . write ( ) . unwrap ( ) ;
225
258
let fs_res = self . fs_store . write ( namespace, sub_namespace, key, buf) ;
226
259
let sqlite_res = self . sqlite_store . write ( namespace, sub_namespace, key, buf) ;
227
260
let test_res = self . test_store . write ( namespace, sub_namespace, key, buf) ;
228
261
229
- assert ! ( self . list ( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
262
+ assert ! ( self . do_list ( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
230
263
231
264
match fs_res {
232
265
Ok ( ( ) ) => {
@@ -245,11 +278,12 @@ impl KVStore for TestSyncStore {
245
278
fn remove (
246
279
& self , namespace : & str , sub_namespace : & str , key : & str , lazy : bool ,
247
280
) -> std:: io:: Result < ( ) > {
281
+ let _guard = self . serializer . write ( ) . unwrap ( ) ;
248
282
let fs_res = self . fs_store . remove ( namespace, sub_namespace, key, lazy) ;
249
283
let sqlite_res = self . sqlite_store . remove ( namespace, sub_namespace, key, lazy) ;
250
284
let test_res = self . test_store . remove ( namespace, sub_namespace, key, lazy) ;
251
285
252
- assert ! ( !self . list ( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
286
+ assert ! ( !self . do_list ( namespace, sub_namespace) . unwrap( ) . contains( & key. to_string( ) ) ) ;
253
287
254
288
match fs_res {
255
289
Ok ( ( ) ) => {
@@ -266,29 +300,7 @@ impl KVStore for TestSyncStore {
266
300
}
267
301
268
302
fn list ( & self , namespace : & str , sub_namespace : & str ) -> std:: io:: Result < Vec < String > > {
269
- let fs_res = self . fs_store . list ( namespace, sub_namespace) ;
270
- let sqlite_res = self . sqlite_store . list ( namespace, sub_namespace) ;
271
- let test_res = self . test_store . list ( namespace, sub_namespace) ;
272
-
273
- match fs_res {
274
- Ok ( mut list) => {
275
- list. sort ( ) ;
276
-
277
- let mut sqlite_list = sqlite_res. unwrap ( ) ;
278
- sqlite_list. sort ( ) ;
279
- assert_eq ! ( list, sqlite_list) ;
280
-
281
- let mut test_list = test_res. unwrap ( ) ;
282
- test_list. sort ( ) ;
283
- assert_eq ! ( list, test_list) ;
284
-
285
- Ok ( list)
286
- }
287
- Err ( e) => {
288
- assert ! ( sqlite_res. is_err( ) ) ;
289
- assert ! ( test_res. is_err( ) ) ;
290
- Err ( e)
291
- }
292
- }
303
+ let _guard = self . serializer . read ( ) . unwrap ( ) ;
304
+ self . do_list ( namespace, sub_namespace)
293
305
}
294
306
}
0 commit comments