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