Add loops, add extra SeedNode class, remove LanTest class

This commit is contained in:
Manfred Karrer 2014-11-05 20:41:34 +01:00
parent bcaa8b9946
commit 4fb8030a43
3 changed files with 278 additions and 376 deletions

View File

@ -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;
}
}
}

View 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;
}
}

View File

@ -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 scenarios in direct connection, auto port forwarding or relay mode.
* <p>
* The seed node code is in startSeedNode.
* The start a seed node code use the {@link SeedNodeForTesting} class.
* <p>
* 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
public class TomP2PTests {
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
///////////////////////////////////////////////////////////////////////////////////////////
@ -83,8 +108,18 @@ public class TomP2PTests {
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.NAT;
private ConnectionType resolvedConnectionType;
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
public void configure() {
@ -105,77 +140,17 @@ public class TomP2PTests {
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT)
ignoreSuccessTests = true;
client1Port = getNewRandomPort();
client2Port = getNewRandomPort();
}
private int getNewRandomPort() {
// new Ports().tcpPort() returns a random port
client1Port = new Ports().tcpPort();
client2Port = new Ports().tcpPort();
}
int newPort = 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.
// 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;
return newPort;
}
@ -195,175 +170,230 @@ public class TomP2PTests {
@Test
public void bootstrapInUnknownMode() throws Exception {
if (forcedConnectionType == ConnectionType.UNKNOWN)
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
if (forcedConnectionType == ConnectionType.UNKNOWN)
assertNotNull(bootstrapInUnknownMode("node_1", client1Port));
shutdown();
}
}
@Test
public void testBootstrapDirectConnection() throws Exception {
if (forcedConnectionType == ConnectionType.DIRECT)
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
if (forcedConnectionType == ConnectionType.DIRECT)
assertNotNull(bootstrapDirectConnection("node_1", client1Port));
shutdown();
}
}
@Test
public void testBootstrapWithPortForwarding() throws Exception {
if (forcedConnectionType == ConnectionType.NAT)
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
if (forcedConnectionType == ConnectionType.NAT)
assertNotNull(bootstrapWithPortForwarding("node_1", client1Port));
shutdown();
}
}
@Test
public void testBootstrapInRelayMode() throws Exception {
if (forcedConnectionType == ConnectionType.RELAY)
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
if (forcedConnectionType == ConnectionType.RELAY)
assertNotNull(bootstrapInRelayMode("node_1", client1Port));
shutdown();
}
}
@Test
public void testPut() throws Exception {
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
futurePut.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut.isSuccess());
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
futurePut.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut.isSuccess());
shutdown();
}
}
@Test
public void testPutGet() throws Exception {
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
futurePut.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut.isSuccess());
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut = peer1DHT.put(Number160.createHash("key")).data(new Data("hallo")).start();
futurePut.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut.isSuccess());
peer2DHT = getDHTPeer("node_2", client2Port);
FutureGet futureGet = peer2DHT.get(Number160.createHash("key")).start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
assertEquals("hallo", futureGet.data().object());
peer2DHT = getDHTPeer("node_2", client2Port);
FutureGet futureGet = peer2DHT.get(Number160.createHash("key")).start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
assertEquals("hallo", futureGet.data().object());
shutdown();
}
}
@Test
public void testAdd() throws Exception {
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
shutdown();
}
}
@Test
public void testAddGet() throws Exception {
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
peer2DHT = getDHTPeer("node_2", client2Port);
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
peer2DHT = getDHTPeer("node_2", client2Port);
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
assertTrue(futureGet.dataMap().values().size() == 2);
assertTrue(futureGet.dataMap().values().contains(new Data("hallo1")));
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
assertTrue(futureGet.dataMap().values().size() == 2);
shutdown();
}
}
@Test
public void testAddRemove() throws Exception {
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
FuturePut futurePut1 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo1")).start();
futurePut1.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut1.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
FuturePut futurePut2 = peer1DHT.add(Number160.createHash("locationKey")).data(new Data("hallo2")).start();
futurePut2.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futurePut2.isSuccess());
peer2DHT = getDHTPeer("node_2", client2Port);
Number160 contentKey = new Data("hallo1").hash();
FutureRemove futureRemove = peer2DHT.remove(Number160.createHash("locationKey")).contentKey(contentKey).start();
futureRemove.awaitUninterruptibly();
peer2DHT = getDHTPeer("node_2", client2Port);
Number160 contentKey = new Data("hallo1").hash();
FutureRemove futureRemove = peer2DHT.remove(Number160.createHash("locationKey")).contentKey(contentKey)
.start();
futureRemove.awaitUninterruptibly();
// TODO: That fails always also with localhost seed node
/*if (!ignoreSuccessTests)
assertTrue(futureRemove.isSuccess());*/
// That fails sometimes in direct mode and NAT
if (!ignoreSuccessTests)
assertTrue(futureRemove.isSuccess());
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
FutureGet futureGet = peer2DHT.get(Number160.createHash("locationKey")).all().start();
futureGet.awaitUninterruptibly();
if (!ignoreSuccessTests)
assertTrue(futureGet.isSuccess());
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
assertTrue(futureGet.dataMap().values().size() == 1);
assertTrue(futureGet.dataMap().values().contains(new Data("hallo2")));
assertTrue(futureGet.dataMap().values().size() == 1);
shutdown();
}
}
// 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.
// That will probably be fixed in a future version of TomP2P
// In relay mode the test succeeds
@Test
public void testSendDirectRelay() throws Exception {
if (forcedConnectionType == ConnectionType.RELAY || resolvedConnectionType == ConnectionType.RELAY) {
peer1DHT = getDHTPeer("node_1", client1Port);
peer2DHT = getDHTPeer("node_2", client2Port);
public void testSendDirectBetweenLocalPeers() throws Exception {
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
if (forcedConnectionType != ConnectionType.NAT && resolvedConnectionType != ConnectionType.NAT) {
peer1DHT = getDHTPeer("node_1", client1Port);
peer2DHT = getDHTPeer("node_2", client2Port);
final CountDownLatch countDownLatch = new CountDownLatch(1);
final CountDownLatch countDownLatch = new CountDownLatch(1);
final StringBuilder result = new StringBuilder();
peer2DHT.peer().objectDataReply((sender, request) -> {
countDownLatch.countDown();
result.append(String.valueOf(request));
return "pong";
});
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection(peer2DHT.peer()
.peerAddress(),
500);
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
futureDirect.awaitUninterruptibly();
final StringBuilder result = new StringBuilder();
peer2DHT.peer().objectDataReply((sender, request) -> {
countDownLatch.countDown();
result.append(String.valueOf(request));
return "pong";
});
FuturePeerConnection futurePeerConnection = peer1DHT.peer().createPeerConnection(peer2DHT.peer()
.peerAddress(), 500);
FutureDirect futureDirect = peer1DHT.peer().sendDirect(futurePeerConnection).object("hallo").start();
futureDirect.awaitUninterruptibly();
countDownLatch.await(3, TimeUnit.SECONDS);
if (countDownLatch.getCount() > 0)
Assert.fail("The test method did not complete successfully!");
countDownLatch.await(3, TimeUnit.SECONDS);
if (countDownLatch.getCount() > 0)
Assert.fail("The test method did not complete successfully!");
assertEquals("hallo", result.toString());
assertTrue(futureDirect.isSuccess());
assertEquals("pong", futureDirect.object());
assertEquals("hallo", result.toString());
assertTrue(futureDirect.isSuccess());
log.debug(futureDirect.object().toString());
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.
@Test
public void testSendDirectPortForwarding() throws Exception {
if (forcedConnectionType == ConnectionType.NAT || resolvedConnectionType == ConnectionType.NAT) {
public void testSendDirectToSeedNode() throws Exception {
for (int i = 0; i < stressTestLoopCount; i++) {
configure();
peer1DHT = getDHTPeer("node_1", client1Port);
PeerAddress reachablePeerAddress = new PeerAddress(Number160.createHash(seedId), seedIP, 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.awaitUninterruptibly();
assertTrue(futureDirect.isSuccess());
assertEquals("pong", futureDirect.object());
shutdown();
}
}
@ -556,8 +586,10 @@ public class TomP2PTests {
}
if (peer == null)
Assert.fail("Bootstrapping in all modes failed. Check if the seed node is running. " +
"seedNodeId= " + seedNodeId +
Assert.fail("Bootstrapping failed. Check if the seed node is running. " +
"forcedConnectionType= " + forcedConnectionType +
" resolvedConnectionType= " + resolvedConnectionType +
" seedNodeId= " + seedNodeId +
" seedNodeIP= " + seedNodeIP +
" seedNodePort= " + seedNodePort);