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 | |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 16 | import org.junit.Test; |
| 17 | import static org.junit.Assert.*; |
| 18 | import com.intel.jndn.mock.MockTransport; |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 19 | import com.intel.jndn.utils.client.FutureData; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 20 | import java.io.IOException; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 21 | import java.util.concurrent.ExecutionException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 22 | import java.util.concurrent.Future; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.concurrent.TimeoutException; |
| 25 | import java.util.logging.Logger; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.Data; |
| 27 | import net.named_data.jndn.Face; |
| 28 | import net.named_data.jndn.Name; |
| 29 | import net.named_data.jndn.util.Blob; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 30 | import org.junit.rules.ExpectedException; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 33 | * Test Client.java |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 34 | * |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 35 | * @author Andrew Brown <andrew.brown@intel.com> |
| 36 | */ |
andrewsbrown | 90712cb | 2015-03-31 14:44:12 -0700 | [diff] [blame] | 37 | public class SimpleClientTest { |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame] | 38 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 39 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
| 40 | public ExpectedException thrown = ExpectedException.none(); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 41 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Test retrieving data synchronously |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 44 | * |
| 45 | * @throws java.io.IOException |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 46 | */ |
| 47 | @Test |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 48 | public void testGetSync() throws IOException { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 49 | // setup face |
| 50 | MockTransport transport = new MockTransport(); |
| 51 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 52 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 53 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 54 | Data response = new Data(new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 55 | response.setContent(new Blob("...")); |
| 56 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 57 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 58 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 59 | logger.info("Client expressing interest synchronously: /test/sync"); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 60 | SimpleClient client = new SimpleClient(); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 61 | Data data = client.getSync(face, new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 62 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 63 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 64 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Test retrieving data asynchronously |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame] | 67 | * |
| 68 | * @throws InterruptedException |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 69 | */ |
| 70 | @Test |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 71 | public void testGetAsync() throws InterruptedException, ExecutionException { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 72 | // setup face |
| 73 | MockTransport transport = new MockTransport(); |
| 74 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 75 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 76 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 77 | Data response = new Data(new Name("/test/async")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 78 | response.setContent(new Blob("...")); |
| 79 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 80 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 81 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 82 | logger.info("Client expressing interest asynchronously: /test/async"); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 83 | SimpleClient client = new SimpleClient(); |
| 84 | Future<Data> futureData = client.getAsync(face, new Name("/test/async")); |
| 85 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 86 | assertTrue(!futureData.isDone()); |
| 87 | futureData.get(); |
| 88 | assertTrue(futureData.isDone()); |
| 89 | assertEquals(new Blob("...").toString(), futureData.get().getContent().toString()); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 90 | } |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Test that asynchronous client times out correctly |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 94 | * |
| 95 | * @throws InterruptedException |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 96 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 97 | @Test(expected = TimeoutException.class) |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 98 | public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 99 | // setup face |
| 100 | MockTransport transport = new MockTransport(); |
| 101 | Face face = new Face(transport, null); |
| 102 | |
| 103 | // retrieve non-existent data, should timeout |
| 104 | logger.info("Client expressing interest asynchronously: /test/timeout"); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 105 | Future<Data> futureData = SimpleClient.getDefault().getAsync(face, new Name("/test/timeout")); |
| 106 | |
| 107 | // expect an exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 108 | futureData.get(50, TimeUnit.MILLISECONDS); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 109 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Test that a sync failed request fails with an exception. |
| 113 | */ |
| 114 | @Test(expected = ExecutionException.class) |
| 115 | public void testAsyncFailureToRetrieve() throws InterruptedException, ExecutionException { |
| 116 | Future future = SimpleClient.getDefault().getAsync(new Face(), new Name("/test/no-data")); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 117 | future.get(); |
Andrew Brown | 2659ec3 | 2015-05-07 11:05:50 -0700 | [diff] [blame] | 118 | assertTrue(future.isDone()); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Test that a sync failed request fails with an exception. |
| 123 | */ |
| 124 | @Test(expected = IOException.class) |
| 125 | public void testSyncFailureToRetrieve() throws IOException { |
| 126 | SimpleClient.getDefault().getSync(new Face(), new Name("/test/no-data")); |
| 127 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 128 | } |