Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test] Add unit test for validator Router connections #3198

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/router/tests/cleanups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn test_connection_cleanups() {
let node = match rng.gen_range(0..3) % 3 {
0 => client(0, 1).await,
1 => prover(0, 1).await,
2 => validator(0, 1).await,
2 => validator(0, 1, &[], true).await,
_ => unreachable!(),
};

Expand Down
11 changes: 8 additions & 3 deletions node/router/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,19 @@ pub async fn prover(listening_port: u16, max_peers: u16) -> TestRouter<CurrentNe

/// Initializes a validator router. Setting the `listening_port = 0` will result in a random port being assigned.
#[allow(dead_code)]
pub async fn validator(listening_port: u16, max_peers: u16) -> TestRouter<CurrentNetwork> {
pub async fn validator(
listening_port: u16,
max_peers: u16,
trusted_peers: &[SocketAddr],
allow_external_peers: bool,
) -> TestRouter<CurrentNetwork> {
Router::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), listening_port),
NodeType::Validator,
sample_account(),
&[],
trusted_peers,
max_peers,
true, // Router tests require validators to connect to peers.
allow_external_peers,
true,
)
.await
Expand Down
59 changes: 56 additions & 3 deletions node/router/tests/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use core::time::Duration;
#[tokio::test]
async fn test_connect_without_handshake() {
// Create 2 routers.
let node0 = validator(0, 2).await;
let node0 = validator(0, 2, &[], true).await;
let node1 = client(0, 2).await;
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
Expand Down Expand Up @@ -78,7 +78,7 @@ async fn test_connect_without_handshake() {
#[tokio::test]
async fn test_connect_with_handshake() {
// Create 2 routers.
let node0 = validator(0, 2).await;
let node0 = validator(0, 2, &[], true).await;
let node1 = client(0, 2).await;
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
Expand Down Expand Up @@ -150,11 +150,64 @@ async fn test_connect_with_handshake() {
}
}

#[tokio::test]
async fn test_validator_connection() {
// Create first router and start listening.
let node0 = validator(0, 2, &[], false).await;
assert_eq!(node0.number_of_connected_peers(), 0);
node0.enable_handshake().await;
node0.tcp().enable_listener().await.unwrap();

// Get the local IP address from the first router.
let addr0 = node0.local_ip();

// Create second router, trusting the first router, and start listening.
let node1 = validator(0, 2, &[addr0], false).await;
assert_eq!(node1.number_of_connected_peers(), 0);
node1.enable_handshake().await;
node1.tcp().enable_listener().await.unwrap();

{
// Connect node0 to node1.
node0.connect(node1.local_ip());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't we racing against the Heartbeat (that might cause node1 to attempt to connect to node0) here? then again, that direction should fail, so it's probably fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heartbeat is not turned on, only if we call initialize_routing.

// Sleep briefly.
tokio::time::sleep(Duration::from_millis(200)).await;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deadline could be used here to avoid the sleep.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will keep it in mind for future tests, for now I'll keep the logic similar to the rest of the tests in the file.

Copy link
Collaborator

@ljedrz ljedrz Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC connect returns a JoinHandle that can be .awaited (and once it's complete, it means the connection is either ready or that it has failed), so there is no need for a sleep here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried it. Just awaiting node0's connect is not enough, because node1 might not be connected yet. I suggest we leave refactoring this file for later.


print_tcp!(node0);
print_tcp!(node1);

// Check the TCP level - connection was accepted.
assert_eq!(node0.tcp().num_connected(), 1);
assert_eq!(node1.tcp().num_connected(), 1);

// Check the router level - connection was accepted.
assert_eq!(node0.number_of_connected_peers(), 1);
assert_eq!(node1.number_of_connected_peers(), 1);

// Disconnect the nodes.
node0.disconnect(node1.local_ip());
node1.disconnect(node0.local_ip());

// Connect node1 to node0.
node1.connect(node0.local_ip());
// Sleep briefly.
tokio::time::sleep(Duration::from_millis(200)).await;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto comment above


// Check the TCP level - connection was not accepted.
assert_eq!(node0.tcp().num_connected(), 0);
assert_eq!(node1.tcp().num_connected(), 0);

// Check the router level - connection was not accepted.
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
}
}

#[ignore]
#[tokio::test]
async fn test_connect_simultaneously_with_handshake() {
// Create 2 routers.
let node0 = validator(0, 2).await;
let node0 = validator(0, 2, &[], true).await;
let node1 = client(0, 2).await;
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
Expand Down
4 changes: 2 additions & 2 deletions node/router/tests/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use core::time::Duration;
#[tokio::test]
async fn test_disconnect_without_handshake() {
// Create 2 routers.
let node0 = validator(0, 1).await;
let node0 = validator(0, 1, &[], true).await;
let node1 = client(0, 1).await;
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn test_disconnect_without_handshake() {
#[tokio::test]
async fn test_disconnect_with_handshake() {
// Create 2 routers.
let node0 = validator(0, 1).await;
let node0 = validator(0, 1, &[], true).await;
let node1 = client(0, 1).await;
assert_eq!(node0.number_of_connected_peers(), 0);
assert_eq!(node1.number_of_connected_peers(), 0);
Expand Down