blob: 8636a2d0287340035b0d472770d0132f56aaca61 [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));
52 c2->setInterestFilter(prefix2, bind(publish2, _1));
53
54 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
55
56 c1->sendInterest(Name("/c2/hi"), closure);
57 usleep(100000);
58 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080059 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080060 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
61 // reset
62 g_dataCallback_counter = 0;
63 g_timeout_counter = 0;
64 delete closure;
65}
66
67BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
68{
69
70 Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1));
71
72 Selectors selectors;
73 selectors.interestLifetime(1);
74
75 string n1 = "/random/01";
76 c1->sendInterest(Name(n1), closure, selectors);
77 sleep(2);
78 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
79 usleep(100000);
80 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
81 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
82
83 string n2 = "/random/02";
84 selectors.interestLifetime(2);
85 c1->sendInterest(Name(n2), closure, selectors);
86 sleep(1);
87 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
88 usleep(100000);
89 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
90 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
91
92 // reset
93 g_dataCallback_counter = 0;
94 g_timeout_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080095 delete closure;
96}
97
98BOOST_AUTO_TEST_SUITE_END()