Skip to content

Commit

Permalink
Fix dummy transport (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Feb 7, 2024
1 parent 2ee2f3b commit 2bfa694
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/swarm/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl ConnectionHandshake for Swarm {
// The party with a smaller Did should reject answering the other party and report an Error::AlreadyConnected error.
if conn.webrtc_connection_state() == WebrtcConnectionState::New {
// drop local offer and continue answer remote offer
if peer > self.did() {
if self.did() > peer {
// this connection will replaced by new connection created bellow
self.disconnect(peer).await?;
} else {
Expand Down
25 changes: 14 additions & 11 deletions crates/core/src/tests/default/test_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use crate::message::handlers::tests::wait_for_msgs;
use crate::tests::default::prepare_node;
use crate::tests::manually_establish_connection;

// this function not work for dummy test
// TODO: need to fix it
#[cfg(not(feature = "dummy"))]
#[tokio::test]
async fn test_handshake_on_both_sides() -> Result<()> {
let key1 = SecretKey::random();
Expand All @@ -30,11 +27,20 @@ async fn test_handshake_on_both_sides() -> Result<()> {
wait_for_msgs(&swarm1, &swarm2, &swarm3).await;
assert_no_more_msg(&swarm1, &swarm2, &swarm3).await;

assert!(swarm3.get_connection(swarm1.did()).is_some());
assert!(swarm3.get_connection(swarm2.did()).is_some());

assert!(swarm1.get_connection(swarm3.did()).is_some());
assert!(swarm2.get_connection(swarm3.did()).is_some());
assert_eq!(
swarm3
.get_connection(swarm1.did())
.unwrap()
.webrtc_connection_state(),
WebrtcConnectionState::Connected
);
assert_eq!(
swarm3
.get_connection(swarm2.did())
.unwrap()
.webrtc_connection_state(),
WebrtcConnectionState::Connected
);

assert_eq!(
swarm1
Expand Down Expand Up @@ -81,9 +87,6 @@ async fn test_handshake_on_both_sides() -> Result<()> {
// and response answer
// When node 2 got offer, node 2 reject offer if did 1 < did 2

assert!(swarm1.get_connection(swarm2.did()).is_some());
assert!(swarm2.get_connection(swarm1.did()).is_some());

assert_eq!(
swarm1
.get_connection(swarm2.did())
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/tests/default/test_stabilization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ async fn test_stabilization() -> Result<()> {
Ok(())
}

// this function not work for dummy test
// TODO: need to fix it
#[cfg(not(feature = "dummy"))]
#[tokio::test]
async fn test_stabilization_final_dht() -> Result<()> {
let mut nodes = vec![];
Expand Down
8 changes: 7 additions & 1 deletion crates/transport/src/connections/dummy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ impl ConnectionInterface for DummyConnection {
if CHANNEL_OPEN_DELAY {
random_delay().await;
}
if self.webrtc_connection_state() == WebrtcConnectionState::Connected {

// Will pass if the state is connecting to prevent release connection in the `test_handshake_on_both_sides` test.
// The connecting state means an offer is answered but not accepted by the other side.
if matches!(
self.webrtc_connection_state(),
WebrtcConnectionState::Connected | WebrtcConnectionState::Connecting
) {
Ok(())
} else {
Err(Error::DataChannelOpen(
Expand Down

0 comments on commit 2bfa694

Please sign in to comment.