Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 1 | #include "ccnx-wrapper.h" |
| 2 | #include "ccnx-closure.h" |
| 3 | #include "ccnx-name.h" |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 4 | #include "ccnx-selectors.h" |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 5 | #include "ccnx-pco.h" |
| 6 | #include <unistd.h> |
| 7 | |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 8 | #include <boost/date_time/posix_time/posix_time.hpp> |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 9 | #include <boost/test/unit_test.hpp> |
| 10 | |
| 11 | |
| 12 | using namespace Ccnx; |
| 13 | using namespace std; |
| 14 | using namespace boost; |
| 15 | |
| 16 | BOOST_AUTO_TEST_SUITE(CcnxWrapperTests) |
| 17 | |
| 18 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 19 | CcnxWrapperPtr c2(new CcnxWrapper()); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 20 | int g_timeout_counter = 0; |
| 21 | int g_dataCallback_counter = 0; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 22 | |
| 23 | void 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 | |
| 29 | void 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 Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 35 | void dataCallback(const Name &name, Ccnx::PcoPtr pco) |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 37 | BytesPtr content = pco->contentPtr (); |
| 38 | string msg(reinterpret_cast<const char *> (head (*content)), content->size()); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 39 | g_dataCallback_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 40 | BOOST_CHECK_EQUAL(name, msg); |
| 41 | } |
| 42 | |
| 43 | Closure::TimeoutCallbackReturnValue timeout(const Name &name) |
| 44 | { |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 45 | g_timeout_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 46 | return Closure::RESULT_OK; |
| 47 | } |
| 48 | |
| 49 | BOOST_AUTO_TEST_CASE (CcnxWrapperTest) |
| 50 | { |
| 51 | Name prefix1("/c1"); |
| 52 | Name prefix2("/c2"); |
| 53 | |
| 54 | c1->setInterestFilter(prefix1, bind(publish1, _1)); |
Zhenkai Zhu | 03a511d | 2013-01-04 17:13:28 -0800 | [diff] [blame] | 55 | usleep(100000); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 56 | c2->setInterestFilter(prefix2, bind(publish2, _1)); |
| 57 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 58 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1)); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 59 | |
| 60 | c1->sendInterest(Name("/c2/hi"), closure); |
| 61 | usleep(100000); |
| 62 | c2->sendInterest(Name("/c1/hi"), closure); |
Zhenkai Zhu | d34106a | 2013-01-04 15:51:55 -0800 | [diff] [blame] | 63 | sleep(1); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
| 65 | // reset |
| 66 | g_dataCallback_counter = 0; |
| 67 | g_timeout_counter = 0; |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE (CcnxWrapperSelector) |
| 71 | { |
| 72 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 73 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1)); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 74 | |
| 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 Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 100 | BOOST_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 Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 126 | BOOST_AUTO_TEST_SUITE_END() |