blob: 4b6d9e74770031d2b7cd7d4166d82fbde6bfdca5 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
2 * File name: ClientTest.java
3 *
4 * Purpose: Test Client.java
5 *
6 * © Copyright Intel Corporation. All rights reserved.
7 * Intel Corporation, 2200 Mission College Boulevard,
8 * Santa Clara, CA 95052-8119, USA
9 */
10package com.intel.jndn.utils;
11
Andrew Brown3f2521a2015-01-17 22:10:15 -080012import org.junit.Test;
13import static org.junit.Assert.*;
14import com.intel.jndn.mock.MockTransport;
Andrew Browndb457052015-02-21 15:41:58 -080015import java.util.concurrent.ExecutionException;
16import java.util.concurrent.TimeUnit;
17import java.util.concurrent.TimeoutException;
18import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080019import net.named_data.jndn.Data;
20import net.named_data.jndn.Face;
21import net.named_data.jndn.Name;
22import net.named_data.jndn.util.Blob;
Andrew Browndb457052015-02-21 15:41:58 -080023import org.junit.rules.ExpectedException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080024
25/**
Andrew Brown070dc892015-01-21 09:55:12 -080026 * Test Client.java
Andrew Brown3f2521a2015-01-17 22:10:15 -080027 * @author Andrew Brown <andrew.brown@intel.com>
28 */
29public class ClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080030
Andrew Brown7b1daf32015-01-19 16:36:01 -080031 /**
32 * Setup logging
33 */
Andrew Browndb457052015-02-21 15:41:58 -080034 private static final Logger logger = Logger.getLogger(Client.class.getName());
Andrew Brown3f2521a2015-01-17 22:10:15 -080035
Andrew Brown7b1daf32015-01-19 16:36:01 -080036 /**
37 * Test retrieving data synchronously
38 */
39 @Test
40 public void testGetSync() {
41 // setup face
42 MockTransport transport = new MockTransport();
43 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080044
Andrew Brown7b1daf32015-01-19 16:36:01 -080045 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080046 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080047 response.setContent(new Blob("..."));
48 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080049
Andrew Brown7b1daf32015-01-19 16:36:01 -080050 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080051 logger.info("Client expressing interest synchronously: /test/sync");
Andrew Brown7b1daf32015-01-19 16:36:01 -080052 Client client = new Client();
Andrew Brown070dc892015-01-21 09:55:12 -080053 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 assertEquals(new Blob("...").buf(), data.getContent().buf());
55 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080056
Andrew Brown7b1daf32015-01-19 16:36:01 -080057 /**
58 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080059 *
60 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080061 */
62 @Test
Andrew Browndb457052015-02-21 15:41:58 -080063 public void testGetAsync() throws InterruptedException, ExecutionException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080064 // setup face
65 MockTransport transport = new MockTransport();
66 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080067
Andrew Brown7b1daf32015-01-19 16:36:01 -080068 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080069 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080070 response.setContent(new Blob("..."));
71 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080072
Andrew Brown7b1daf32015-01-19 16:36:01 -080073 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080074 logger.info("Client expressing interest asynchronously: /test/async");
Andrew Brown7b1daf32015-01-19 16:36:01 -080075 Client client = new Client();
Andrew Browndb457052015-02-21 15:41:58 -080076 FutureData futureData = client.getAsync(face, new Name("/test/async"));
77
78 assertTrue(!futureData.isDone());
79 futureData.get();
80 assertTrue(futureData.isDone());
81 assertEquals(new Blob("...").toString(), futureData.get().getContent().toString());
Andrew Brown7b1daf32015-01-19 16:36:01 -080082 }
Andrew Brown070dc892015-01-21 09:55:12 -080083
84 /**
85 * Test that asynchronous client times out correctly
86 *
87 * @throws InterruptedException
88 */
89 @Test
Andrew Browndb457052015-02-21 15:41:58 -080090 public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException {
Andrew Brown070dc892015-01-21 09:55:12 -080091 // setup face
92 MockTransport transport = new MockTransport();
93 Face face = new Face(transport, null);
94
95 // retrieve non-existent data, should timeout
96 logger.info("Client expressing interest asynchronously: /test/timeout");
Andrew Browndb457052015-02-21 15:41:58 -080097 FutureData futureData = Client.getDefault().getAsync(face, new Name("/test/timeout"));
Andrew Brown070dc892015-01-21 09:55:12 -080098
Andrew Browndb457052015-02-21 15:41:58 -080099 ExpectedException.none().expect(TimeoutException.class);
100 futureData.get(50, TimeUnit.MILLISECONDS);
Andrew Brown070dc892015-01-21 09:55:12 -0800101 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800102}