Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: ClientTest.java |
| 3 | * |
| 4 | * Purpose: Test Client.java |
| 5 | * |
| 6 | * © Copyright Intel Corporation. All rights reserved. |
| 7 | * Intel Corporation, 2200 Mission College Boulevard, |
| 8 | * Santa Clara, CA 95052-8119, USA |
| 9 | */ |
| 10 | package com.intel.jndn.utils; |
| 11 | |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 12 | import org.junit.Test; |
| 13 | import static org.junit.Assert.*; |
| 14 | import com.intel.jndn.mock.MockTransport; |
| 15 | import net.named_data.jndn.Data; |
| 16 | import net.named_data.jndn.Face; |
| 17 | import net.named_data.jndn.Name; |
| 18 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 19 | import org.apache.logging.log4j.LogManager; |
| 20 | import org.apache.logging.log4j.Logger; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 23 | * Test Client.java |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 24 | * @author Andrew Brown <andrew.brown@intel.com> |
| 25 | */ |
| 26 | public class ClientTest { |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame] | 27 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Setup logging |
| 30 | */ |
| 31 | private static final Logger logger = LogManager.getLogger(); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 32 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Test retrieving data synchronously |
| 35 | */ |
| 36 | @Test |
| 37 | public void testGetSync() { |
| 38 | // setup face |
| 39 | MockTransport transport = new MockTransport(); |
| 40 | Face face = new Face(transport, null); |
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 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 43 | Data response = new Data(new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 44 | response.setContent(new Blob("...")); |
| 45 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 46 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 47 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 48 | logger.info("Client expressing interest synchronously: /test/sync"); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 49 | Client client = new Client(); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 50 | Data data = client.getSync(face, new Name("/test/sync")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 51 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 52 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 53 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Test retrieving data asynchronously |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame] | 56 | * |
| 57 | * @throws InterruptedException |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 58 | */ |
| 59 | @Test |
| 60 | public void testGetAsync() throws InterruptedException { |
| 61 | // setup face |
| 62 | MockTransport transport = new MockTransport(); |
| 63 | Face face = new Face(transport, null); |
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 | // setup return data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 66 | Data response = new Data(new Name("/test/async")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 67 | response.setContent(new Blob("...")); |
| 68 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 69 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 70 | // retrieve data |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 71 | logger.info("Client expressing interest asynchronously: /test/async"); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 72 | Client client = new Client(); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 73 | NDNObserver observer = client.get(face, new Name("/test/async")); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 74 | |
| 75 | // wait |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 76 | while (observer.eventCount() == 0) { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 77 | Thread.sleep(10); |
| 78 | } |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 79 | assertEquals(1, observer.eventCount()); |
| 80 | assertEquals(1, observer.dataCount()); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 81 | Data data = (Data) observer.getFirst().getPacket(); |
| 82 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 83 | } |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Test that asynchronous client times out correctly |
| 87 | * |
| 88 | * @throws InterruptedException |
| 89 | */ |
| 90 | @Test |
| 91 | public void testTimeout() throws InterruptedException { |
| 92 | // setup face |
| 93 | MockTransport transport = new MockTransport(); |
| 94 | Face face = new Face(transport, null); |
| 95 | |
| 96 | // retrieve non-existent data, should timeout |
| 97 | logger.info("Client expressing interest asynchronously: /test/timeout"); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 98 | NDNObserver observer = Client.getDefault().get(face, new Name("/test/timeout")); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 99 | |
| 100 | // wait |
| 101 | while (observer.errorCount() == 0) { |
| 102 | Thread.sleep(100); |
| 103 | } |
| 104 | Exception e = (Exception) observer.getFirst().getPacket(); |
| 105 | assertEquals(1, observer.errorCount()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Test that callback is called on event |
| 110 | * @throws InterruptedException |
| 111 | */ |
| 112 | @Test |
| 113 | public void testCallback() throws InterruptedException { |
| 114 | // setup face |
| 115 | MockTransport transport = new MockTransport(); |
| 116 | Face face = new Face(transport, null); |
| 117 | |
| 118 | // setup return data |
| 119 | Data response = new Data(new Name("/test/callback")); |
| 120 | response.setContent(new Blob("...")); |
| 121 | transport.respondWith(response); |
| 122 | |
| 123 | // retrieve non-existent data, should timeout |
| 124 | logger.info("Client expressing interest asynchronously: /test/callback"); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 125 | NDNObserver observer = Client.getDefault().get(face, new Name("/test/callback")); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 126 | observer.then(new OnEvent(){ |
| 127 | @Override |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 128 | public void onEvent(NDNEvent event) { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 129 | assertEquals(new Blob("...").buf(), ((Data) event.getPacket()).getContent().buf()); |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | // wait |
| 134 | while (observer.eventCount() == 0) { |
| 135 | Thread.sleep(100); |
| 136 | } |
| 137 | assertEquals(1, observer.eventCount()); |
| 138 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 139 | } |