add OpenSent state

This commit is contained in:
Masato Imai
2025-02-24 15:05:33 +00:00
parent dc0c9129c9
commit b490971a6d
3 changed files with 30 additions and 16 deletions

View File

@ -42,22 +42,8 @@ impl Connection {
"waiting connection from remote peer, local-ip={:?}, bgp-port={}",
config.local_ip, BGP_PORT
);
let listener = TcpListener::bind((config.local_ip, BGP_PORT))
.await
.unwrap()
/*.context(format!(
"cannot bind to local-ip={:?}, bgp-port={}",
config.local_ip, BGP_PORT
))?*/;
let listener = TcpListener::bind((config.local_ip, BGP_PORT)).await?;
Ok(listener
.accept()
.await
.unwrap()
/*.context(format!(
"cannot accept connection from remote peer, local-ip={:?}, bgp-port={}",
config.local_ip, BGP_PORT
))?*/
.0)
Ok(listener.accept().await?.0)
}
}

View File

@ -52,6 +52,13 @@ impl Peer {
}
_ => {}
},
State::Connect => match event {
Event::TcpConnectionConfirmed => {
info!("state transitioned from Connect to OpenSent");
self.state = State::OpenSent;
}
_ => {}
},
_ => {}
}
}
@ -82,4 +89,24 @@ mod tests {
peer.next().await;
assert_eq!(peer.state, State::Connect);
}
#[tokio::test]
async fn peer_can_transition_to_open_sent_state() {
let config: Config = "64512 127.0.0.1 65413 127.0.0.2 active".parse().unwrap();
let mut peer = Peer::new(config);
peer.start();
tokio::spawn(async move {
let remote_config = "64513 127.0.0.2 65412 127.0.0.1 passive".parse().unwrap();
let mut remote_peer = Peer::new(remote_config);
remote_peer.start();
remote_peer.next().await;
remote_peer.next().await;
});
tokio::time::sleep(Duration::from_secs(1)).await;
peer.next().await;
peer.next().await;
assert_eq!(peer.state, State::OpenSent);
}
}

View File

@ -2,4 +2,5 @@
pub enum State {
Idle,
Connect,
OpenSent,
}