blob: 35883a92ca01d8dc33de9224fc5dcc19fda7b3bd [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
andrewsbrown4feb2da2015-03-03 16:05:29 -08002 * 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 Brown3f2521a2015-01-17 22:10:15 -080013 */
14package com.intel.jndn.utils;
15
andrewsbrown8d5ae292015-07-01 17:38:22 -070016import com.intel.jndn.mock.MockFace;
Andrew Brown3f2521a2015-01-17 22:10:15 -080017import org.junit.Test;
18import static org.junit.Assert.*;
19import com.intel.jndn.mock.MockTransport;
andrewsbrown69d53292015-03-17 19:37:34 +010020import java.io.IOException;
andrewsbrown8d5ae292015-07-01 17:38:22 -070021import java.util.concurrent.CompletableFuture;
Andrew Browndb457052015-02-21 15:41:58 -080022import java.util.concurrent.ExecutionException;
andrewsbrown69d53292015-03-17 19:37:34 +010023import java.util.concurrent.Future;
Andrew Browndb457052015-02-21 15:41:58 -080024import java.util.concurrent.TimeUnit;
25import java.util.concurrent.TimeoutException;
26import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080027import net.named_data.jndn.Data;
28import net.named_data.jndn.Face;
andrewsbrown8d5ae292015-07-01 17:38:22 -070029import net.named_data.jndn.Interest;
Andrew Brown3f2521a2015-01-17 22:10:15 -080030import net.named_data.jndn.Name;
andrewsbrown8d5ae292015-07-01 17:38:22 -070031import net.named_data.jndn.encoding.EncodingException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080032import net.named_data.jndn.util.Blob;
Andrew Browndb457052015-02-21 15:41:58 -080033import org.junit.rules.ExpectedException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080034
35/**
andrewsbrown8d5ae292015-07-01 17:38:22 -070036 * Test SimpleClient.java
andrewsbrown69d53292015-03-17 19:37:34 +010037 *
Andrew Brown3f2521a2015-01-17 22:10:15 -080038 * @author Andrew Brown <andrew.brown@intel.com>
39 */
andrewsbrown90712cb2015-03-31 14:44:12 -070040public class SimpleClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080041
andrewsbrown69d53292015-03-17 19:37:34 +010042 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
43 public ExpectedException thrown = ExpectedException.none();
Andrew Brown3f2521a2015-01-17 22:10:15 -080044
Andrew Brown7b1daf32015-01-19 16:36:01 -080045 @Test
andrewsbrown69d53292015-03-17 19:37:34 +010046 public void testGetSync() throws IOException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080047 // setup face
48 MockTransport transport = new MockTransport();
49 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080050
Andrew Brown7b1daf32015-01-19 16:36:01 -080051 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080052 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080053 response.setContent(new Blob("..."));
54 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080055
Andrew Brown7b1daf32015-01-19 16:36:01 -080056 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080057 logger.info("Client expressing interest synchronously: /test/sync");
andrewsbrown69d53292015-03-17 19:37:34 +010058 SimpleClient client = new SimpleClient();
Andrew Brown070dc892015-01-21 09:55:12 -080059 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080060 assertEquals(new Blob("...").buf(), data.getContent().buf());
61 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080062
Andrew Brown7b1daf32015-01-19 16:36:01 -080063 @Test
andrewsbrown8d5ae292015-07-01 17:38:22 -070064 public void testGetAsync() throws InterruptedException, ExecutionException, IOException, EncodingException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 // setup face
66 MockTransport transport = new MockTransport();
67 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080068
Andrew Brown7b1daf32015-01-19 16:36:01 -080069 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080070 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080071 response.setContent(new Blob("..."));
72 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080073
Andrew Brown7b1daf32015-01-19 16:36:01 -080074 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080075 logger.info("Client expressing interest asynchronously: /test/async");
andrewsbrown69d53292015-03-17 19:37:34 +010076 SimpleClient client = new SimpleClient();
77 Future<Data> futureData = client.getAsync(face, new Name("/test/async"));
Andrew Browndb457052015-02-21 15:41:58 -080078 assertTrue(!futureData.isDone());
andrewsbrown8d5ae292015-07-01 17:38:22 -070079
80 // process events to retrieve data
81 face.processEvents();
Andrew Browndb457052015-02-21 15:41:58 -080082 assertTrue(futureData.isDone());
83 assertEquals(new Blob("...").toString(), futureData.get().getContent().toString());
Andrew Brown7b1daf32015-01-19 16:36:01 -080084 }
Andrew Brown070dc892015-01-21 09:55:12 -080085
andrewsbrown8d5ae292015-07-01 17:38:22 -070086 @Test
87 public void testTimeout() throws Exception {
Andrew Brown070dc892015-01-21 09:55:12 -080088 // setup face
89 MockTransport transport = new MockTransport();
90 Face face = new Face(transport, null);
91
92 // retrieve non-existent data, should timeout
93 logger.info("Client expressing interest asynchronously: /test/timeout");
andrewsbrown8d5ae292015-07-01 17:38:22 -070094 Interest interest = new Interest(new Name("/test/timeout"), 1);
95 CompletableFuture<Data> futureData = SimpleClient.getDefault().getAsync(face, interest);
andrewsbrown69d53292015-03-17 19:37:34 +010096
andrewsbrown8d5ae292015-07-01 17:38:22 -070097 // wait for NDN timeout
98 Thread.sleep(2);
99 face.processEvents();
100
101 // verify that the client is completing the future with a TimeoutException
102 assertTrue(futureData.isDone());
103 assertTrue(futureData.isCompletedExceptionally());
104 try{
105 futureData.get();
106 }
107 catch(ExecutionException e){
108 assertTrue(e.getCause() instanceof TimeoutException);
109 }
Andrew Brown070dc892015-01-21 09:55:12 -0800110 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700111
andrewsbrown8d5ae292015-07-01 17:38:22 -0700112 @Test(expected = Exception.class)
113 public void testAsyncFailureToRetrieve() throws Exception {
114 Face face = new MockFace();
115
116 logger.info("Client expressing interest asynchronously: /test/no-data");
117 Interest interest = new Interest(new Name("/test/no-data"), 10);
118 Future future = SimpleClient.getDefault().getAsync(face, interest);
119
120 face.processEvents();
121 future.get(15, TimeUnit.MILLISECONDS);
andrewsbrown4dddd472015-04-01 14:28:46 -0700122 }
123
andrewsbrown4dddd472015-04-01 14:28:46 -0700124 @Test(expected = IOException.class)
125 public void testSyncFailureToRetrieve() throws IOException {
andrewsbrown8d5ae292015-07-01 17:38:22 -0700126 logger.info("Client expressing interest synchronously: /test/no-data");
127 Interest interest = new Interest(new Name("/test/no-data"), 10);
128 SimpleClient.getDefault().getSync(new Face(), interest);
andrewsbrown4dddd472015-04-01 14:28:46 -0700129 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800130}