Andrew Brown | db45705 | 2015-02-21 15:41:58 -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 | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
| 16 | import com.intel.jndn.mock.MockFace; |
| 17 | import java.io.IOException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 18 | import java.util.List; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 19 | import java.util.concurrent.Future; |
| 20 | import java.util.logging.Logger; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 21 | import net.named_data.jndn.Data; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 22 | import net.named_data.jndn.Face; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 23 | import net.named_data.jndn.Interest; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 24 | import net.named_data.jndn.InterestFilter; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.Name; |
| 26 | import net.named_data.jndn.Name.Component; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 27 | import net.named_data.jndn.OnInterestCallback; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 28 | import net.named_data.jndn.util.Blob; |
| 29 | import org.junit.Test; |
| 30 | import static org.junit.Assert.*; |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 31 | import org.junit.Before; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Test SegmentedClient functionality. |
| 35 | * |
| 36 | * @author Andrew Brown <andrew.brown@intel.com> |
| 37 | */ |
| 38 | public class SegmentedClientTest { |
| 39 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 40 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 41 | private SegmentedClient instance; |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 42 | private MockFace face; |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 43 | |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 44 | @Before |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 45 | public void beforeTest() { |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 46 | face = new MockFace(); |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 47 | instance = new SegmentedClient(1, 100); // warning: setting an interest |
| 48 | // lifetime that is too low will cause the getSync() tests to fail due to |
| 49 | // Thread.sleep() |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 50 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 51 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 52 | @Test |
| 53 | public void testGetSync() throws Exception { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 54 | Name name = new Name("/segmented/data"); |
| 55 | face.registerPrefix(name, new OnInterestCallback() { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 56 | private int count = 0; |
| 57 | private int max = 9; |
| 58 | |
| 59 | @Override |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 60 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 61 | Data data = new Data(interest.getName()); |
| 62 | if (!SegmentedClient.hasSegment(data.getName())) { |
| 63 | data.getName().appendSegment(0); |
| 64 | } |
| 65 | data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00)); |
| 66 | data.setContent(new Blob(".")); |
| 67 | try { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 68 | face.putData(data); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 69 | } catch (IOException e) { |
| 70 | fail(e.getMessage()); |
| 71 | } |
| 72 | } |
| 73 | }, null); |
| 74 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 75 | logger.info("Client retrieving segments synchronously: " + name.toUri()); |
| 76 | Data data = instance.getSync(face, new Name(name).appendSegment(0)); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 77 | assertEquals(10, data.getContent().size()); |
| 78 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Test that a failed request fails with an exception. |
| 82 | * |
| 83 | * @throws java.lang.Exception |
| 84 | */ |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 85 | // @Test(expected = ExecutionException.class) |
| 86 | // public void testFailureToRetrieve() throws Exception { |
| 87 | // // retrieve non-existent data, should timeout |
| 88 | // logger.info("Client retrieving segments asynchronously: /test/no-data"); |
| 89 | // Future<Data> futureData = instance.getAsync(face, new Name("/test/no-data")); |
| 90 | // face.processEvents(); |
| 91 | // futureData.get(); |
| 92 | // } |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 93 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 94 | // /** |
| 95 | // * Test that a sync failed request fails with an exception. |
| 96 | // */ |
| 97 | // @Test(expected = IOException.class) |
| 98 | // public void testSyncFailureToRetrieve() throws IOException { |
| 99 | // logger.info("Client retrieving segments synchronously: /test/no-data"); |
| 100 | // instance.getSync(face, new Name("/test/no-data")); |
| 101 | // } |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame] | 104 | * Verify that Data returned with a different Name than the Interest is still |
| 105 | * segmented correctly. |
| 106 | * |
| 107 | * @throws Exception |
| 108 | */ |
| 109 | @Test |
| 110 | public void testWhenDataNameIsLongerThanInterestName() throws Exception { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 111 | final List<Data> segments = TestHelper.buildSegments(new Name("/a/b/c/d"), 0, 10); |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame] | 112 | for (Data segment : segments) { |
| 113 | face.addResponse(segment.getName(), segment); |
| 114 | } |
| 115 | |
| 116 | Name name = new Name("/a/b"); |
| 117 | face.addResponse(name, segments.get(0)); |
| 118 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 119 | logger.info("Client retrieving segments synchronously: " + name.toUri()); |
| 120 | Data data = instance.getSync(face, name); |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame] | 121 | assertNotNull(data); |
| 122 | assertEquals("/a/b/c/d", data.getName().toUri()); |
| 123 | } |
| 124 | |
| 125 | /** |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 126 | * Verify that Data packets with no content do not cause errors; identifies |
| 127 | * bug. |
| 128 | * |
| 129 | * @throws Exception |
| 130 | */ |
| 131 | @Test |
| 132 | public void testNoContent() throws Exception { |
| 133 | Name name = new Name("/test/no-content").appendSegment(0); |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 134 | Data data = TestHelper.buildData(name, "", 0); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 135 | face.addResponse(name, data); |
| 136 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 137 | logger.info("Client retrieving segments asynchronously: /test/no-content"); |
| 138 | Future<Data> result = instance.getAsync(face, name); |
| 139 | face.processEvents(); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 140 | assertEquals("/test/no-content", result.get().getName().toUri()); |
| 141 | assertEquals("", result.get().getContent().toString()); |
| 142 | } |
| 143 | |
| 144 | /** |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 145 | * Verify that segmented content is the correct length when retrieved by the |
| 146 | * client. |
| 147 | * |
| 148 | * @throws Exception |
| 149 | */ |
| 150 | @Test |
| 151 | public void testContentLength() throws Exception { |
| 152 | Data data1 = new Data(new Name("/test/content-length").appendSegment(0)); |
| 153 | data1.setContent(new Blob("0123456789")); |
| 154 | data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00)); |
| 155 | face.addResponse(data1.getName(), data1); |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame] | 156 | |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 157 | Data data2 = new Data(new Name("/test/content-length").appendSegment(1)); |
| 158 | data2.setContent(new Blob("0123456789")); |
| 159 | data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00)); |
| 160 | face.addResponse(data2.getName(), data2); |
| 161 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 162 | logger.info("Client retrieving segments asynchronously: /test/content-length"); |
| 163 | Future<Data> result = instance.getAsync(face, new Name("/test/content-length").appendSegment(0)); |
| 164 | face.processEvents(); |
| 165 | face.processEvents(); |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 166 | assertEquals(20, result.get().getContent().size()); |
| 167 | } |
| 168 | |
| 169 | /** |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 170 | * If a Data packet does not have a FinalBlockId, the SegmentedClient should |
| 171 | * just return the packet. |
| 172 | * |
| 173 | * @throws Exception |
| 174 | */ |
| 175 | @Test |
| 176 | public void testNoFinalBlockId() throws Exception { |
| 177 | Name name = new Name("/test/no-final-block-id"); |
| 178 | Data data = new Data(name); |
| 179 | data.setContent(new Blob("1")); |
| 180 | face.addResponse(name, data); |
| 181 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame^] | 182 | logger.info("Client retrieving segments asynchronously: /test/no-final-block-id"); |
| 183 | Future<Data> result = instance.getAsync(face, name); |
| 184 | face.processEvents(); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 185 | assertEquals("/test/no-final-block-id", result.get().getName().toUri()); |
| 186 | assertEquals("1", result.get().getContent().toString()); |
| 187 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 188 | } |