Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 4feb2da | 2015-03-03 16:05:29 -0800 | [diff] [blame] | 2 | * jndn-utils |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 16 | import com.intel.jndn.mock.MockFace; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 17 | import org.junit.Test; |
| 18 | import static org.junit.Assert.*; |
| 19 | import com.intel.jndn.mock.MockTransport; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 20 | import java.io.IOException; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 21 | import java.util.concurrent.CompletableFuture; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 22 | import java.util.concurrent.ExecutionException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 23 | import java.util.concurrent.Future; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 24 | import java.util.concurrent.TimeUnit; |
| 25 | import java.util.concurrent.TimeoutException; |
| 26 | import java.util.logging.Logger; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 27 | import net.named_data.jndn.Data; |
| 28 | import net.named_data.jndn.Face; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 29 | import net.named_data.jndn.Interest; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 30 | import net.named_data.jndn.Name; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 31 | import net.named_data.jndn.encoding.EncodingException; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 32 | import net.named_data.jndn.util.Blob; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 33 | import org.junit.rules.ExpectedException; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 36 | * Test SimpleClient.java |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 37 | * |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 38 | * @author Andrew Brown <andrew.brown@intel.com> |
| 39 | */ |
andrewsbrown | 90712cb | 2015-03-31 14:44:12 -0700 | [diff] [blame] | 40 | public class SimpleClientTest { |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame] | 41 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 42 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
| 43 | public ExpectedException thrown = ExpectedException.none(); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 44 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 45 | @Test |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 46 | public void testGetSync() throws IOException { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 47 | // setup face |
| 48 | MockTransport transport = new MockTransport(); |
| 49 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 50 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 51 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 52 | Data response = new Data(new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 53 | response.setContent(new Blob("...")); |
| 54 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 55 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 56 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 57 | logger.info("Client expressing interest synchronously: /test/sync"); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 58 | SimpleClient client = new SimpleClient(); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 59 | Data data = client.getSync(face, new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 60 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 61 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 62 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 63 | @Test |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 64 | public void testGetAsync() throws InterruptedException, ExecutionException, IOException, EncodingException { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 65 | // setup face |
| 66 | MockTransport transport = new MockTransport(); |
| 67 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 68 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 69 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 70 | Data response = new Data(new Name("/test/async")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 71 | response.setContent(new Blob("...")); |
| 72 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 73 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 74 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 75 | logger.info("Client expressing interest asynchronously: /test/async"); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 76 | SimpleClient client = new SimpleClient(); |
| 77 | Future<Data> futureData = client.getAsync(face, new Name("/test/async")); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 78 | assertTrue(!futureData.isDone()); |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 79 | |
| 80 | // process events to retrieve data |
| 81 | face.processEvents(); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 82 | assertTrue(futureData.isDone()); |
| 83 | assertEquals(new Blob("...").toString(), futureData.get().getContent().toString()); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 84 | } |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 85 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 86 | @Test |
| 87 | public void testTimeout() throws Exception { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 88 | // setup face |
| 89 | MockTransport transport = new MockTransport(); |
| 90 | Face face = new Face(transport, null); |
| 91 | |
| 92 | // retrieve non-existent data, should timeout |
| 93 | logger.info("Client expressing interest asynchronously: /test/timeout"); |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 94 | Interest interest = new Interest(new Name("/test/timeout"), 1); |
| 95 | CompletableFuture<Data> futureData = SimpleClient.getDefault().getAsync(face, interest); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 96 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 97 | // wait for NDN timeout |
| 98 | Thread.sleep(2); |
| 99 | face.processEvents(); |
| 100 | |
| 101 | // verify that the client is completing the future with a TimeoutException |
| 102 | assertTrue(futureData.isDone()); |
| 103 | assertTrue(futureData.isCompletedExceptionally()); |
| 104 | try{ |
| 105 | futureData.get(); |
| 106 | } |
| 107 | catch(ExecutionException e){ |
| 108 | assertTrue(e.getCause() instanceof TimeoutException); |
| 109 | } |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 110 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 111 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 112 | @Test(expected = Exception.class) |
| 113 | public void testAsyncFailureToRetrieve() throws Exception { |
| 114 | Face face = new MockFace(); |
| 115 | |
| 116 | logger.info("Client expressing interest asynchronously: /test/no-data"); |
| 117 | Interest interest = new Interest(new Name("/test/no-data"), 10); |
| 118 | Future future = SimpleClient.getDefault().getAsync(face, interest); |
| 119 | |
| 120 | face.processEvents(); |
| 121 | future.get(15, TimeUnit.MILLISECONDS); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 122 | } |
| 123 | |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 124 | @Test(expected = IOException.class) |
| 125 | public void testSyncFailureToRetrieve() throws IOException { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 126 | logger.info("Client expressing interest synchronously: /test/no-data"); |
| 127 | Interest interest = new Interest(new Name("/test/no-data"), 10); |
| 128 | SimpleClient.getDefault().getSync(new Face(), interest); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 129 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 130 | } |