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; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 17 | import com.intel.jndn.mock.MockTransport; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 19 | import java.util.List; |
| 20 | import java.util.concurrent.ExecutionException; |
| 21 | import java.util.concurrent.Future; |
| 22 | import java.util.logging.Logger; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 23 | import net.named_data.jndn.Data; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 24 | import net.named_data.jndn.Face; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.Interest; |
| 26 | import net.named_data.jndn.Name; |
| 27 | import net.named_data.jndn.Name.Component; |
| 28 | import net.named_data.jndn.OnInterest; |
| 29 | import net.named_data.jndn.transport.Transport; |
| 30 | import net.named_data.jndn.util.Blob; |
| 31 | import org.junit.Test; |
| 32 | import static org.junit.Assert.*; |
| 33 | |
| 34 | /** |
| 35 | * Test SegmentedClient functionality. |
| 36 | * |
| 37 | * @author Andrew Brown <andrew.brown@intel.com> |
| 38 | */ |
| 39 | public class SegmentedClientTest { |
| 40 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 41 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
| 42 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Test of getSync method, of class SegmentedClient. |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 45 | * |
| 46 | * @throws java.lang.Exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 47 | */ |
| 48 | @Test |
| 49 | public void testGetSync() throws Exception { |
| 50 | MockFace face = new MockFace(); |
| 51 | face.registerPrefix(new Name("/segmented/data"), new OnInterest() { |
| 52 | private int count = 0; |
| 53 | private int max = 9; |
| 54 | |
| 55 | @Override |
| 56 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 57 | Data data = new Data(interest.getName()); |
| 58 | if (!SegmentedClient.hasSegment(data.getName())) { |
| 59 | data.getName().appendSegment(0); |
| 60 | } |
| 61 | data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00)); |
| 62 | data.setContent(new Blob(".")); |
| 63 | try { |
| 64 | transport.send(data.wireEncode().buf()); |
| 65 | } catch (IOException e) { |
| 66 | fail(e.getMessage()); |
| 67 | } |
| 68 | } |
| 69 | }, null); |
| 70 | |
| 71 | Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0)); |
| 72 | assertEquals(10, data.getContent().size()); |
| 73 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame^] | 74 | |
| 75 | /** |
| 76 | * Test that a failed request fails with an exception. |
| 77 | * |
| 78 | * @throws java.lang.Exception |
| 79 | */ |
| 80 | @Test(expected = ExecutionException.class) |
| 81 | public void testFailureToRetrieve() throws Exception { |
| 82 | // setup face |
| 83 | MockTransport transport = new MockTransport(); |
| 84 | Face face = new Face(transport, null); |
| 85 | |
| 86 | // retrieve non-existent data, should timeout |
| 87 | logger.info("Client expressing interest asynchronously: /test/no-data"); |
| 88 | List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data")); |
| 89 | |
| 90 | // the list of future packets should be initialized |
| 91 | assertEquals(1, futureSegments.size()); |
| 92 | assertTrue(futureSegments.get(0).isDone()); |
| 93 | |
| 94 | // should throw error |
| 95 | futureSegments.get(0).get(); |
| 96 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 97 | } |