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