Fix tests using MockForwarder
TODO DefaultRetryClient is still broken
diff --git a/pom.xml b/pom.xml
index 6c5c33e..0c7428a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
<dependency>
<groupId>com.intel.jndn.mock</groupId>
<artifactId>jndn-mock</artifactId>
- <version>1.0.3</version>
+ <version>1.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/src/main/java/com/intel/jndn/utils/client/impl/AdvancedClient.java b/src/main/java/com/intel/jndn/utils/client/impl/AdvancedClient.java
index be1e876..2659656 100644
--- a/src/main/java/com/intel/jndn/utils/client/impl/AdvancedClient.java
+++ b/src/main/java/com/intel/jndn/utils/client/impl/AdvancedClient.java
@@ -150,7 +150,7 @@
this.face = face;
this.stream = stream;
}
-
+
@Override
public void onTimeout(Interest failedInterest) {
logger.info("Timeout: " + failedInterest.toUri());
diff --git a/src/main/java/com/intel/jndn/utils/client/impl/DefaultRetryClient.java b/src/main/java/com/intel/jndn/utils/client/impl/DefaultRetryClient.java
index f984ac2..21f1c0e 100644
--- a/src/main/java/com/intel/jndn/utils/client/impl/DefaultRetryClient.java
+++ b/src/main/java/com/intel/jndn/utils/client/impl/DefaultRetryClient.java
@@ -32,7 +32,7 @@
private static final Logger logger = Logger.getLogger(DefaultRetryClient.class.getName());
private final int numRetriesAllowed;
- private int totalRetries = 0;
+ private volatile int totalRetries = 0;
public DefaultRetryClient(int numRetriesAllowed) {
this.numRetriesAllowed = numRetriesAllowed;
diff --git a/src/test/java/com/intel/jndn/utils/client/impl/AdvancedClientTest.java b/src/test/java/com/intel/jndn/utils/client/impl/AdvancedClientTest.java
index 26942d0..3ee3bb8 100644
--- a/src/test/java/com/intel/jndn/utils/client/impl/AdvancedClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/client/impl/AdvancedClientTest.java
@@ -14,9 +14,9 @@
package com.intel.jndn.utils.client.impl;
import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockForwarder;
import com.intel.jndn.utils.TestHelper;
import com.intel.jndn.utils.client.SegmentationType;
-import com.intel.jndn.utils.server.impl.SimpleServer;
import net.named_data.jndn.Data;
import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
@@ -32,22 +32,20 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
/**
- *
- * @author Andrew Brown <andrew.brown@intel.com>
+ * @author Andrew Brown, andrew.brown@intel.com
*/
public class AdvancedClientTest {
- MockFace face;
- AdvancedClient instance;
+ private MockFace face;
+ private AdvancedClient instance;
+ private MockForwarder forwarder;
@Before
public void before() throws Exception {
+ forwarder = new MockForwarder();
face = new MockFace();
instance = new AdvancedClient();
}
@@ -70,8 +68,8 @@
public void testGetSync() throws Exception {
Name name = new Name("/segmented/data");
+ Face face = forwarder.connect();
face.registerPrefix(name, new OnInterestCallback() {
- private int count = 0;
private int max = 9;
@Override
@@ -93,12 +91,12 @@
Data data = instance.getSync(face, new Name(name).appendSegment(0));
assertEquals(10, data.getContent().size());
}
-
- /**
+
+ /**
* Verify that Data returned with a different Name than the Interest is still
* segmented correctly.
*
- * @throws Exception
+ * @throws Exception if client fails
*/
@Test
public void testWhenDataNameIsLongerThanInterestName() throws Exception {
@@ -119,7 +117,7 @@
* Verify that Data packets with no content do not cause errors; identifies
* bug.
*
- * @throws Exception
+ * @throws Exception if client fails
*/
@Test
public void testNoContent() throws Exception {
@@ -137,7 +135,7 @@
* Verify that segmented content is the correct length when retrieved by the
* client.
*
- * @throws Exception
+ * @throws Exception if client fails
*/
@Test
public void testContentLength() throws Exception {
@@ -159,9 +157,9 @@
/**
* If a Data packet does not have a FinalBlockId, the AdvancedClient should
- just return the packet.
+ * just return the packet.
*
- * @throws Exception
+ * @throws Exception if client fails
*/
@Test
public void testNoFinalBlockId() throws Exception {
diff --git a/src/test/java/com/intel/jndn/utils/client/impl/DefaultRetryClientTest.java b/src/test/java/com/intel/jndn/utils/client/impl/DefaultRetryClientTest.java
index 6cf256b..f529b58 100644
--- a/src/test/java/com/intel/jndn/utils/client/impl/DefaultRetryClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/client/impl/DefaultRetryClientTest.java
@@ -13,71 +13,57 @@
*/
package com.intel.jndn.utils.client.impl;
-import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockForwarder;
import com.intel.jndn.utils.TestHelper.TestCounter;
import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
import net.named_data.jndn.Name;
-import net.named_data.jndn.OnData;
-import net.named_data.jndn.OnTimeout;
-import net.named_data.jndn.encoding.EncodingException;
import org.junit.Before;
import org.junit.Test;
-import java.io.IOException;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Test DefaultRetryClient
*
- * @author Andrew Brown <andrew.brown@intel.com>
+ * @author Andrew Brown, andrew.brown@intel.com
*/
public class DefaultRetryClientTest {
- DefaultRetryClient client;
- MockFace face;
- Name name = new Name("/test/retry/client");
- Interest interest = new Interest(name, 0.0);
- TestCounter counter = new TestCounter();
+ private static final double INTEREST_LIFETIME_MS = 1.0;
+ private DefaultRetryClient client;
+ private Name name = new Name("/test/retry/client");
+ private Interest interest = new Interest(name, INTEREST_LIFETIME_MS);
+ private TestCounter counter = new TestCounter();
@Before
public void before() throws Exception {
- face = new MockFace();
client = new DefaultRetryClient(3);
}
@Test
public void testRetry() throws Exception {
-
- client.retry(face, interest, new OnData() {
- @Override
- public void onData(Interest interest, Data data) {
- counter.count++;
- }
- }, new OnTimeout() {
- @Override
- public void onTimeout(Interest interest) {
- fail("Should not timeout.");
- }
- });
+ MockForwarder forwarder = new MockForwarder();
+ Face face = forwarder.connect();
+ client.retry(face, interest, (interest1, data) -> counter.count++, interest2 -> fail("Should not timeout."));
assertEquals(1, client.totalRetries());
- timeoutAndVerifyRetry(2);
- timeoutAndVerifyRetry(3);
- respondToRetryAttempt();
+ timeoutAndVerifyRetry(face, 2);
+ timeoutAndVerifyRetry(face, 3);
+ respondToRetryAttempt(face);
}
- private void timeoutAndVerifyRetry(int retryCount) throws Exception {
- Thread.sleep(1);
+ private void timeoutAndVerifyRetry(Face face, int retryCount) throws Exception {
+ Thread.sleep((long) INTEREST_LIFETIME_MS + 1); // necessary to timeout the pending interest
face.processEvents();
assertEquals(retryCount, client.totalRetries());
assertEquals(0, counter.count);
}
- protected void respondToRetryAttempt() throws IOException, EncodingException {
- face.receive(new Data(name));
+ private void respondToRetryAttempt(Face face) throws Exception {
+ face.putData(new Data(name));
face.processEvents();
assertEquals(1, counter.count);
}
diff --git a/src/test/java/com/intel/jndn/utils/client/impl/DefaultSegmentedClientTest.java b/src/test/java/com/intel/jndn/utils/client/impl/DefaultSegmentedClientTest.java
index 060350b..05c85f5 100644
--- a/src/test/java/com/intel/jndn/utils/client/impl/DefaultSegmentedClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/client/impl/DefaultSegmentedClientTest.java
@@ -13,11 +13,14 @@
*/
package com.intel.jndn.utils.client.impl;
+import com.intel.jndn.mock.MeasurableFace;
import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockForwarder;
import com.intel.jndn.utils.TestHelper;
import com.intel.jndn.utils.TestHelper.TestCounter;
import com.intel.jndn.utils.client.DataStream;
import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
import net.named_data.jndn.Name;
import org.junit.Test;
@@ -27,11 +30,11 @@
/**
* Test DefaultSegmentedClient
*
- * @author Andrew Brown <andrew.brown@intel.com>
+ * @author Andrew Brown, andrew.brown@intel.com
*/
public class DefaultSegmentedClientTest {
- DefaultSegmentedClient instance = new DefaultSegmentedClient();
+ private DefaultSegmentedClient instance = new DefaultSegmentedClient();
@Test
public void testGetSegmentsAsync() throws Exception {
@@ -41,9 +44,7 @@
DataStream stream = instance.getSegmentsAsync(face, interest);
TestCounter counter = new TestCounter();
- stream.observe((i, d) -> {
- counter.count++;
- });
+ stream.observe((i, d) -> counter.count++);
for (Data segment : TestHelper.buildSegments(name, 0, 5)) {
stream.onData(interest, segment);
@@ -72,15 +73,14 @@
@Test
public void verifyThatSegmentsAreRetrievedOnlyOnce() throws Exception {
- MockFace face = new MockFace();
+ MockForwarder forwarder = new MockForwarder();
+ Face face = forwarder.connect();
Name name = new Name("/test/segmented/client");
Interest interest = new Interest(name);
DataStream stream = instance.getSegmentsAsync(face, interest);
TestCounter counter = new TestCounter();
- stream.observe((i, d) -> {
- counter.count++;
- });
+ stream.observe((i, d) -> counter.count++);
for (Data segment : TestHelper.buildSegments(name, 0, 5)) {
face.putData(segment);
@@ -88,7 +88,7 @@
}
assertEquals(5, counter.count);
- assertEquals(5, face.sentInterests.size());
+ assertEquals(5, ((MeasurableFace) face).sentInterests().size());
assertEquals("01234", stream.assemble().getContent().toString());
}
}
diff --git a/src/test/java/com/intel/jndn/utils/server/impl/SegmentedServerTest.java b/src/test/java/com/intel/jndn/utils/server/impl/SegmentedServerTest.java
index 409edbe..fd6b516 100644
--- a/src/test/java/com/intel/jndn/utils/server/impl/SegmentedServerTest.java
+++ b/src/test/java/com/intel/jndn/utils/server/impl/SegmentedServerTest.java
@@ -13,10 +13,12 @@
*/
package com.intel.jndn.utils.server.impl;
+import com.intel.jndn.mock.MockForwarder;
import com.intel.jndn.utils.client.impl.AdvancedClient;
import com.intel.jndn.mock.MockFace;
import java.io.IOException;
import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
import net.named_data.jndn.Name;
import net.named_data.jndn.util.Blob;
@@ -31,12 +33,13 @@
*/
public class SegmentedServerTest {
- MockFace face;
+ Face face;
SegmentedServer instance;
@Before
public void before() throws Exception {
- face = new MockFace();
+ MockForwarder forwarder = new MockForwarder();
+ face = forwarder.connect();
instance = new SegmentedServer(face, new Name("/test/prefix"));
}
diff --git a/src/test/java/com/intel/jndn/utils/server/impl/SimpleServerTest.java b/src/test/java/com/intel/jndn/utils/server/impl/SimpleServerTest.java
index 01df6f2..4ce40ae 100644
--- a/src/test/java/com/intel/jndn/utils/server/impl/SimpleServerTest.java
+++ b/src/test/java/com/intel/jndn/utils/server/impl/SimpleServerTest.java
@@ -13,11 +13,14 @@
*/
package com.intel.jndn.utils.server.impl;
+import com.intel.jndn.mock.MeasurableFace;
import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockForwarder;
import com.intel.jndn.utils.server.RespondWithBlob;
import com.intel.jndn.utils.server.RespondWithData;
import java.io.IOException;
import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
import net.named_data.jndn.Interest;
import net.named_data.jndn.Name;
import net.named_data.jndn.OnData;
@@ -35,12 +38,13 @@
*/
public class SimpleServerTest {
- MockFace face;
+ Face face;
SimpleServer instance;
@Before
public void before() throws Exception {
- face = new MockFace();
+ MockForwarder forwarder = new MockForwarder();
+ face = forwarder.connect();
instance = new SimpleServer(face, new Name("/test/prefix"));
}
@@ -90,15 +94,10 @@
private void sendAndCheckOneInterest(Name interestName) throws EncodingException, IOException {
Interest interest = new Interest(interestName);
- face.expressInterest(interest, new OnData() {
- @Override
- public void onData(Interest interest, Data data) {
- assertEquals("/test/prefix/response", data.getName().toUri());
- }
- });
-
+ face.expressInterest(interest, (interest1, data) -> assertEquals(interestName, data.getName()));
face.processEvents();
- assertEquals(1, face.sentData.size());
- assertEquals("...", face.sentData.get(0).getContent().toString());
+
+ assertEquals(1, ((MeasurableFace) face).sentDatas().size());
+ assertEquals("...", ((MeasurableFace) face).sentDatas().iterator().next().getContent().toString());
}
}