blob: d5188de4a51a8f7a4ad136ba5263c23b6508a456 [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
Alexander Afanasyevf278db32013-01-21 14:41:01 -080034void dataCallback(const Name &name, Ccnx::PcoPtr pco)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080035{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080036 BytesPtr content = pco->contentPtr ();
37 string msg(reinterpret_cast<const char *> (head (*content)), content->size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080038 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080039 BOOST_CHECK_EQUAL(name, msg);
40}
41
42Closure::TimeoutCallbackReturnValue timeout(const Name &name)
43{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080044 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080045 return Closure::RESULT_OK;
46}
47
48BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
49{
50 Name prefix1("/c1");
51 Name prefix2("/c2");
52
53 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -080054 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080055 c2->setInterestFilter(prefix2, bind(publish2, _1));
56
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080057 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080058
59 c1->sendInterest(Name("/c2/hi"), closure);
60 usleep(100000);
61 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080062 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080063 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
64 // reset
65 g_dataCallback_counter = 0;
66 g_timeout_counter = 0;
Zhenkai Zhu3722d432013-01-04 16:23:36 -080067}
68
69BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
70{
71
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080072 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3722d432013-01-04 16:23:36 -080073
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}
98
99BOOST_AUTO_TEST_SUITE_END()