diff --git a/src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs b/src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs index 85a79762acda..36ab108cbd36 100644 --- a/src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs +++ b/src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs @@ -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) { @@ -207,9 +208,12 @@ 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); @@ -217,12 +221,6 @@ void EnsureSession(ISession? session) { 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 { @@ -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)