blob: b64f513b520ce3e3268eb18d53ac98a07db63550 [file] [log] [blame]
Zhenkai Zhu3b82d432013-01-03 22:48:40 -08001#include "ccnx-wrapper.h"
2#include "ccnx-closure.h"
3#include "ccnx-name.h"
Zhenkai Zhua6472e02013-01-06 14:33:37 -08004#include "ccnx-selectors.h"
Zhenkai Zhu3b82d432013-01-03 22:48:40 -08005#include "ccnx-pco.h"
6#include <unistd.h>
7
8#include <boost/test/unit_test.hpp>
9
10
11using namespace Ccnx;
12using namespace std;
13using namespace boost;
14
15BOOST_AUTO_TEST_SUITE(CcnxWrapperTests)
16
17CcnxWrapperPtr c1(new CcnxWrapper());
18CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080019int g_timeout_counter = 0;
20int g_dataCallback_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080021
22void publish1(const Name &name)
23{
24 string content = name.toString();
25 c1->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
26}
27
28void publish2(const Name &name)
29{
30 string content = name.toString();
31 c2->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
32}
33
34void dataCallback(const Name &name, const Bytes &content)
35{
36 string msg((const char*)&content[0], content.size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080037 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080038 BOOST_CHECK_EQUAL(name, msg);
39}
40
41Closure::TimeoutCallbackReturnValue timeout(const Name &name)
42{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080043 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080044 return Closure::RESULT_OK;
45}
46
47BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
48{
49 Name prefix1("/c1");
50 Name prefix2("/c2");
51
52 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -080053 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080054 c2->setInterestFilter(prefix2, bind(publish2, _1));
55
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080056 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080057
58 c1->sendInterest(Name("/c2/hi"), closure);
59 usleep(100000);
60 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080061 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080062 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
63 // reset
64 g_dataCallback_counter = 0;
65 g_timeout_counter = 0;
Zhenkai Zhu3722d432013-01-04 16:23:36 -080066}
67
68BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
69{
70
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080071 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3722d432013-01-04 16:23:36 -080072
73 Selectors selectors;
74 selectors.interestLifetime(1);
75
76 string n1 = "/random/01";
77 c1->sendInterest(Name(n1), closure, selectors);
78 sleep(2);
79 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
80 usleep(100000);
81 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
82 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
83
84 string n2 = "/random/02";
85 selectors.interestLifetime(2);
86 c1->sendInterest(Name(n2), closure, selectors);
87 sleep(1);
88 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
89 usleep(100000);
90 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
91 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
92
93 // reset
94 g_dataCallback_counter = 0;
95 g_timeout_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080096}
97
98BOOST_AUTO_TEST_SUITE_END()