blob: 7f2efb73740287aabdac872151b25fe71e933cb6 [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
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -08008#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhu3b82d432013-01-03 22:48:40 -08009#include <boost/test/unit_test.hpp>
10
11
12using namespace Ccnx;
13using namespace std;
14using namespace boost;
15
16BOOST_AUTO_TEST_SUITE(CcnxWrapperTests)
17
18CcnxWrapperPtr c1(new CcnxWrapper());
19CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080020int g_timeout_counter = 0;
21int g_dataCallback_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080022
23void publish1(const Name &name)
24{
25 string content = name.toString();
26 c1->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
27}
28
29void publish2(const Name &name)
30{
31 string content = name.toString();
32 c2->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
33}
34
Alexander Afanasyevf278db32013-01-21 14:41:01 -080035void dataCallback(const Name &name, Ccnx::PcoPtr pco)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080036{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080037 BytesPtr content = pco->contentPtr ();
38 string msg(reinterpret_cast<const char *> (head (*content)), content->size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080039 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080040 BOOST_CHECK_EQUAL(name, msg);
41}
42
43Closure::TimeoutCallbackReturnValue timeout(const Name &name)
44{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080045 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080046 return Closure::RESULT_OK;
47}
48
49BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
50{
51 Name prefix1("/c1");
52 Name prefix2("/c2");
53
54 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -080055 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080056 c2->setInterestFilter(prefix2, bind(publish2, _1));
57
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080058 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080059
60 c1->sendInterest(Name("/c2/hi"), closure);
61 usleep(100000);
62 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080063 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080064 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
65 // reset
66 g_dataCallback_counter = 0;
67 g_timeout_counter = 0;
Zhenkai Zhu3722d432013-01-04 16:23:36 -080068}
69
70BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
71{
72
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080073 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1));
Zhenkai Zhu3722d432013-01-04 16:23:36 -080074
75 Selectors selectors;
76 selectors.interestLifetime(1);
77
78 string n1 = "/random/01";
79 c1->sendInterest(Name(n1), closure, selectors);
80 sleep(2);
81 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4);
82 usleep(100000);
83 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
84 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
85
86 string n2 = "/random/02";
87 selectors.interestLifetime(2);
88 c1->sendInterest(Name(n2), closure, selectors);
89 sleep(1);
90 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4);
91 usleep(100000);
92 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
93 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
94
95 // reset
96 g_dataCallback_counter = 0;
97 g_timeout_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080098}
99
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800100BOOST_AUTO_TEST_CASE (CcnxWrapperSigningTest)
101{
102 Bytes data;
103 data.resize(1024);
104 for (int i = 0; i < 1024; i++)
105 {
106 data[i] = 'm';
107 }
108
109 Name name("/signingtest");
110
111 posix_time::ptime start = posix_time::second_clock::local_time();
112 for (uint64_t i = 0; i < 10000; i++)
113 {
114 Name n = name;
115 n.appendComp(i);
116 c1->publishData(n, data, 10);
117 }
118 posix_time::ptime end = posix_time::second_clock::local_time();
119
120 posix_time::time_duration duration = end - start;
121
122 cout << "Publishing 10000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl;
123 cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 10000.0 << " milliseconds" << endl;
124}
125
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800126BOOST_AUTO_TEST_SUITE_END()