blob: d4ca95182a18b90c42819b02bbb2dd02bbfb2bab [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;
Andrew Browndb457052015-02-21 15:41:58 -080019import java.util.concurrent.ExecutionException;
20import java.util.concurrent.TimeUnit;
21import java.util.concurrent.TimeoutException;
22import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080023import net.named_data.jndn.Data;
24import net.named_data.jndn.Face;
25import net.named_data.jndn.Name;
26import net.named_data.jndn.util.Blob;
Andrew Browndb457052015-02-21 15:41:58 -080027import org.junit.rules.ExpectedException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080028
29/**
Andrew Brown070dc892015-01-21 09:55:12 -080030 * Test Client.java
Andrew Brown3f2521a2015-01-17 22:10:15 -080031 * @author Andrew Brown <andrew.brown@intel.com>
32 */
33public class ClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080034
Andrew Brown7b1daf32015-01-19 16:36:01 -080035 /**
36 * Setup logging
37 */
Andrew Browndb457052015-02-21 15:41:58 -080038 private static final Logger logger = Logger.getLogger(Client.class.getName());
Andrew Brown3f2521a2015-01-17 22:10:15 -080039
Andrew Brown7b1daf32015-01-19 16:36:01 -080040 /**
41 * Test retrieving data synchronously
42 */
43 @Test
44 public void testGetSync() {
45 // setup face
46 MockTransport transport = new MockTransport();
47 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080048
Andrew Brown7b1daf32015-01-19 16:36:01 -080049 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080050 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080051 response.setContent(new Blob("..."));
52 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080053
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080055 logger.info("Client expressing interest synchronously: /test/sync");
Andrew Brown7b1daf32015-01-19 16:36:01 -080056 Client client = new Client();
Andrew Brown070dc892015-01-21 09:55:12 -080057 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080058 assertEquals(new Blob("...").buf(), data.getContent().buf());
59 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080060
Andrew Brown7b1daf32015-01-19 16:36:01 -080061 /**
62 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080063 *
64 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 */
66 @Test
Andrew Browndb457052015-02-21 15:41:58 -080067 public void testGetAsync() throws InterruptedException, ExecutionException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080068 // setup face
69 MockTransport transport = new MockTransport();
70 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080071
Andrew Brown7b1daf32015-01-19 16:36:01 -080072 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080073 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080074 response.setContent(new Blob("..."));
75 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080076
Andrew Brown7b1daf32015-01-19 16:36:01 -080077 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080078 logger.info("Client expressing interest asynchronously: /test/async");
Andrew Brown7b1daf32015-01-19 16:36:01 -080079 Client client = new Client();
Andrew Browndb457052015-02-21 15:41:58 -080080 FutureData futureData = client.getAsync(face, new Name("/test/async"));
81
82 assertTrue(!futureData.isDone());
83 futureData.get();
84 assertTrue(futureData.isDone());
85 assertEquals(new Blob("...").toString(), futureData.get().getContent().toString());
Andrew Brown7b1daf32015-01-19 16:36:01 -080086 }
Andrew Brown070dc892015-01-21 09:55:12 -080087
88 /**
89 * Test that asynchronous client times out correctly
90 *
91 * @throws InterruptedException
92 */
93 @Test
Andrew Browndb457052015-02-21 15:41:58 -080094 public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException {
Andrew Brown070dc892015-01-21 09:55:12 -080095 // setup face
96 MockTransport transport = new MockTransport();
97 Face face = new Face(transport, null);
98
99 // retrieve non-existent data, should timeout
100 logger.info("Client expressing interest asynchronously: /test/timeout");
Andrew Browndb457052015-02-21 15:41:58 -0800101 FutureData futureData = Client.getDefault().getAsync(face, new Name("/test/timeout"));
Andrew Brown070dc892015-01-21 09:55:12 -0800102
Andrew Browndb457052015-02-21 15:41:58 -0800103 ExpectedException.none().expect(TimeoutException.class);
104 futureData.get(50, TimeUnit.MILLISECONDS);
Andrew Brown070dc892015-01-21 09:55:12 -0800105 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800106}