Skip to content

Commit 6a613d8

Browse files
committed
peer_state/tests: Check on dial failure
Signed-off-by: Alexandru Vasile <[email protected]>
1 parent 5959e4e commit 6a613d8

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/transport/manager/peer_state.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,83 @@ mod tests {
523523
}
524524
);
525525
}
526+
527+
#[test]
528+
fn check_dial_failure() {
529+
let record = ConnectionRecord::new(
530+
PeerId::random(),
531+
"/ip4/1.1.1.1/tcp/80".parse().unwrap(),
532+
ConnectionId::from(0),
533+
);
534+
535+
// Check from the dialing state.
536+
{
537+
let mut state = PeerState::Dialing {
538+
dial_record: record.clone(),
539+
};
540+
let previous_state = state.clone();
541+
// Check with different connection ID.
542+
state.on_dial_failure(ConnectionId::from(1));
543+
assert_eq!(state, previous_state);
544+
545+
// Check with the same connection ID.
546+
state.on_dial_failure(ConnectionId::from(0));
547+
assert_eq!(state, PeerState::Disconnected { dial_record: None });
548+
}
549+
550+
// Check from the connected state without dialing state.
551+
{
552+
let mut state = PeerState::Connected {
553+
record: record.clone(),
554+
secondary: None,
555+
};
556+
let previous_state = state.clone();
557+
// Check with different connection ID.
558+
state.on_dial_failure(ConnectionId::from(1));
559+
assert_eq!(state, previous_state);
560+
561+
// Check with the same connection ID.
562+
// The connection ID is checked against dialing records, not established connections.
563+
state.on_dial_failure(ConnectionId::from(0));
564+
assert_eq!(state, previous_state);
565+
}
566+
567+
// Check from the connected state with dialing state.
568+
{
569+
let mut state = PeerState::Connected {
570+
record: record.clone(),
571+
secondary: Some(SecondaryOrDialing::Dialing(record.clone())),
572+
};
573+
let previous_state = state.clone();
574+
// Check with different connection ID.
575+
state.on_dial_failure(ConnectionId::from(1));
576+
assert_eq!(state, previous_state);
577+
578+
// Check with the same connection ID.
579+
// Dial record is cleared.
580+
state.on_dial_failure(ConnectionId::from(0));
581+
assert_eq!(
582+
state,
583+
PeerState::Connected {
584+
record: record.clone(),
585+
secondary: None,
586+
}
587+
);
588+
}
589+
590+
// Check from the disconnected state.
591+
{
592+
let mut state = PeerState::Disconnected {
593+
dial_record: Some(record.clone()),
594+
};
595+
let previous_state = state.clone();
596+
// Check with different connection ID.
597+
state.on_dial_failure(ConnectionId::from(1));
598+
assert_eq!(state, previous_state);
599+
600+
// Check with the same connection ID.
601+
state.on_dial_failure(ConnectionId::from(0));
602+
assert_eq!(state, PeerState::Disconnected { dial_record: None });
603+
}
604+
}
526605
}

0 commit comments

Comments
 (0)