blob: 88c39cc9e8807184fe7e971a2aefe0347c3c621e [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;
15import net.named_data.jndn.Data;
16import net.named_data.jndn.Face;
17import net.named_data.jndn.Name;
18import net.named_data.jndn.util.Blob;
Andrew Brown7b1daf32015-01-19 16:36:01 -080019import org.apache.logging.log4j.LogManager;
20import org.apache.logging.log4j.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080021
22/**
Andrew Brown070dc892015-01-21 09:55:12 -080023 * Test Client.java
Andrew Brown3f2521a2015-01-17 22:10:15 -080024 * @author Andrew Brown <andrew.brown@intel.com>
25 */
26public class ClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080027
Andrew Brown7b1daf32015-01-19 16:36:01 -080028 /**
29 * Setup logging
30 */
31 private static final Logger logger = LogManager.getLogger();
Andrew Brown3f2521a2015-01-17 22:10:15 -080032
Andrew Brown7b1daf32015-01-19 16:36:01 -080033 /**
34 * Test retrieving data synchronously
35 */
36 @Test
37 public void testGetSync() {
38 // setup face
39 MockTransport transport = new MockTransport();
40 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080041
Andrew Brown7b1daf32015-01-19 16:36:01 -080042 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080043 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080044 response.setContent(new Blob("..."));
45 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080046
Andrew Brown7b1daf32015-01-19 16:36:01 -080047 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080048 logger.info("Client expressing interest synchronously: /test/sync");
Andrew Brown7b1daf32015-01-19 16:36:01 -080049 Client client = new Client();
Andrew Brown070dc892015-01-21 09:55:12 -080050 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080051 assertEquals(new Blob("...").buf(), data.getContent().buf());
52 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080053
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 /**
55 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080056 *
57 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080058 */
59 @Test
60 public void testGetAsync() throws InterruptedException {
61 // setup face
62 MockTransport transport = new MockTransport();
63 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080064
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080066 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080067 response.setContent(new Blob("..."));
68 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080069
Andrew Brown7b1daf32015-01-19 16:36:01 -080070 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080071 logger.info("Client expressing interest asynchronously: /test/async");
Andrew Brown7b1daf32015-01-19 16:36:01 -080072 Client client = new Client();
Andrew Browna450fad2015-01-22 11:24:40 -080073 NDNObserver observer = client.get(face, new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080074
75 // wait
Andrew Brown070dc892015-01-21 09:55:12 -080076 while (observer.eventCount() == 0) {
Andrew Brown7b1daf32015-01-19 16:36:01 -080077 Thread.sleep(10);
78 }
Andrew Brown070dc892015-01-21 09:55:12 -080079 assertEquals(1, observer.eventCount());
80 assertEquals(1, observer.dataCount());
Andrew Brown7b1daf32015-01-19 16:36:01 -080081 Data data = (Data) observer.getFirst().getPacket();
82 assertEquals(new Blob("...").buf(), data.getContent().buf());
83 }
Andrew Brown070dc892015-01-21 09:55:12 -080084
85 /**
86 * Test that asynchronous client times out correctly
87 *
88 * @throws InterruptedException
89 */
90 @Test
91 public void testTimeout() throws InterruptedException {
92 // setup face
93 MockTransport transport = new MockTransport();
94 Face face = new Face(transport, null);
95
96 // retrieve non-existent data, should timeout
97 logger.info("Client expressing interest asynchronously: /test/timeout");
Andrew Browna450fad2015-01-22 11:24:40 -080098 NDNObserver observer = Client.getDefault().get(face, new Name("/test/timeout"));
Andrew Brown070dc892015-01-21 09:55:12 -080099
100 // wait
101 while (observer.errorCount() == 0) {
102 Thread.sleep(100);
103 }
104 Exception e = (Exception) observer.getFirst().getPacket();
105 assertEquals(1, observer.errorCount());
106 }
107
108 /**
109 * Test that callback is called on event
110 * @throws InterruptedException
111 */
112 @Test
113 public void testCallback() throws InterruptedException {
114 // setup face
115 MockTransport transport = new MockTransport();
116 Face face = new Face(transport, null);
117
118 // setup return data
119 Data response = new Data(new Name("/test/callback"));
120 response.setContent(new Blob("..."));
121 transport.respondWith(response);
122
123 // retrieve non-existent data, should timeout
124 logger.info("Client expressing interest asynchronously: /test/callback");
Andrew Browna450fad2015-01-22 11:24:40 -0800125 NDNObserver observer = Client.getDefault().get(face, new Name("/test/callback"));
Andrew Brown070dc892015-01-21 09:55:12 -0800126 observer.then(new OnEvent(){
127 @Override
Andrew Browna450fad2015-01-22 11:24:40 -0800128 public void onEvent(NDNEvent event) {
Andrew Brown070dc892015-01-21 09:55:12 -0800129 assertEquals(new Blob("...").buf(), ((Data) event.getPacket()).getContent().buf());
130 }
131 });
132
133 // wait
134 while (observer.eventCount() == 0) {
135 Thread.sleep(100);
136 }
137 assertEquals(1, observer.eventCount());
138 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800139}