mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-27 16:17:37 -04:00
Refactor TomP2PTests
- Introduce use of Node abstraction for concision - Use to BootstrapNode#LOCALHOST and #DIGITAL_OCEAN1 vs. repeating info - Make all configuration variables static and final constants
This commit is contained in:
parent
655100e69f
commit
8b6f0ac64e
1 changed files with 99 additions and 168 deletions
|
@ -17,8 +17,13 @@
|
||||||
|
|
||||||
package io.bitsquare.msg;
|
package io.bitsquare.msg;
|
||||||
|
|
||||||
|
import io.bitsquare.network.BootstrapNode;
|
||||||
|
import io.bitsquare.network.Node;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
@ -63,104 +68,59 @@ import static org.junit.Assert.*;
|
||||||
* To configure your test environment edit the static fields for id, IP and port.
|
* To configure your test environment edit the static fields for id, IP and port.
|
||||||
* In the configure method and the connectionType you can define your test scenario.
|
* In the configure method and the connectionType you can define your test scenario.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public class TomP2PTests {
|
public class TomP2PTests {
|
||||||
private static final Logger log = LoggerFactory.getLogger(TomP2PTests.class);
|
private static final Logger log = LoggerFactory.getLogger(TomP2PTests.class);
|
||||||
|
|
||||||
/**
|
|
||||||
* Use UNKNOWN when you want to test the strategy to try first direct, then nat and lat relay
|
|
||||||
* Use on eof the others when you want to connect only with that mode. Be sure that you can really succeed in that
|
|
||||||
* mode (e.g. for NAT you need to have a UPnP or NAT PMP enabled router).
|
|
||||||
*/
|
|
||||||
private enum ConnectionType {
|
private enum ConnectionType {
|
||||||
UNKNOWN,
|
UNKNOWN, DIRECT, NAT, RELAY
|
||||||
DIRECT,
|
|
||||||
NAT,
|
|
||||||
RELAY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN
|
||||||
|
private static final ConnectionType FORCED_CONNECTION_TYPE = ConnectionType.DIRECT;
|
||||||
|
|
||||||
|
// Typically you run the seed node in localhost to test direct connection.
|
||||||
|
// If you have a setup where you are not behind a router you can also use a WAN side seed node.
|
||||||
|
private static final Node BOOTSTRAP_NODE =
|
||||||
|
(FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) ? BootstrapNode.LOCALHOST : BootstrapNode.DIGITAL_OCEAN1;
|
||||||
|
|
||||||
|
private static final PeerAddress BOOTSTRAP_NODE_ADDRESS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
BOOTSTRAP_NODE_ADDRESS = new PeerAddress(
|
||||||
|
Number160.createHash(BOOTSTRAP_NODE.getId()),
|
||||||
|
BOOTSTRAP_NODE.getIp(), BOOTSTRAP_NODE.getPort(), BOOTSTRAP_NODE.getPort());
|
||||||
|
} catch (UnknownHostException ex) {
|
||||||
|
throw new RuntimeException(BOOTSTRAP_NODE.toString(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If cache is used tests get faster as it doesn't create and bootstrap a new node at every test.
|
||||||
|
// Need to observe if it can have some side effects.
|
||||||
|
private static final boolean CACHE_CLIENTS = true;
|
||||||
|
|
||||||
|
// Use that to stress test with repeated run of the test method body
|
||||||
|
private static final int STRESS_TEST_LOOP_COUNT = 1;
|
||||||
|
|
||||||
// need to be static to keep them during tests
|
// need to be static to keep them during tests
|
||||||
private final static Map<String, Peer> cachedPeers = new HashMap<>();
|
private final static Map<String, Peer> cachedPeers = new HashMap<>();
|
||||||
|
|
||||||
private String seedId;
|
|
||||||
private String seedIP;
|
|
||||||
private int seedPort;
|
|
||||||
private PeerDHT peer1DHT;
|
private PeerDHT peer1DHT;
|
||||||
private PeerDHT peer2DHT;
|
private PeerDHT peer2DHT;
|
||||||
private int client1Port;
|
private int client1Port;
|
||||||
private int client2Port;
|
private int client2Port;
|
||||||
private ConnectionType resolvedConnectionType;
|
private ConnectionType resolvedConnectionType;
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Configure
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// Setup your seed node
|
|
||||||
private final static String SEED_ID_WAN_1 = "digitalocean1.bitsquare.io";
|
|
||||||
private final static String SEED_IP_WAN_1 = "188.226.179.109";
|
|
||||||
private final static int SEED_PORT_WAN_1 = 5000;
|
|
||||||
|
|
||||||
// Setup a second seed node used in some tests
|
|
||||||
private final static String SEED_ID_WAN_2 = "digitalocean2.bitsquare.io";
|
|
||||||
private final static String SEED_IP_WAN_2 = "188.226.179.109";
|
|
||||||
private final static int SEED_PORT_WAN_2 = 5001;
|
|
||||||
|
|
||||||
// If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN
|
|
||||||
private final ConnectionType forcedConnectionType = ConnectionType.DIRECT;
|
|
||||||
|
|
||||||
// In port forwarding mode the isSuccess returns false, but the DHT operations succeeded.
|
|
||||||
// Needs investigation why. Will be removed as far its fixed.
|
|
||||||
private boolean ignoreSuccessTests = false;
|
|
||||||
|
|
||||||
// If cache is used tests get faster as it doesn't create and bootstrap a new node at every test.
|
|
||||||
// Need to observe if it can have some side effects.
|
|
||||||
private boolean cacheClients = true;
|
|
||||||
|
|
||||||
// Use that to stress test with repeated run of the test method body
|
|
||||||
private int stressTestLoopCount = 1;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void configure() {
|
public void configure() {
|
||||||
// Typically you run the seed node in localhost to test direct connection.
|
|
||||||
// If you have a setup where you are not behind a router you can also use a WAN side seed node.
|
|
||||||
if (forcedConnectionType == ConnectionType.DIRECT) {
|
|
||||||
seedId = "localhost";
|
|
||||||
seedIP = "127.0.0.1";
|
|
||||||
seedPort = 5000;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
seedId = SEED_ID_WAN_1;
|
|
||||||
seedIP = SEED_IP_WAN_1;
|
|
||||||
seedPort = SEED_PORT_WAN_1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only in NAT mode we have to deal with that bug.
|
|
||||||
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT)
|
|
||||||
ignoreSuccessTests = true;
|
|
||||||
|
|
||||||
client1Port = getNewRandomPort();
|
client1Port = getNewRandomPort();
|
||||||
client2Port = getNewRandomPort();
|
client2Port = getNewRandomPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getNewRandomPort() {
|
|
||||||
// new Ports().tcpPort() returns a random port
|
|
||||||
int newPort = new Ports().tcpPort();
|
|
||||||
while (newPort == client1Port || newPort == client2Port)
|
|
||||||
newPort = new Ports().tcpPort();
|
|
||||||
|
|
||||||
return newPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Tests
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
if (!cacheClients) {
|
if (!CACHE_CLIENTS) {
|
||||||
if (peer1DHT != null)
|
if (peer1DHT != null)
|
||||||
peer1DHT.shutdown().awaitUninterruptibly();
|
peer1DHT.shutdown().awaitUninterruptibly();
|
||||||
if (peer2DHT != null)
|
if (peer2DHT != null)
|
||||||
|
@ -170,9 +130,9 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bootstrapInUnknownMode() throws Exception {
|
public void bootstrapInUnknownMode() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.UNKNOWN)
|
if (FORCED_CONNECTION_TYPE == ConnectionType.UNKNOWN)
|
||||||
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
|
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -181,9 +141,9 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapDirectConnection() throws Exception {
|
public void testBootstrapDirectConnection() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.DIRECT)
|
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT)
|
||||||
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
|
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -192,9 +152,9 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapWithPortForwarding() throws Exception {
|
public void testBootstrapWithPortForwarding() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.NAT)
|
if (FORCED_CONNECTION_TYPE == ConnectionType.NAT)
|
||||||
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
|
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -203,9 +163,9 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapInRelayMode() throws Exception {
|
public void testBootstrapInRelayMode() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.RELAY)
|
if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY)
|
||||||
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
|
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -214,13 +174,13 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPut() throws Exception {
|
public void testPut() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
|
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
|
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
|
||||||
futurePut.awaitUninterruptibly();
|
futurePut.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut.isSuccess());
|
assertTrue(futurePut.isSuccess());
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -229,19 +189,19 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPutGet() throws Exception {
|
public void testPutGet() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
|
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
|
||||||
futurePut.awaitUninterruptibly();
|
futurePut.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut.isSuccess());
|
assertTrue(futurePut.isSuccess());
|
||||||
|
|
||||||
|
|
||||||
peer2DHT = getDHTPeer("node_2", client2Port);
|
peer2DHT = getDHTPeer("node_2", client2Port);
|
||||||
FutureGet futureGet = peer2DHT.get(Number160.createHash("key")).start();
|
FutureGet futureGet = peer2DHT.get(Number160.createHash("key")).start();
|
||||||
futureGet.awaitUninterruptibly();
|
futureGet.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futureGet.isSuccess());
|
assertTrue(futureGet.isSuccess());
|
||||||
assertEquals("hallo", futureGet.data().object());
|
assertEquals("hallo", futureGet.data().object());
|
||||||
|
|
||||||
|
@ -251,17 +211,17 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdd() throws Exception {
|
public void testAdd() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
||||||
futurePut1.awaitUninterruptibly();
|
futurePut1.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut1.isSuccess());
|
assertTrue(futurePut1.isSuccess());
|
||||||
|
|
||||||
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
||||||
futurePut2.awaitUninterruptibly();
|
futurePut2.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut2.isSuccess());
|
assertTrue(futurePut2.isSuccess());
|
||||||
|
|
||||||
shutdown();
|
shutdown();
|
||||||
|
@ -270,24 +230,24 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddGet() throws Exception {
|
public void testAddGet() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
||||||
futurePut1.awaitUninterruptibly();
|
futurePut1.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut1.isSuccess());
|
assertTrue(futurePut1.isSuccess());
|
||||||
|
|
||||||
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
||||||
futurePut2.awaitUninterruptibly();
|
futurePut2.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut2.isSuccess());
|
assertTrue(futurePut2.isSuccess());
|
||||||
|
|
||||||
|
|
||||||
peer2DHT = getDHTPeer("node_2", client2Port);
|
peer2DHT = getDHTPeer("node_2", client2Port);
|
||||||
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
|
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
|
||||||
futureGet.awaitUninterruptibly();
|
futureGet.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futureGet.isSuccess());
|
assertTrue(futureGet.isSuccess());
|
||||||
|
|
||||||
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
|
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
|
||||||
|
@ -300,17 +260,17 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddRemove() throws Exception {
|
public void testAddRemove() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
|
||||||
futurePut1.awaitUninterruptibly();
|
futurePut1.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut1.isSuccess());
|
assertTrue(futurePut1.isSuccess());
|
||||||
|
|
||||||
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
|
||||||
futurePut2.awaitUninterruptibly();
|
futurePut2.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futurePut2.isSuccess());
|
assertTrue(futurePut2.isSuccess());
|
||||||
|
|
||||||
|
|
||||||
|
@ -321,12 +281,12 @@ public class TomP2PTests {
|
||||||
futureRemove.awaitUninterruptibly();
|
futureRemove.awaitUninterruptibly();
|
||||||
|
|
||||||
// That fails sometimes in direct mode and NAT
|
// That fails sometimes in direct mode and NAT
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futureRemove.isSuccess());
|
assertTrue(futureRemove.isSuccess());
|
||||||
|
|
||||||
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
|
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
|
||||||
futureGet.awaitUninterruptibly();
|
futureGet.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (shouldEvaluateSuccess())
|
||||||
assertTrue(futureGet.isSuccess());
|
assertTrue(futureGet.isSuccess());
|
||||||
|
|
||||||
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
||||||
|
@ -342,9 +302,9 @@ public class TomP2PTests {
|
||||||
// That will probably be fixed in a future version of TomP2P
|
// That will probably be fixed in a future version of TomP2P
|
||||||
@Test
|
@Test
|
||||||
public void testSendDirectBetweenLocalPeers() throws Exception {
|
public void testSendDirectBetweenLocalPeers() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
if (forcedConnectionType != ConnectionType.NAT && resolvedConnectionType != ConnectionType.NAT) {
|
if (FORCED_CONNECTION_TYPE != ConnectionType.NAT && resolvedConnectionType != ConnectionType.NAT) {
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
peer2DHT = getDHTPeer("node_2", client2Port);
|
peer2DHT = getDHTPeer("node_2", client2Port);
|
||||||
|
|
||||||
|
@ -380,14 +340,12 @@ public class TomP2PTests {
|
||||||
// A node can send a message to another peer which is not in the same LAN.
|
// A node can send a message to another peer which is not in the same LAN.
|
||||||
@Test
|
@Test
|
||||||
public void testSendDirectToSeedNode() throws Exception {
|
public void testSendDirectToSeedNode() throws Exception {
|
||||||
for (int i = 0; i < stressTestLoopCount; i++) {
|
for (int i = 0; i < STRESS_TEST_LOOP_COUNT; i++) {
|
||||||
configure();
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
PeerAddress reachablePeerAddress = new PeerAddress(Number160.createHash(seedId), seedIP, seedPort,
|
|
||||||
seedPort);
|
|
||||||
|
|
||||||
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection
|
FuturePeerConnection futurePeerConnection =
|
||||||
(reachablePeerAddress, 500);
|
peer1DHT.peer().createPeerConnection(BOOTSTRAP_NODE_ADDRESS, 500);
|
||||||
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
|
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
|
||||||
futureDirect.awaitUninterruptibly();
|
futureDirect.awaitUninterruptibly();
|
||||||
assertTrue(futureDirect.isSuccess());
|
assertTrue(futureDirect.isSuccess());
|
||||||
|
@ -398,26 +356,15 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Bootstrapping
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private Peer bootstrapDirectConnection(String clientId, int clientPort) {
|
private Peer bootstrapDirectConnection(String clientId, int clientPort) {
|
||||||
return bootstrapDirectConnection(clientId, clientPort, seedId, seedIP, seedPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Peer bootstrapDirectConnection(String clientId, int clientPort, String seedNodeId,
|
|
||||||
String seedNodeIP, int seedNodePort) {
|
|
||||||
final String id = clientId + clientPort;
|
final String id = clientId + clientPort;
|
||||||
if (cacheClients && cachedPeers.containsKey(id)) {
|
if (CACHE_CLIENTS && cachedPeers.containsKey(id)) {
|
||||||
return cachedPeers.get(id);
|
return cachedPeers.get(id);
|
||||||
}
|
}
|
||||||
Peer peer = null;
|
Peer peer = null;
|
||||||
try {
|
try {
|
||||||
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).start();
|
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).start();
|
||||||
PeerAddress masterNodeAddress = new PeerAddress(Number160.createHash(seedNodeId), seedNodeIP, seedNodePort,
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
seedNodePort);
|
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
|
|
||||||
futureDiscover.awaitUninterruptibly();
|
futureDiscover.awaitUninterruptibly();
|
||||||
if (futureDiscover.isSuccess()) {
|
if (futureDiscover.isSuccess()) {
|
||||||
log.info("Discover with direct connection successful. Address = " + futureDiscover.peerAddress());
|
log.info("Discover with direct connection successful. Address = " + futureDiscover.peerAddress());
|
||||||
|
@ -440,28 +387,21 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapWithPortForwarding(String clientId, int clientPort) {
|
private Peer bootstrapWithPortForwarding(String clientId, int clientPort) {
|
||||||
return bootstrapWithPortForwarding(clientId, clientPort, seedId, seedIP, seedPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Peer bootstrapWithPortForwarding(String clientId, int clientPort, String seedNodeId,
|
|
||||||
String seedNodeIP, int seedNodePort) {
|
|
||||||
final String id = clientId + clientPort;
|
final String id = clientId + clientPort;
|
||||||
if (cacheClients && cachedPeers.containsKey(id)) {
|
if (CACHE_CLIENTS && cachedPeers.containsKey(id)) {
|
||||||
return cachedPeers.get(id);
|
return cachedPeers.get(id);
|
||||||
}
|
}
|
||||||
Peer peer = null;
|
Peer peer = null;
|
||||||
try {
|
try {
|
||||||
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
|
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
|
||||||
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
||||||
PeerAddress masterNodeAddress = new PeerAddress(Number160.createHash(seedNodeId), seedNodeIP, seedNodePort,
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
seedNodePort);
|
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
|
|
||||||
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
|
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
|
||||||
futureNAT.awaitUninterruptibly();
|
futureNAT.awaitUninterruptibly();
|
||||||
if (futureNAT.isSuccess()) {
|
if (futureNAT.isSuccess()) {
|
||||||
log.info("Automatic port forwarding is setup. Now we do a futureDiscover again. Address = " +
|
log.info("Automatic port forwarding is setup. Now we do a futureDiscover again. Address = " +
|
||||||
futureNAT.peerAddress());
|
futureNAT.peerAddress());
|
||||||
futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
|
futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
futureDiscover.awaitUninterruptibly();
|
futureDiscover.awaitUninterruptibly();
|
||||||
if (futureDiscover.isSuccess()) {
|
if (futureDiscover.isSuccess()) {
|
||||||
log.info("Discover with automatic port forwarding was successful. Address = " + futureDiscover
|
log.info("Discover with automatic port forwarding was successful. Address = " + futureDiscover
|
||||||
|
@ -493,13 +433,8 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapInRelayMode(String clientId, int clientPort) {
|
private Peer bootstrapInRelayMode(String clientId, int clientPort) {
|
||||||
return bootstrapInRelayMode(clientId, clientPort, seedId, seedIP, seedPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Peer bootstrapInRelayMode(String clientId, int clientPort, String seedNodeId,
|
|
||||||
String seedNodeIP, int seedNodePort) {
|
|
||||||
final String id = clientId + clientPort;
|
final String id = clientId + clientPort;
|
||||||
if (cacheClients && cachedPeers.containsKey(id)) {
|
if (CACHE_CLIENTS && cachedPeers.containsKey(id)) {
|
||||||
return cachedPeers.get(id);
|
return cachedPeers.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -507,9 +442,7 @@ public class TomP2PTests {
|
||||||
try {
|
try {
|
||||||
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
|
peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
|
||||||
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
||||||
PeerAddress masterNodeAddress = new PeerAddress(Number160.createHash(seedNodeId), seedNodeIP, seedNodePort,
|
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
|
||||||
seedNodePort);
|
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
|
|
||||||
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
|
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
|
||||||
FutureRelayNAT futureRelayNAT = peerNAT.startRelay(futureDiscover, futureNAT);
|
FutureRelayNAT futureRelayNAT = peerNAT.startRelay(futureDiscover, futureNAT);
|
||||||
futureRelayNAT.awaitUninterruptibly();
|
futureRelayNAT.awaitUninterruptibly();
|
||||||
|
@ -536,63 +469,61 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Peer bootstrapInUnknownMode(String clientId, int clientPort) {
|
private Peer bootstrapInUnknownMode(String clientId, int clientPort) {
|
||||||
return bootstrapInUnknownMode(clientId, clientPort, seedId, seedIP, seedPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Peer bootstrapInUnknownMode(String clientId, int clientPort, String seedNodeId,
|
|
||||||
String seedNodeIP, int seedNodePort) {
|
|
||||||
resolvedConnectionType = ConnectionType.DIRECT;
|
resolvedConnectionType = ConnectionType.DIRECT;
|
||||||
Peer peer = bootstrapDirectConnection(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
Peer peer = bootstrapDirectConnection(clientId, clientPort);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
|
|
||||||
resolvedConnectionType = ConnectionType.NAT;
|
resolvedConnectionType = ConnectionType.NAT;
|
||||||
peer = bootstrapWithPortForwarding(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapWithPortForwarding(clientId, clientPort);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
|
|
||||||
resolvedConnectionType = ConnectionType.RELAY;
|
resolvedConnectionType = ConnectionType.RELAY;
|
||||||
peer = bootstrapInRelayMode(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapInRelayMode(clientId, clientPort);
|
||||||
if (peer != null)
|
if (peer != null)
|
||||||
return peer;
|
return peer;
|
||||||
else
|
else
|
||||||
log.error("Bootstrapping in all modes failed. Check if the seed node is running. " +
|
log.error("Bootstrapping in all modes failed. Is bootstrap node " + BOOTSTRAP_NODE + "running?");
|
||||||
"seedNodeId= " + seedNodeId +
|
|
||||||
"seedNodeIP= " + seedNodeIP +
|
|
||||||
"seedNodePort= " + seedNodePort);
|
|
||||||
|
|
||||||
resolvedConnectionType = null;
|
resolvedConnectionType = null;
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PeerDHT getDHTPeer(String clientId, int clientPort) {
|
private PeerDHT getDHTPeer(String clientId, int clientPort) {
|
||||||
return getDHTPeer(clientId, clientPort, seedId, seedIP, seedPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private PeerDHT getDHTPeer(String clientId, int clientPort, String seedNodeId,
|
|
||||||
String seedNodeIP, int seedNodePort) {
|
|
||||||
Peer peer;
|
Peer peer;
|
||||||
if (forcedConnectionType == ConnectionType.DIRECT) {
|
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
|
||||||
peer = bootstrapDirectConnection(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapDirectConnection(clientId, clientPort);
|
||||||
}
|
}
|
||||||
else if (forcedConnectionType == ConnectionType.NAT) {
|
else if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) {
|
||||||
peer = bootstrapWithPortForwarding(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapWithPortForwarding(clientId, clientPort);
|
||||||
}
|
}
|
||||||
else if (forcedConnectionType == ConnectionType.RELAY) {
|
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
|
||||||
peer = bootstrapInRelayMode(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapInRelayMode(clientId, clientPort);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
peer = bootstrapInUnknownMode(clientId, clientPort, seedNodeId, seedNodeIP, seedNodePort);
|
peer = bootstrapInUnknownMode(clientId, clientPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (peer == null)
|
if (peer == null)
|
||||||
Assert.fail("Bootstrapping failed. Check if the seed node is running. " +
|
Assert.fail("Bootstrapping failed." +
|
||||||
"forcedConnectionType= " + forcedConnectionType +
|
" forcedConnectionType= " + FORCED_CONNECTION_TYPE +
|
||||||
" resolvedConnectionType= " + resolvedConnectionType +
|
" resolvedConnectionType= " + resolvedConnectionType + "." +
|
||||||
" seedNodeId= " + seedNodeId +
|
" Is bootstrap node " + BOOTSTRAP_NODE + "running?");
|
||||||
" seedNodeIP= " + seedNodeIP +
|
|
||||||
" seedNodePort= " + seedNodePort);
|
|
||||||
|
|
||||||
return new PeerBuilderDHT(peer).start();
|
return new PeerBuilderDHT(peer).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getNewRandomPort() {
|
||||||
|
// new Ports().tcpPort() returns a random port
|
||||||
|
int newPort = new Ports().tcpPort();
|
||||||
|
while (newPort == client1Port || newPort == client2Port)
|
||||||
|
newPort = new Ports().tcpPort();
|
||||||
|
|
||||||
|
return newPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldEvaluateSuccess() {
|
||||||
|
return FORCED_CONNECTION_TYPE == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue