Upgrade API: use base interface, return vanilla Future<Data>, throw exceptions instead of returning null, and improve underlying implementation/tests
diff --git a/src/test/java/com/intel/jndn/utils/ClientTest.java b/src/test/java/com/intel/jndn/utils/ClientTest.java
index d4ca951..af22560 100644
--- a/src/test/java/com/intel/jndn/utils/ClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/ClientTest.java
@@ -16,7 +16,9 @@
import org.junit.Test;
import static org.junit.Assert.*;
import com.intel.jndn.mock.MockTransport;
+import java.io.IOException;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
@@ -28,20 +30,21 @@
/**
* Test Client.java
+ *
* @author Andrew Brown <andrew.brown@intel.com>
*/
public class ClientTest {
- /**
- * Setup logging
- */
- private static final Logger logger = Logger.getLogger(Client.class.getName());
+ private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
+ public ExpectedException thrown = ExpectedException.none();
/**
* Test retrieving data synchronously
+ *
+ * @throws java.io.IOException
*/
@Test
- public void testGetSync() {
+ public void testGetSync() throws IOException {
// setup face
MockTransport transport = new MockTransport();
Face face = new Face(transport, null);
@@ -53,7 +56,7 @@
// retrieve data
logger.info("Client expressing interest synchronously: /test/sync");
- Client client = new Client();
+ SimpleClient client = new SimpleClient();
Data data = client.getSync(face, new Name("/test/sync"));
assertEquals(new Blob("...").buf(), data.getContent().buf());
}
@@ -76,9 +79,9 @@
// retrieve data
logger.info("Client expressing interest asynchronously: /test/async");
- Client client = new Client();
- FutureData futureData = client.getAsync(face, new Name("/test/async"));
-
+ SimpleClient client = new SimpleClient();
+ Future<Data> futureData = client.getAsync(face, new Name("/test/async"));
+
assertTrue(!futureData.isDone());
futureData.get();
assertTrue(futureData.isDone());
@@ -87,10 +90,10 @@
/**
* Test that asynchronous client times out correctly
- *
- * @throws InterruptedException
+ *
+ * @throws InterruptedException
*/
- @Test
+ @Test(expected = TimeoutException.class)
public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException {
// setup face
MockTransport transport = new MockTransport();
@@ -98,9 +101,9 @@
// retrieve non-existent data, should timeout
logger.info("Client expressing interest asynchronously: /test/timeout");
- FutureData futureData = Client.getDefault().getAsync(face, new Name("/test/timeout"));
-
- ExpectedException.none().expect(TimeoutException.class);
+ Future<Data> futureData = SimpleClient.getDefault().getAsync(face, new Name("/test/timeout"));
+
+ // expect an exception
futureData.get(50, TimeUnit.MILLISECONDS);
}
}
diff --git a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
index dd75f3e..e6773ec 100644
--- a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
@@ -14,8 +14,14 @@
package com.intel.jndn.utils;
import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockTransport;
import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.logging.Logger;
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.Name.Component;
@@ -32,8 +38,12 @@
*/
public class SegmentedClientTest {
+ private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
+
/**
* Test of getSync method, of class SegmentedClient.
+ *
+ * @throws java.lang.Exception
*/
@Test
public void testGetSync() throws Exception {
@@ -61,4 +71,27 @@
Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0));
assertEquals(10, data.getContent().size());
}
+
+ /**
+ * Test that a failed request fails with an exception.
+ *
+ * @throws java.lang.Exception
+ */
+ @Test(expected = ExecutionException.class)
+ public void testFailureToRetrieve() throws Exception {
+ // setup face
+ MockTransport transport = new MockTransport();
+ Face face = new Face(transport, null);
+
+ // retrieve non-existent data, should timeout
+ logger.info("Client expressing interest asynchronously: /test/no-data");
+ List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data"));
+
+ // the list of future packets should be initialized
+ assertEquals(1, futureSegments.size());
+ assertTrue(futureSegments.get(0).isDone());
+
+ // should throw error
+ futureSegments.get(0).get();
+ }
}
diff --git a/src/test/java/com/intel/jndn/utils/ServerTest.java b/src/test/java/com/intel/jndn/utils/ServerTest.java
index 2488f0b..0c2645b 100644
--- a/src/test/java/com/intel/jndn/utils/ServerTest.java
+++ b/src/test/java/com/intel/jndn/utils/ServerTest.java
@@ -33,7 +33,7 @@
/**
* Setup logging
*/
- private static final Logger logger = Logger.getLogger(Client.class.getName());
+ private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
/**
* Test on functionality