@@ -32,7 +32,7 @@ use p2p_test_utils::run_with_timeout;
3232use randomness:: Rng ;
3333use serialization:: Encode as _;
3434use test_utils:: {
35- assert_matches,
35+ assert_matches, assert_matches_return_val ,
3636 random:: { make_seedable_rng, Seed } ,
3737 BasicTestTimeGetter ,
3838} ;
@@ -41,7 +41,7 @@ use crate::{
4141 config:: { BackendTimeoutsConfig , P2pConfig } ,
4242 message:: { HeaderList , HeaderListRequest } ,
4343 net:: {
44- default_backend:: types:: { HandshakeMessage , Message , P2pTimestamp } ,
44+ default_backend:: types:: { HandshakeMessage , Message , MessageTag , P2pTimestamp } ,
4545 types:: PeerManagerMessageExtTag ,
4646 } ,
4747 sync:: test_helpers:: make_new_blocks,
@@ -242,35 +242,56 @@ async fn no_connection_lockup_when_socket_not_read(#[case] seed: Seed) {
242242 . await ;
243243}
244244
245- // Similar to the test above, here the peer also stops reading from the socket, but it doesn't do
246- // any discourageable actions this time.
247- // The expected result is that the connection should also be terminated.
248- // This checks that there is a timeout on socket write attempts.
245+ #[ derive( Debug ) ]
246+ enum TimeoutTestType {
247+ SocketWrite ,
248+ Disconnect ,
249+ }
250+
251+ // Here the peer also stops reading from the socket, but doesn't do any discourageable actions.
252+ // Two cases exist:
253+ // 1) Socket write timeout is small, disconnection timeout is artificially large (just in case).
254+ // The expected result is that the connection should be terminated, though not immediately.
255+ // I.e. this checks that there is a timeout on socket write attempts.
256+ // 2) Disconnection timeout is small, socket write timeout is artificially large.
257+ // We wait until the HeaderList message has been sent by the node and then initiate manual
258+ // disconnection. The expected result is the same - the connection should be terminated,
259+ // but not immediately.
260+ // I.e. this checks that there is a timeout on disconnection attempts.
249261#[ tracing:: instrument( skip( seed) ) ]
250262#[ rstest]
251263#[ trace]
252264#[ case( Seed :: from_entropy( ) ) ]
253265#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
254- async fn timeout_when_socket_not_read ( #[ case] seed : Seed ) {
266+ async fn timeout_when_socket_not_read (
267+ #[ case] seed : Seed ,
268+ #[ values( TimeoutTestType :: SocketWrite , TimeoutTestType :: Disconnect ) ] test_type : TimeoutTestType ,
269+ ) {
255270 let mut rng = make_seedable_rng ( seed) ;
256271
257272 run_with_timeout ( async {
258273 // Note: this is in bytes; this size should be less than the encoded size of `node_headers_count` headers.
259274 let channel_max_buf_size = 1000 ;
260275 let node_headers_count = 100 ;
261276
262- let socket_write_timeout = Duration :: from_secs ( 3 ) ;
277+ let timeout = Duration :: from_secs ( 3 ) ;
278+ let large_timeout = Duration :: from_secs ( 3600 ) ;
263279 let no_disconnection_after = Duration :: from_secs ( 1 ) ;
264280
281+ let ( socket_write_timeout, disconnection_timeout) = match test_type {
282+ TimeoutTestType :: SocketWrite => ( timeout, large_timeout) ,
283+ TimeoutTestType :: Disconnect => ( large_timeout, timeout) ,
284+ } ;
285+
265286 let time_getter = BasicTestTimeGetter :: new ( ) ;
266287 let chain_config = Arc :: new ( chain:: config:: create_unit_test_config ( ) ) ;
267288 let p2p_config = Arc :: new ( P2pConfig {
268289 backend_timeouts : BackendTimeoutsConfig {
269290 socket_write_timeout : socket_write_timeout. into ( ) ,
291+ disconnection_timeout : disconnection_timeout. into ( ) ,
270292
271293 outbound_connection_timeout : Default :: default ( ) ,
272294 peer_handshake_timeout : Default :: default ( ) ,
273- disconnection_timeout : Default :: default ( ) ,
274295 } ,
275296
276297 bind_addresses : Default :: default ( ) ,
@@ -382,9 +403,10 @@ async fn timeout_when_socket_not_read(#[case] seed: Seed) {
382403
383404 log:: debug!( "Expecting PeerManagerNotification::ConnectionAccepted" ) ;
384405 let peer_mgr_notif = test_node. peer_mgr_notification_receiver ( ) . recv ( ) . await . unwrap ( ) ;
385- assert_matches ! (
406+ let peer_id = assert_matches_return_val ! (
386407 peer_mgr_notif,
387- PeerManagerNotification :: ConnectionAccepted { .. }
408+ PeerManagerNotification :: ConnectionAccepted { peer_id, .. } ,
409+ peer_id
388410 ) ;
389411
390412 log:: debug!( "Expecting PeerManagerNotification::FirstSyncMessageReceived" ) ;
@@ -397,6 +419,16 @@ async fn timeout_when_socket_not_read(#[case] seed: Seed) {
397419 }
398420 ) ;
399421
422+ let manual_disconnect_result_receiver = match test_type {
423+ TimeoutTestType :: SocketWrite => None ,
424+ TimeoutTestType :: Disconnect => {
425+ test_node
426+ . wait_for_next_backend_socket_write ( peer_id, MessageTag :: HeaderList )
427+ . await ;
428+ Some ( test_node. start_disconnecting ( peer_id) )
429+ }
430+ } ;
431+
400432 // Wait for a short period of time, there should be no disconnection.
401433 tokio:: time:: timeout (
402434 no_disconnection_after,
@@ -413,6 +445,10 @@ async fn timeout_when_socket_not_read(#[case] seed: Seed) {
413445 PeerManagerNotification :: ConnectionClosed { .. }
414446 ) ;
415447
448+ if let Some ( manual_disconnect_result_receiver) = manual_disconnect_result_receiver {
449+ manual_disconnect_result_receiver. await . unwrap ( ) . unwrap ( ) ;
450+ }
451+
416452 test_node. join ( ) . await ;
417453 } )
418454 . await ;
0 commit comments