blob: 7c010ec9b3764ced4b01cd7606a3c6e7c78011fe [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
56 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
57
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;
66 delete closure;
67}
68
69BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
70{
71
72 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
73
74 Selectors selectors;
75 selectors.interestLifetime(1);
76
77 string n1 = "/random/01";
78 c1->sendInterest(Name(n1), closure, selectors);
79 sleep(2);
80 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
81 usleep(100000);
82 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
83 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
84
85 string n2 = "/random/02";
86 selectors.interestLifetime(2);
87 c1->sendInterest(Name(n2), closure, selectors);
88 sleep(1);
89 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
90 usleep(100000);
91 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
92 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
93
94 // reset
95 g_dataCallback_counter = 0;
96 g_timeout_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080097 delete closure;
98}
99
100BOOST_AUTO_TEST_SUITE_END()