diff --git a/test/dorkbox/network/PingPongLocalTest.java b/test/dorkbox/network/PingPongLocalTest.java index 23d87772..e1f22433 100644 --- a/test/dorkbox/network/PingPongLocalTest.java +++ b/test/dorkbox/network/PingPongLocalTest.java @@ -78,12 +78,10 @@ class PingPongLocalTest extends BaseTest { // ---- - Client client = new Client(); + Client client = new Client(configuration); addEndPoint(client); final Listeners listeners1 = client.listeners(); listeners1.add(new Listener.OnConnected() { - AtomicInteger check = new AtomicInteger(0); - @Override public void connected(Connection connection) { @@ -95,8 +93,6 @@ class PingPongLocalTest extends BaseTest { }); listeners1.add(new Listener.OnError() { - AtomicInteger check = new AtomicInteger(0); - @Override public void error(Connection connection, Throwable throwable) { diff --git a/test/dorkbox/network/PingTest.java b/test/dorkbox/network/PingTest.java index 84895daf..9b8b6a1a 100644 --- a/test/dorkbox/network/PingTest.java +++ b/test/dorkbox/network/PingTest.java @@ -18,6 +18,7 @@ package dorkbox.network; import static org.junit.Assert.fail; import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; @@ -31,6 +32,36 @@ class PingTest extends BaseTest { private volatile int response = -1; + @Test + public + void pingLocal() throws SecurityException, IOException { + this.response = -1; + + Server server = new Server(); + addEndPoint(server); + server.bind(false); + + // ---- + + Client client = new Client(); + addEndPoint(client); + + client.connect(5000); + + System.err.println("Testing LOCAL ping"); + for (int i = 0; i < 10; i++) { + this.response = client.send() + .ping() + .getResponse(); + System.err.println("Ping: " + this.response); + } + + stopEndPoints(); + if (this.response == -1) { + fail(); + } + } + // ping prefers the following order: UDP, TCP @Test public @@ -66,6 +97,57 @@ class PingTest extends BaseTest { } } + @Test + public + void pingLocal_testListeners1() throws SecurityException, IOException { + this.response = -1; + + Server server = new Server(); + addEndPoint(server); + server.bind(false); + + // ---- + + Client client = new Client(); + addEndPoint(client); + + client.connect(5000); + + System.err.println("Testing LOCAL ping with multi callback"); + + final PingListener pingListener = new PingListener() { + AtomicInteger count = new AtomicInteger(); + + @Override + public + void response(Connection connection, int pingResponseTime) { + System.err.println("Ping: " + pingResponseTime); + + if (this.count.incrementAndGet() < 10) { + connection.send() + .ping() + .add(this); + } + else { + PingTest.this.response = pingResponseTime; + stopEndPoints(); + } + } + }; + + // alternate way to register for the receipt of a one-off ping response + // doesn't matter how many times this is called. If there is a PING waiting, then it's overwritten + Ping ping = client.send() + .ping(); + ping.add(pingListener); + + waitForThreads(); + + if (this.response == -1) { + fail(); + } + } + @Test public void pingTCP_testListeners1() throws SecurityException, IOException { @@ -89,14 +171,14 @@ class PingTest extends BaseTest { System.err.println("Testing TCP ping with multi callback"); final PingListener pingListener = new PingListener() { - volatile int count = 0; + AtomicInteger count = new AtomicInteger(); @Override public void response(Connection connection, int pingResponseTime) { System.err.println("Ping: " + pingResponseTime); - if (this.count++ < 10) { + if (this.count.incrementAndGet() < 10) { connection.send() .ping() .add(this); @@ -121,6 +203,47 @@ class PingTest extends BaseTest { } } + @Test + public + void pingLocal_testListeners2() throws SecurityException, IOException { + this.response = -1; + + Server server = new Server(); + addEndPoint(server); + server.bind(false); + + // ---- + + Client client = new Client(); + addEndPoint(client); + + client.connect(5000); + + System.err.println("Testing TCP ping with single callback"); + + final PingListener pingListener = new PingListener() { + @Override + public + void response(Connection connection, int pingResponseTime) { + System.err.println("Ping: " + pingResponseTime); + PingTest.this.response = pingResponseTime; + stopEndPoints(); + } + }; + + + // alternate way to register for the receipt of a one-off ping response + // doesn't matter how many times this is called. If there is a PING waiting, then it's overwritten + Ping ping = client.send() + .ping(); + ping.add(pingListener); + + waitForThreads(); + if (this.response == -1) { + fail(); + } + } + @Test public void pingTCP_testListeners2() throws SecurityException, IOException {