Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public async Task Will_accept_static_connection()
[TestCase(false, ConnectionDirection.In)]
// [TestCase(true, ConnectionDirection.Out)] // cannot create an active peer waiting for the test
[TestCase(false, ConnectionDirection.Out)]
[NonParallelizable]
public async Task Will_agree_on_which_session_to_disconnect_when_connecting_at_once(bool shouldLose,
ConnectionDirection firstDirection)
{
Expand All @@ -207,22 +208,19 @@ void EnsureSession(ISession? session)
{
if (session is null) return;
if (session.State < SessionState.HandshakeComplete) session.Handshake(session.Node.Id);
session.Init(5, context, packetSender);
if (session.State < SessionState.Initialized) session.Init(5, context, packetSender);
}

bool expectedOutSessionClosing = firstDirection == ConnectionDirection.In ? shouldLose : !shouldLose;
bool expectedInSessionClosing = !expectedOutSessionClosing;

if (firstDirection == ConnectionDirection.In)
{
ctx.RlpxPeer.CreateIncoming(session1);
if (!await ctx.RlpxPeer.ConnectAsync(session1.Node))
{
throw new NetworkingException($"Failed to connect to {session1.Node:s}", NetworkExceptionType.TargetUnreachable);
}

EnsureSession(ctx.PeerManager.ActivePeers.First().OutSession);
EnsureSession(ctx.PeerManager.ActivePeers.First().InSession);

(ctx.PeerManager.ActivePeers.First().OutSession?.IsClosing ?? true).Should().Be(shouldLose);
(ctx.PeerManager.ActivePeers.First().InSession?.IsClosing ?? true).Should().Be(!shouldLose);
}
else
{
Expand All @@ -233,15 +231,23 @@ void EnsureSession(ISession? session)
}
ctx.RlpxPeer.SessionCreated -= HandshakeOnCreate;
ctx.RlpxPeer.CreateIncoming(session1);
}

EnsureSession(ctx.PeerManager.ActivePeers.First().OutSession);
EnsureSession(ctx.PeerManager.ActivePeers.First().InSession);
Assert.That(() =>
{
Peer? activePeer = ctx.PeerManager.ActivePeers.SingleOrDefault();
if (activePeer is null) return false;

(ctx.PeerManager.ActivePeers.First().OutSession?.IsClosing ?? true).Should().Be(!shouldLose);
(ctx.PeerManager.ActivePeers.First().InSession?.IsClosing ?? true).Should().Be(shouldLose);
}
EnsureSession(activePeer.OutSession);
EnsureSession(activePeer.InSession);

ctx.PeerManager.ActivePeers.Count.Should().Be(1);
return activePeer.OutSession is not null
&& activePeer.InSession is not null
&& activePeer.OutSession.IsClosing == expectedOutSessionClosing
&& activePeer.InSession.IsClosing == expectedInSessionClosing;
}, Is.True.After(_delayLonger, 20));

Assert.That(() => ctx.PeerManager.ActivePeers.Count, Is.EqualTo(1).After(_delay, 10));
}

private void HandshakeOnCreate(object sender, SessionEventArgs e)
Expand Down
Loading