-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The old PeerManagerTest was located under network/p2p/routing, which is no longer the correct location. Additionally, it was outdated so I just removed it and added a new file under network/p2p/peers containing tests for checkMaxConnections.
- Loading branch information
1 parent
6f80b8a
commit 500a90a
Showing
2 changed files
with
187 additions
and
486 deletions.
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
p2p/src/test/java/bisq/network/p2p/PeerManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.network.p2p.peers; | ||
|
||
import bisq.network.p2p.network.CloseConnectionReason; | ||
import bisq.network.p2p.network.Connection; | ||
import bisq.network.p2p.network.InboundConnection; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class PeerManagerTest { | ||
private MockNode node; | ||
private int maxConnectionsPeer; | ||
private int maxConnectionsNonDirect; | ||
|
||
@Before | ||
public void Setup() { | ||
node = new MockNode(2); | ||
maxConnectionsPeer = Math.max(4, (int) Math.round(node.getMaxConnections() * 1.3)); | ||
maxConnectionsNonDirect = Math.max(8, (int) Math.round(node.getMaxConnections() * 1.7)); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsNotExceeded() { | ||
for (int i = 0; i < 2; i++) { | ||
node.addInboundConnection(Connection.PeerType.PEER); | ||
} | ||
assertEquals(2, node.getNetworkNode().getAllConnections().size()); | ||
|
||
assertFalse(node.getPeerManager().checkMaxConnections()); | ||
|
||
node.getNetworkNode().getAllConnections().forEach(connection -> { | ||
verify(connection, never()).shutDown(eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsExceededWithInboundPeers() throws InterruptedException { | ||
for (int i = 0; i < 3; i++) { | ||
node.addInboundConnection(Connection.PeerType.PEER); | ||
} | ||
assertEquals(3, node.getNetworkNode().getAllConnections().size()); | ||
List<Connection> inboundSortedPeerConnections = node.getNetworkNode().getAllConnections().stream() | ||
.filter(e -> e instanceof InboundConnection) | ||
.filter(e -> e.getPeerType() == Connection.PeerType.PEER) | ||
.sorted(Comparator.comparingLong(o -> o.getStatistic().getLastActivityTimestamp())) | ||
.collect(Collectors.toList()); | ||
Connection oldestConnection = inboundSortedPeerConnections.remove(0); | ||
|
||
assertTrue(node.getPeerManager().checkMaxConnections()); | ||
// Need to wait because the shutDownCompleteHandler calls | ||
// checkMaxConnections on the user thread after a delay | ||
Thread.sleep(500); | ||
|
||
verify(oldestConnection, times(1)).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
inboundSortedPeerConnections.forEach(connection -> { | ||
verify(connection, never()).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsPeerLimitNotExceeded() { | ||
for (int i = 0; i < maxConnectionsPeer; i++) { | ||
node.addOutboundConnection(Connection.PeerType.PEER); | ||
} | ||
assertEquals(maxConnectionsPeer, node.getNetworkNode().getAllConnections().size()); | ||
|
||
assertFalse(node.getPeerManager().checkMaxConnections()); | ||
|
||
node.getNetworkNode().getAllConnections().forEach(connection -> { | ||
verify(connection, never()).shutDown(eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsPeerLimitExceeded() throws InterruptedException { | ||
for (int i = 0; i < maxConnectionsPeer + 1; i++) { | ||
node.addOutboundConnection(Connection.PeerType.PEER); | ||
} | ||
assertEquals(maxConnectionsPeer + 1, node.getNetworkNode().getAllConnections().size()); | ||
List<Connection> sortedPeerConnections = node.getNetworkNode().getAllConnections().stream() | ||
.filter(e -> e.getPeerType() == Connection.PeerType.PEER) | ||
.sorted(Comparator.comparingLong(o -> o.getStatistic().getLastActivityTimestamp())) | ||
.collect(Collectors.toList()); | ||
Connection oldestConnection = sortedPeerConnections.remove(0); | ||
|
||
assertTrue(node.getPeerManager().checkMaxConnections()); | ||
// Need to wait because the shutDownCompleteHandler calls | ||
// checkMaxConnections on the user thread after a delay | ||
Thread.sleep(500); | ||
|
||
verify(oldestConnection, times(1)).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
sortedPeerConnections.forEach(connection -> { | ||
verify(connection, never()).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsNonDirectLimitNotExceeded() { | ||
for (int i = 0; i < maxConnectionsNonDirect; i++) { | ||
node.addOutboundConnection(Connection.PeerType.SEED_NODE); | ||
} | ||
assertEquals(maxConnectionsNonDirect, node.getNetworkNode().getAllConnections().size()); | ||
|
||
assertFalse(node.getPeerManager().checkMaxConnections()); | ||
|
||
node.getNetworkNode().getAllConnections().forEach(connection -> { | ||
verify(connection, never()).shutDown(eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsNonDirectLimitExceeded() throws InterruptedException { | ||
for (int i = 0; i < maxConnectionsNonDirect + 1; i++) { | ||
node.addOutboundConnection(Connection.PeerType.PEER); | ||
} | ||
assertEquals(maxConnectionsNonDirect + 1, node.getNetworkNode().getAllConnections().size()); | ||
List<Connection> sortedPeerConnections = node.getNetworkNode().getAllConnections().stream() | ||
.filter(e -> e.getPeerType() != Connection.PeerType.DIRECT_MSG_PEER && | ||
e.getPeerType() != Connection.PeerType.INITIAL_DATA_REQUEST) | ||
.sorted(Comparator.comparingLong(o -> o.getStatistic().getLastActivityTimestamp())) | ||
.collect(Collectors.toList()); | ||
Connection oldestConnection = sortedPeerConnections.remove(0); | ||
|
||
assertTrue(node.getPeerManager().checkMaxConnections()); | ||
// Need to wait because the shutDownCompleteHandler calls | ||
// checkMaxConnections on the user thread after a delay | ||
Thread.sleep(500); | ||
|
||
verify(oldestConnection, times(1)).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
sortedPeerConnections.forEach(connection -> { | ||
verify(connection, never()).shutDown( | ||
eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), | ||
isA(Runnable.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCheckMaxConnectionsExceededWithOutboundSeeds() { | ||
for (int i = 0; i < 3; i++) { | ||
node.addOutboundConnection(Connection.PeerType.SEED_NODE); | ||
} | ||
assertEquals(3, node.getNetworkNode().getAllConnections().size()); | ||
|
||
assertFalse(node.getPeerManager().checkMaxConnections()); | ||
|
||
node.getNetworkNode().getAllConnections().forEach(connection -> { | ||
verify(connection, never()).shutDown(eq(CloseConnectionReason.TOO_MANY_CONNECTIONS_OPEN), isA(Runnable.class)); | ||
}); | ||
} | ||
} |
Oops, something went wrong.