@@ -44,8 +44,8 @@ const CHAN_FRESHNESS_TIMER: u64 = 60;
44
44
const CHAN_FRESHNESS_TIMER : u64 = 1 ;
45
45
46
46
impl BackgroundProcessor {
47
- /// Start the background thread that takes care of responsibilities ( enumerated in the top-level
48
- /// documentation) . Marked as `must_use` because otherwise the result is dropped immediately,
47
+ /// Start a background thread that takes care of responsibilities enumerated in the top-level
48
+ /// documentation. Marked as `must_use` because otherwise the result is dropped immediately,
49
49
/// resulting in the thread being terminated.
50
50
/// Important note: this thread will panic if invoking `persist_manager` results in an error (and
51
51
/// `start()` will need to be called again to restart the `BackgroundProcessor`).
@@ -84,7 +84,7 @@ impl BackgroundProcessor {
84
84
panic ! ( "Errored persisting manager: {}" , e) ;
85
85
} ;
86
86
}
87
- // If we see that the thread has been stopped, exit now .
87
+ // Exit the loop if the background processor was requested to stop .
88
88
if stop_thread. load ( Ordering :: Acquire ) == true {
89
89
log_trace ! ( logger, "Terminating background processor." ) ;
90
90
break ;
@@ -214,70 +214,51 @@ mod tests {
214
214
let nodes = create_nodes ( 2 , "test_background_processor" . to_string ( ) ) ;
215
215
216
216
// Initiate the background processors to watch each node.
217
- let data_dir_0 = nodes[ 0 ] . persister . get_data_dir ( ) ;
218
- let data_dir_1 = nodes[ 1 ] . persister . get_data_dir ( ) ;
219
- let callback_0 = move |node| FilesystemPersister :: persist_manager ( data_dir_0. clone ( ) , node) ;
220
- let callback_1 = move |node| FilesystemPersister :: persist_manager ( data_dir_1. clone ( ) , node) ;
221
- let _processor_0 = BackgroundProcessor :: start ( callback_0, nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
222
- let _processor_1 = BackgroundProcessor :: start ( callback_1, nodes[ 1 ] . node . clone ( ) , nodes[ 1 ] . logger . clone ( ) ) ;
217
+ let data_dir = nodes[ 0 ] . persister . get_data_dir ( ) ;
218
+ let callback = move |node| FilesystemPersister :: persist_manager ( data_dir. clone ( ) , node) ;
219
+ let _processor = BackgroundProcessor :: start ( callback, nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
223
220
224
221
// Go through the channel creation process until each node should have something persisted.
225
222
let tx = open_channel ! ( nodes[ 0 ] , nodes[ 1 ] , 100000 ) ;
226
223
227
- let mut done_persisting = false ;
228
224
macro_rules! check_persisted_data {
229
- ( $node: expr, $filepath: expr) => {
230
- let bytes = loop {
231
- match std:: fs:: read( $filepath) {
232
- Ok ( bytes) => break bytes,
233
- Err ( _) => continue
234
- }
235
- } ;
236
- let mut expected_bytes = Vec :: new( ) ;
237
- assert!( $node. write( & mut expected_bytes) . is_ok( ) ) ;
238
- if bytes == expected_bytes {
239
- done_persisting = true ;
225
+ ( $node: expr, $filepath: expr, $expected_bytes: expr) => {
226
+ match $node. write( & mut $expected_bytes) {
227
+ Ok ( ( ) ) => {
228
+ loop {
229
+ match std:: fs:: read( $filepath) {
230
+ Ok ( bytes) => {
231
+ if bytes == $expected_bytes {
232
+ break
233
+ } else {
234
+ continue
235
+ }
236
+ } ,
237
+ Err ( _) => continue
238
+ }
239
+ }
240
+ } ,
241
+ Err ( e) => panic!( "Unexpected error: {}" , e)
240
242
}
241
243
}
242
244
}
243
245
244
246
// Check that the initial channel manager data is persisted as expected.
245
- let filepath_0 = get_full_filepath ( "test_background_processor_persister_0" . to_string ( ) , "manager" . to_string ( ) ) ;
246
- let filepath_1 = get_full_filepath ( "test_background_processor_persister_1" . to_string ( ) , "manager" . to_string ( ) ) ;
247
+ let filepath = get_full_filepath ( "test_background_processor_persister_0" . to_string ( ) , "manager" . to_string ( ) ) ;
248
+ let mut expected_bytes = Vec :: new ( ) ;
249
+ check_persisted_data ! ( nodes[ 0 ] . node, filepath. clone( ) , expected_bytes) ;
247
250
loop {
248
- check_persisted_data ! ( nodes[ 0 ] . node, filepath_0. clone( ) ) ;
249
- if done_persisting {
250
- // Check that eventually BackgroundProcessor resets the condvar when everything's done persisting.
251
- loop {
252
- if !nodes[ 0 ] . node . get_persistence_condvar_value ( ) { break }
253
- }
254
- break
255
- }
256
- }
257
- done_persisting = false ;
258
- loop {
259
- check_persisted_data ! ( nodes[ 1 ] . node, filepath_1. clone( ) ) ;
260
- if done_persisting {
261
- loop {
262
- if !nodes[ 1 ] . node . get_persistence_condvar_value ( ) { break }
263
- }
264
- break
265
- }
251
+ if !nodes[ 0 ] . node . get_persistence_condvar_value ( ) { break }
266
252
}
267
253
268
254
// Force-close the channel.
269
- nodes[ 0 ] . node . force_close_channel ( & OutPoint { txid : tx. txid ( ) , index : 0 } . to_channel_id ( ) ) ;
255
+ nodes[ 0 ] . node . force_close_channel ( & OutPoint { txid : tx. txid ( ) , index : 0 } . to_channel_id ( ) ) . unwrap ( ) ;
270
256
271
257
// Check that the force-close updates are persisted.
272
- done_persisting = false ;
258
+ let mut expected_bytes = Vec :: new ( ) ;
259
+ check_persisted_data ! ( nodes[ 0 ] . node, filepath. clone( ) , expected_bytes) ;
273
260
loop {
274
- check_persisted_data ! ( nodes[ 0 ] . node, filepath_0. clone( ) ) ;
275
- if done_persisting {
276
- loop {
277
- if !nodes[ 0 ] . node . get_persistence_condvar_value ( ) { break }
278
- }
279
- break
280
- }
261
+ if !nodes[ 0 ] . node . get_persistence_condvar_value ( ) { break }
281
262
}
282
263
}
283
264
@@ -314,6 +295,8 @@ mod tests {
314
295
Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "test" ) )
315
296
}
316
297
298
+ // We don't want the expected panic to print to the console during testing, so
299
+ // swallow it here.
317
300
std:: panic:: set_hook ( Box :: new ( |panic_info| {
318
301
if let Some ( s) = panic_info. payload ( ) . downcast_ref :: < String > ( ) {
319
302
assert_eq ! ( s, "Errored persisting manager: test" ) ;
0 commit comments