mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-07 14:42:51 -04:00
Add loops, add extra SeedNode class, remove LanTest class
This commit is contained in:
parent
bcaa8b9946
commit
4fb8030a43
3 changed files with 278 additions and 376 deletions
|
@ -1,202 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of Bitsquare.
|
|
||||||
*
|
|
||||||
* Bitsquare 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.
|
|
||||||
*
|
|
||||||
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package io.bitsquare.msg;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import net.tomp2p.dht.FutureGet;
|
|
||||||
import net.tomp2p.dht.FuturePut;
|
|
||||||
import net.tomp2p.dht.PeerBuilderDHT;
|
|
||||||
import net.tomp2p.dht.PeerDHT;
|
|
||||||
import net.tomp2p.dht.StorageLayer;
|
|
||||||
import net.tomp2p.dht.StorageMemory;
|
|
||||||
import net.tomp2p.futures.FutureDirect;
|
|
||||||
import net.tomp2p.futures.FutureDiscover;
|
|
||||||
import net.tomp2p.nat.FutureNAT;
|
|
||||||
import net.tomp2p.nat.FutureRelayNAT;
|
|
||||||
import net.tomp2p.nat.PeerBuilderNAT;
|
|
||||||
import net.tomp2p.nat.PeerNAT;
|
|
||||||
import net.tomp2p.p2p.Peer;
|
|
||||||
import net.tomp2p.p2p.PeerBuilder;
|
|
||||||
import net.tomp2p.peers.Number160;
|
|
||||||
import net.tomp2p.peers.PeerAddress;
|
|
||||||
import net.tomp2p.storage.Data;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class BasicUsecasesInLANTest {
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(BasicUsecasesInLANTest.class);
|
|
||||||
|
|
||||||
private final static String SERVER_ID = "localhost";
|
|
||||||
private final static String SERVER_IP = "127.0.0.1";
|
|
||||||
private final static int SERVER_PORT = 5000;
|
|
||||||
|
|
||||||
private final static String CLIENT_1_ID = "alice";
|
|
||||||
private final static String CLIENT_2_ID = "bob";
|
|
||||||
private final static int CLIENT_1_PORT = 6510;
|
|
||||||
private final static int CLIENT_2_PORT = 6511;
|
|
||||||
|
|
||||||
private Thread serverThread;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void startServer() throws Exception {
|
|
||||||
serverThread = new Thread(() -> {
|
|
||||||
Peer peer = null;
|
|
||||||
try {
|
|
||||||
peer = new PeerBuilder(Number160.createHash(SERVER_ID)).ports(SERVER_PORT).start();
|
|
||||||
log.debug("peer started.");
|
|
||||||
while (true) {
|
|
||||||
for (PeerAddress pa : peer.peerBean().peerMap().all()) {
|
|
||||||
log.debug("peer online (TCP):" + pa);
|
|
||||||
}
|
|
||||||
Thread.sleep(2000);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
if (peer != null)
|
|
||||||
peer.shutdown().awaitUninterruptibly();
|
|
||||||
} catch (IOException e2) {
|
|
||||||
e2.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
serverThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void stopServer() throws Exception {
|
|
||||||
serverThread.interrupt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore
|
|
||||||
public void testBootstrap() throws Exception {
|
|
||||||
PeerDHT peerDHT = startClient(CLIENT_1_ID, CLIENT_1_PORT);
|
|
||||||
assertEquals(CLIENT_1_PORT, peerDHT.peerAddress().tcpPort());
|
|
||||||
assertEquals(CLIENT_1_PORT, peerDHT.peerAddress().udpPort());
|
|
||||||
peerDHT.shutdown().awaitUninterruptibly();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore
|
|
||||||
public void testDHT() throws Exception {
|
|
||||||
PeerDHT peer1DHT = startClient(CLIENT_1_ID, CLIENT_1_PORT);
|
|
||||||
PeerDHT peer2DHT = startClient(CLIENT_2_ID, CLIENT_2_PORT);
|
|
||||||
|
|
||||||
FuturePut futurePut1 = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo1")).start();
|
|
||||||
futurePut1.awaitUninterruptibly();
|
|
||||||
// why fails that?
|
|
||||||
//assertTrue(futurePut1.isSuccess());
|
|
||||||
|
|
||||||
FutureGet futureGet2 = peer1DHT.get(Number160.createHash("key")).start();
|
|
||||||
futureGet2.awaitUninterruptibly();
|
|
||||||
assertTrue(futureGet2.isSuccess());
|
|
||||||
assertNotNull(futureGet2.data());
|
|
||||||
assertEquals("hallo1", futureGet2.data().object());
|
|
||||||
|
|
||||||
peer1DHT.shutdown().awaitUninterruptibly();
|
|
||||||
peer2DHT.shutdown().awaitUninterruptibly();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore
|
|
||||||
public void testSendDirect() throws Exception {
|
|
||||||
PeerDHT peer1DHT = startClient(CLIENT_1_ID, CLIENT_1_PORT);
|
|
||||||
PeerDHT peer2DHT = startClient(CLIENT_2_ID, CLIENT_2_PORT);
|
|
||||||
|
|
||||||
final CountDownLatch countDownLatch = new CountDownLatch(1);
|
|
||||||
final StringBuilder result = new StringBuilder();
|
|
||||||
peer2DHT.peer().objectDataReply((sender, request) -> {
|
|
||||||
countDownLatch.countDown();
|
|
||||||
result.append(String.valueOf(request));
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
FutureDirect futureDirect = peer1DHT.peer().sendDirect(peer2DHT.peerAddress()).object("hallo").start();
|
|
||||||
futureDirect.awaitUninterruptibly();
|
|
||||||
countDownLatch.await(1, TimeUnit.SECONDS);
|
|
||||||
if (countDownLatch.getCount() > 0)
|
|
||||||
Assert.fail("The test method did not complete successfully!");
|
|
||||||
|
|
||||||
peer1DHT.shutdown().awaitUninterruptibly();
|
|
||||||
peer2DHT.shutdown().awaitUninterruptibly();
|
|
||||||
|
|
||||||
assertEquals("hallo", result.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private PeerDHT startClient(String clientId, int clientPort) throws Exception {
|
|
||||||
try {
|
|
||||||
Peer peer = new PeerBuilder(Number160.createHash(clientId)).ports(clientPort).behindFirewall().start();
|
|
||||||
PeerDHT peerDHT = new PeerBuilderDHT(peer).storageLayer(new StorageLayer(new StorageMemory())).start();
|
|
||||||
|
|
||||||
PeerAddress masterNodeAddress = new PeerAddress(Number160.createHash(SERVER_ID), SERVER_IP, SERVER_PORT,
|
|
||||||
SERVER_PORT);
|
|
||||||
FutureDiscover futureDiscover = peer.discover().peerAddress(masterNodeAddress).start();
|
|
||||||
futureDiscover.awaitUninterruptibly();
|
|
||||||
if (futureDiscover.isSuccess()) {
|
|
||||||
log.info("Discover with direct connection successful. Address = " + futureDiscover.peerAddress());
|
|
||||||
return peerDHT;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
|
|
||||||
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
|
|
||||||
futureNAT.awaitUninterruptibly();
|
|
||||||
if (futureNAT.isSuccess()) {
|
|
||||||
log.info("Automatic port forwarding is setup. Address = " +
|
|
||||||
futureNAT.peerAddress());
|
|
||||||
return peerDHT;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
FutureRelayNAT futureRelayNAT = peerNAT.startRelay(futureDiscover, futureNAT);
|
|
||||||
futureRelayNAT.awaitUninterruptibly();
|
|
||||||
if (futureRelayNAT.isSuccess()) {
|
|
||||||
log.info("Bootstrap using relay successful. Address = " +
|
|
||||||
peer.peerAddress());
|
|
||||||
futureRelayNAT.shutdown();
|
|
||||||
peer.shutdown().awaitUninterruptibly();
|
|
||||||
return peerDHT;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
log.error("Bootstrap using relay failed " + futureRelayNAT.failedReason());
|
|
||||||
Assert.fail("Bootstrap using relay failed " + futureRelayNAT.failedReason());
|
|
||||||
futureRelayNAT.shutdown();
|
|
||||||
peer.shutdown().awaitUninterruptibly();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Bootstrap in relay mode failed " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail("Bootstrap in relay mode failed " + e.getMessage());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
72
src/test/java/io/bitsquare/msg/SeedNodeForTesting.java
Normal file
72
src/test/java/io/bitsquare/msg/SeedNodeForTesting.java
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare 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.
|
||||||
|
*
|
||||||
|
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.bitsquare.msg;
|
||||||
|
|
||||||
|
import net.tomp2p.dht.PeerBuilderDHT;
|
||||||
|
import net.tomp2p.dht.PeerDHT;
|
||||||
|
import net.tomp2p.nat.PeerBuilderNAT;
|
||||||
|
import net.tomp2p.p2p.Peer;
|
||||||
|
import net.tomp2p.p2p.PeerBuilder;
|
||||||
|
import net.tomp2p.peers.Number160;
|
||||||
|
import net.tomp2p.peers.PeerAddress;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for testing with {@link TomP2PTests}
|
||||||
|
*/
|
||||||
|
public class SeedNodeForTesting {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(SeedNodeForTesting.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Define your seed node IP and port
|
||||||
|
// "127.0.0.1" for localhost or SEED_ID_WAN_1
|
||||||
|
new SeedNodeForTesting().startSeedNode("localhost", 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Thread startSeedNode(String seedNodeId, int seedNodePort) {
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
Peer peer = null;
|
||||||
|
try {
|
||||||
|
peer = new PeerBuilder(Number160.createHash(seedNodeId)).ports(seedNodePort).start();
|
||||||
|
PeerDHT peerDHT = new PeerBuilderDHT(peer).start();
|
||||||
|
peerDHT.peer().objectDataReply((sender, request) -> {
|
||||||
|
log.trace("received request: ", request.toString());
|
||||||
|
return "pong";
|
||||||
|
});
|
||||||
|
|
||||||
|
new PeerBuilderNAT(peer).start();
|
||||||
|
|
||||||
|
log.debug("SeedNode started.");
|
||||||
|
for (; ; ) {
|
||||||
|
for (PeerAddress pa : peer.peerBean().peerMap().all()) {
|
||||||
|
log.debug("peer online:" + pa);
|
||||||
|
}
|
||||||
|
Thread.sleep(2000);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (peer != null)
|
||||||
|
peer.shutdown().awaitUninterruptibly();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -58,16 +58,41 @@ import static org.junit.Assert.*;
|
||||||
* Test bootstrapping, DHT operations like put/get/add/remove and sendDirect in both LAN and WAN environment
|
* Test bootstrapping, DHT operations like put/get/add/remove and sendDirect in both LAN and WAN environment
|
||||||
* Test scenarios in direct connection, auto port forwarding or relay mode.
|
* Test scenarios in direct connection, auto port forwarding or relay mode.
|
||||||
* <p>
|
* <p>
|
||||||
* The seed node code is in startSeedNode.
|
* The start a seed node code use the {@link SeedNodeForTesting} class.
|
||||||
* <p>
|
* <p>
|
||||||
* 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 further.
|
* 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 {
|
||||||
|
UNKNOWN,
|
||||||
|
DIRECT,
|
||||||
|
NAT,
|
||||||
|
RELAY
|
||||||
|
}
|
||||||
|
|
||||||
|
// need to be static to keep them during tests
|
||||||
|
private final static Map<String, Peer> cachedPeers = new HashMap<>();
|
||||||
|
|
||||||
|
private String seedId;
|
||||||
|
private String seedIP;
|
||||||
|
private int seedPort;
|
||||||
|
private PeerDHT peer1DHT;
|
||||||
|
private PeerDHT peer2DHT;
|
||||||
|
private int client1Port;
|
||||||
|
private int client2Port;
|
||||||
|
private ConnectionType resolvedConnectionType;
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Configure
|
// Configure
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -83,8 +108,18 @@ public class TomP2PTests {
|
||||||
private final static int SEED_PORT_WAN_2 = 5001;
|
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
|
// If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN
|
||||||
private final ConnectionType forcedConnectionType = ConnectionType.NAT;
|
private final ConnectionType forcedConnectionType = ConnectionType.DIRECT;
|
||||||
private ConnectionType resolvedConnectionType;
|
|
||||||
|
// 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() {
|
||||||
|
@ -105,77 +140,17 @@ public class TomP2PTests {
|
||||||
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT)
|
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT)
|
||||||
ignoreSuccessTests = true;
|
ignoreSuccessTests = true;
|
||||||
|
|
||||||
|
client1Port = getNewRandomPort();
|
||||||
|
client2Port = getNewRandomPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNewRandomPort() {
|
||||||
// new Ports().tcpPort() returns a random port
|
// new Ports().tcpPort() returns a random port
|
||||||
client1Port = new Ports().tcpPort();
|
int newPort = new Ports().tcpPort();
|
||||||
client2Port = new Ports().tcpPort();
|
while (newPort == client1Port || newPort == client2Port)
|
||||||
}
|
newPort = new Ports().tcpPort();
|
||||||
|
|
||||||
// In port forwarding mode the isSuccess returns false, but the DHT operations succeeded.
|
return newPort;
|
||||||
// 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;
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Private fields
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
private enum ConnectionType {
|
|
||||||
UNKNOWN,
|
|
||||||
DIRECT,
|
|
||||||
NAT,
|
|
||||||
RELAY
|
|
||||||
}
|
|
||||||
|
|
||||||
private final static Map<String, Peer> cachedPeers = new HashMap<>();
|
|
||||||
private String seedId;
|
|
||||||
private String seedIP;
|
|
||||||
private int seedPort;
|
|
||||||
private PeerDHT peer1DHT;
|
|
||||||
private PeerDHT peer2DHT;
|
|
||||||
private int client1Port;
|
|
||||||
private int client2Port;
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Seed node
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// Define your seed node IP and port
|
|
||||||
// "127.0.0.1" for localhost or SEED_ID_WAN_1
|
|
||||||
new TomP2PTests().startSeedNode("127.0.0.1", 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Thread startSeedNode(String seedNodeId, int seedNodePort) {
|
|
||||||
Thread thread = new Thread(() -> {
|
|
||||||
Peer peer = null;
|
|
||||||
try {
|
|
||||||
peer = new PeerBuilder(Number160.createHash(seedNodeId)).ports(seedNodePort).start();
|
|
||||||
PeerDHT peerDHT = new PeerBuilderDHT(peer).start();
|
|
||||||
peerDHT.peer().objectDataReply((sender, request) -> {
|
|
||||||
log.trace("received request: ", request.toString());
|
|
||||||
return "pong";
|
|
||||||
});
|
|
||||||
|
|
||||||
new PeerBuilderNAT(peer).start();
|
|
||||||
|
|
||||||
log.debug("peer started.");
|
|
||||||
for (; ; ) {
|
|
||||||
for (PeerAddress pa : peer.peerBean().peerMap().all()) {
|
|
||||||
log.debug("peer online (TCP):" + pa);
|
|
||||||
}
|
|
||||||
Thread.sleep(2000);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (peer != null)
|
|
||||||
peer.shutdown().awaitUninterruptibly();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
thread.start();
|
|
||||||
return thread;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -195,39 +170,67 @@ public class TomP2PTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bootstrapInUnknownMode() throws Exception {
|
public void bootstrapInUnknownMode() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.UNKNOWN)
|
if (forcedConnectionType == ConnectionType.UNKNOWN)
|
||||||
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
|
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapDirectConnection() throws Exception {
|
public void testBootstrapDirectConnection() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.DIRECT)
|
if (forcedConnectionType == ConnectionType.DIRECT)
|
||||||
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
|
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapWithPortForwarding() throws Exception {
|
public void testBootstrapWithPortForwarding() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.NAT)
|
if (forcedConnectionType == ConnectionType.NAT)
|
||||||
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
|
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBootstrapInRelayMode() throws Exception {
|
public void testBootstrapInRelayMode() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
if (forcedConnectionType == ConnectionType.RELAY)
|
if (forcedConnectionType == ConnectionType.RELAY)
|
||||||
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
|
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPut() throws Exception {
|
public void testPut() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
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 (!ignoreSuccessTests)
|
||||||
assertTrue(futurePut.isSuccess());
|
assertTrue(futurePut.isSuccess());
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPutGet() throws Exception {
|
public void testPutGet() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
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();
|
||||||
|
@ -241,10 +244,15 @@ public class TomP2PTests {
|
||||||
if (!ignoreSuccessTests)
|
if (!ignoreSuccessTests)
|
||||||
assertTrue(futureGet.isSuccess());
|
assertTrue(futureGet.isSuccess());
|
||||||
assertEquals("hallo", futureGet.data().object());
|
assertEquals("hallo", futureGet.data().object());
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdd() throws Exception {
|
public void testAdd() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
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();
|
||||||
|
@ -255,10 +263,15 @@ public class TomP2PTests {
|
||||||
futurePut2.awaitUninterruptibly();
|
futurePut2.awaitUninterruptibly();
|
||||||
if (!ignoreSuccessTests)
|
if (!ignoreSuccessTests)
|
||||||
assertTrue(futurePut2.isSuccess());
|
assertTrue(futurePut2.isSuccess());
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddGet() throws Exception {
|
public void testAddGet() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
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();
|
||||||
|
@ -280,10 +293,15 @@ public class TomP2PTests {
|
||||||
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
|
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
|
||||||
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
||||||
assertTrue(futureGet.dataMap().values().size() == 2);
|
assertTrue(futureGet.dataMap().values().size() == 2);
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddRemove() throws Exception {
|
public void testAddRemove() throws Exception {
|
||||||
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
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();
|
||||||
|
@ -298,12 +316,13 @@ public class TomP2PTests {
|
||||||
|
|
||||||
peer2DHT = getDHTPeer("node_2", client2Port);
|
peer2DHT = getDHTPeer("node_2", client2Port);
|
||||||
Number160 contentKey = new Data("hallo1").hash();
|
Number160 contentKey = new Data("hallo1").hash();
|
||||||
FutureRemove futureRemove = peer2DHT.remove(Number160.createHash("locationKey")).contentKey(contentKey).start();
|
FutureRemove futureRemove = peer2DHT.remove(Number160.createHash("locationKey")).contentKey(contentKey)
|
||||||
|
.start();
|
||||||
futureRemove.awaitUninterruptibly();
|
futureRemove.awaitUninterruptibly();
|
||||||
|
|
||||||
// TODO: That fails always also with localhost seed node
|
// That fails sometimes in direct mode and NAT
|
||||||
/*if (!ignoreSuccessTests)
|
if (!ignoreSuccessTests)
|
||||||
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();
|
||||||
|
@ -312,16 +331,20 @@ public class TomP2PTests {
|
||||||
|
|
||||||
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
|
||||||
assertTrue(futureGet.dataMap().values().size() == 1);
|
assertTrue(futureGet.dataMap().values().size() == 1);
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The sendDirect operation fails in port forwarding mode because most routers does not support NAT reflections.
|
// The sendDirect operation fails in port forwarding mode because most routers does not support NAT reflections.
|
||||||
// So if both clients are behind NAT they cannot send direct message to each other.
|
// So if both clients are behind NAT they cannot send direct message to each other.
|
||||||
// That will probably be fixed in a future version of TomP2P
|
// That will probably be fixed in a future version of TomP2P
|
||||||
// In relay mode the test succeeds
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendDirectRelay() throws Exception {
|
public void testSendDirectBetweenLocalPeers() throws Exception {
|
||||||
if (forcedConnectionType == ConnectionType.RELAY || resolvedConnectionType == ConnectionType.RELAY) {
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
|
if (forcedConnectionType != 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);
|
||||||
|
|
||||||
|
@ -334,8 +357,7 @@ public class TomP2PTests {
|
||||||
return "pong";
|
return "pong";
|
||||||
});
|
});
|
||||||
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection(peer2DHT.peer()
|
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection(peer2DHT.peer()
|
||||||
.peerAddress(),
|
.peerAddress(), 500);
|
||||||
500);
|
|
||||||
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
|
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
|
||||||
futureDirect.awaitUninterruptibly();
|
futureDirect.awaitUninterruptibly();
|
||||||
|
|
||||||
|
@ -346,24 +368,32 @@ public class TomP2PTests {
|
||||||
|
|
||||||
assertEquals("hallo", result.toString());
|
assertEquals("hallo", result.toString());
|
||||||
assertTrue(futureDirect.isSuccess());
|
assertTrue(futureDirect.isSuccess());
|
||||||
|
log.debug(futureDirect.object().toString());
|
||||||
assertEquals("pong", futureDirect.object());
|
assertEquals("pong", futureDirect.object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// That test should succeed in port forwarding as we use the server seed node as receiver.
|
// That test should always succeed as we use the server seed node as receiver.
|
||||||
// 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 testSendDirectPortForwarding() throws Exception {
|
public void testSendDirectToSeedNode() throws Exception {
|
||||||
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT) {
|
for (int i = 0; i < stressTestLoopCount; i++) {
|
||||||
|
configure();
|
||||||
peer1DHT = getDHTPeer("node_1", client1Port);
|
peer1DHT = getDHTPeer("node_1", client1Port);
|
||||||
PeerAddress reachablePeerAddress = new PeerAddress(Number160.createHash(seedId), seedIP, seedPort,
|
PeerAddress reachablePeerAddress = new PeerAddress(Number160.createHash(seedId), seedIP, seedPort,
|
||||||
seedPort);
|
seedPort);
|
||||||
|
|
||||||
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection(reachablePeerAddress, 500);
|
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection
|
||||||
|
(reachablePeerAddress, 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());
|
||||||
assertEquals("pong", futureDirect.object());
|
assertEquals("pong", futureDirect.object());
|
||||||
|
|
||||||
|
shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -556,8 +586,10 @@ public class TomP2PTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (peer == null)
|
if (peer == null)
|
||||||
Assert.fail("Bootstrapping in all modes failed. Check if the seed node is running. " +
|
Assert.fail("Bootstrapping failed. Check if the seed node is running. " +
|
||||||
"seedNodeId= " + seedNodeId +
|
"forcedConnectionType= " + forcedConnectionType +
|
||||||
|
" resolvedConnectionType= " + resolvedConnectionType +
|
||||||
|
" seedNodeId= " + seedNodeId +
|
||||||
" seedNodeIP= " + seedNodeIP +
|
" seedNodeIP= " + seedNodeIP +
|
||||||
" seedNodePort= " + seedNodePort);
|
" seedNodePort= " + seedNodePort);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue