blob: 58d8d246f57972c20de8fb9ac6c269271cfe22c9 [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"
4#include "ccnx-pco.h"
5#include <unistd.h>
6
7#include <boost/test/unit_test.hpp>
8
9
10using namespace Ccnx;
11using namespace std;
12using namespace boost;
13
14BOOST_AUTO_TEST_SUITE(CcnxWrapperTests)
15
16CcnxWrapperPtr c1(new CcnxWrapper());
17CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080018int g_timeout_counter = 0;
19int g_dataCallback_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080020
21void publish1(const Name &name)
22{
23 string content = name.toString();
24 c1->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
25}
26
27void publish2(const Name &name)
28{
29 string content = name.toString();
30 c2->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
31}
32
33void dataCallback(const Name &name, const Bytes &content)
34{
35 string msg((const char*)&content[0], content.size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080036 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080037 BOOST_CHECK_EQUAL(name, msg);
38}
39
40Closure::TimeoutCallbackReturnValue timeout(const Name &name)
41{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080042 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080043 return Closure::RESULT_OK;
44}
45
46BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
47{
48 Name prefix1("/c1");
49 Name prefix2("/c2");
50
51 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -080052 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080053 c2->setInterestFilter(prefix2, bind(publish2, _1));
54
55 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
56
57 c1->sendInterest(Name("/c2/hi"), closure);
58 usleep(100000);
59 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080060 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080061 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
62 // reset
63 g_dataCallback_counter = 0;
64 g_timeout_counter = 0;
65 delete closure;
66}
67
68BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
69{
70
71 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
72
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 delete closure;
97}
98
99BOOST_AUTO_TEST_SUITE_END()