blob: 0386db182b5d9c52d069711688c3c867ca998fe7 [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
Andrew Brown3f2521a2015-01-17 22:10:15 -080016import org.junit.Test;
17import static org.junit.Assert.*;
18import com.intel.jndn.mock.MockTransport;
andrewsbrown69d53292015-03-17 19:37:34 +010019import java.io.IOException;
Andrew Browndb457052015-02-21 15:41:58 -080020import java.util.concurrent.ExecutionException;
andrewsbrown69d53292015-03-17 19:37:34 +010021import java.util.concurrent.Future;
Andrew Browndb457052015-02-21 15:41:58 -080022import java.util.concurrent.TimeUnit;
23import java.util.concurrent.TimeoutException;
24import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080025import net.named_data.jndn.Data;
26import net.named_data.jndn.Face;
27import net.named_data.jndn.Name;
28import net.named_data.jndn.util.Blob;
Andrew Browndb457052015-02-21 15:41:58 -080029import org.junit.rules.ExpectedException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080030
31/**
Andrew Brown070dc892015-01-21 09:55:12 -080032 * Test Client.java
andrewsbrown69d53292015-03-17 19:37:34 +010033 *
Andrew Brown3f2521a2015-01-17 22:10:15 -080034 * @author Andrew Brown <andrew.brown@intel.com>
35 */
andrewsbrown90712cb2015-03-31 14:44:12 -070036public class SimpleClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080037
andrewsbrown69d53292015-03-17 19:37:34 +010038 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
39 public ExpectedException thrown = ExpectedException.none();
Andrew Brown3f2521a2015-01-17 22:10:15 -080040
Andrew Brown7b1daf32015-01-19 16:36:01 -080041 /**
42 * Test retrieving data synchronously
andrewsbrown69d53292015-03-17 19:37:34 +010043 *
44 * @throws java.io.IOException
Andrew Brown7b1daf32015-01-19 16:36:01 -080045 */
46 @Test
andrewsbrown69d53292015-03-17 19:37:34 +010047 public void testGetSync() throws IOException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080048 // setup face
49 MockTransport transport = new MockTransport();
50 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080051
Andrew Brown7b1daf32015-01-19 16:36:01 -080052 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080053 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 response.setContent(new Blob("..."));
55 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080056
Andrew Brown7b1daf32015-01-19 16:36:01 -080057 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080058 logger.info("Client expressing interest synchronously: /test/sync");
andrewsbrown69d53292015-03-17 19:37:34 +010059 SimpleClient client = new SimpleClient();
Andrew Brown070dc892015-01-21 09:55:12 -080060 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080061 assertEquals(new Blob("...").buf(), data.getContent().buf());
62 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080063
Andrew Brown7b1daf32015-01-19 16:36:01 -080064 /**
65 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080066 *
67 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080068 */
69 @Test
Andrew Browndb457052015-02-21 15:41:58 -080070 public void testGetAsync() throws InterruptedException, ExecutionException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080071 // setup face
72 MockTransport transport = new MockTransport();
73 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080074
Andrew Brown7b1daf32015-01-19 16:36:01 -080075 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080076 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080077 response.setContent(new Blob("..."));
78 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080079
Andrew Brown7b1daf32015-01-19 16:36:01 -080080 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080081 logger.info("Client expressing interest asynchronously: /test/async");
andrewsbrown69d53292015-03-17 19:37:34 +010082 SimpleClient client = new SimpleClient();
83 Future<Data> futureData = client.getAsync(face, new Name("/test/async"));
84
Andrew Browndb457052015-02-21 15:41:58 -080085 assertTrue(!futureData.isDone());
86 futureData.get();
87 assertTrue(futureData.isDone());
88 assertEquals(new Blob("...").toString(), futureData.get().getContent().toString());
Andrew Brown7b1daf32015-01-19 16:36:01 -080089 }
Andrew Brown070dc892015-01-21 09:55:12 -080090
91 /**
92 * Test that asynchronous client times out correctly
andrewsbrown69d53292015-03-17 19:37:34 +010093 *
94 * @throws InterruptedException
Andrew Brown070dc892015-01-21 09:55:12 -080095 */
andrewsbrown69d53292015-03-17 19:37:34 +010096 @Test(expected = TimeoutException.class)
Andrew Browndb457052015-02-21 15:41:58 -080097 public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException {
Andrew Brown070dc892015-01-21 09:55:12 -080098 // 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");
andrewsbrown69d53292015-03-17 19:37:34 +0100104 Future<Data> futureData = SimpleClient.getDefault().getAsync(face, new Name("/test/timeout"));
105
106 // expect an exception
Andrew Browndb457052015-02-21 15:41:58 -0800107 futureData.get(50, TimeUnit.MILLISECONDS);
Andrew Brown070dc892015-01-21 09:55:12 -0800108 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800109}